473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Regarding Serialization in .net

Hi there,
I am just a learner in .net . I am presently learning serialization in .net by my own.Just a quick question.
When we serialize any class using serializable attribute, the whole class gets serialized right?

Consider this code
Expand|Select|Wrap|Line Numbers
  1. [Serializable]
  2.     public class Store
  3.     {
  4.         private int sCount;
  5.         public int stockCount
  6.         {
  7.             get { return sCount; }
  8.             set { sCount = value; }
  9.         }
  10.  
  11.         [NonSerialized]public int temp;
  12.         public  string storeName = "My Local Store";
  13.  
  14.         public Store()
  15.         {
  16.             stockCount = 0;
  17.         }
  18.  
  19.     }
  20.  
What happens to the temp variable? According to theory, it should not serialize the temp variable. That means if we serialize using BinarySerializer, it has to store/serialize all the variables (into a file or stream or what ever it is)except temp right?
But when I serialized and then DeSerialized this class into Store object, it is showing the value of temp =0; How did it get back the temp variable as we omitted the serialization of temp variable?
Or will it save the temp variable in file and omits the value or totally omits the temp variable?
Can any one explain what exactly happens to the temp variable?
Note: Source code is also available at http://www.csharpfriends.com/Article...x?articleID=94 I was following this example when displaying the content, I used Console.WriteLine at the end of program in main

Any help is much appreciated. Thanks heaps in advance.
Aug 24 '10 #1
2 991
Joseph Martell
198 Expert 128KB
Remember that in C#, variables are automatically initialized to a default value. In the case of an int, the default value is 0. So, you are correct. Your temp variable was not serialized or deserialized, it was just initialized for your class.

Try setting your temp variable to a specific number besides 0, like 193 or 42, and repeat your experiment. You should find that the variable is back to 0 after deserializing.
Aug 25 '10 #2
Hi Joseph,
Thanks for getting back to me. Your answer gave me confidence that I learnt the right stuff. Yeah I tried the example as you said,it was showing 0. As we discussed, during the serialization, temp variable is not serialized, and how did it appear after deserializing?
This is what I did
Expand|Select|Wrap|Line Numbers
  1. Store readStore;
  2. FileStream flStreamRead = new FileStream("MyStore.dat", FileMode.OpenOrCreate, FileAccess.Read);
  3. BinaryReader br = new BinaryReader(flStreamRead);
  4. BinaryFormatter binFormatterDSerialize = new BinaryFormatter();
  5.                 readStore = (Store) binFormatterDSerialize.Deserialize(flStreamRead);
  6.                 flStreamRead.Close();
  7.                 readStore.temp = 120;
  8.                 Console.WriteLine("After Deserialization:\n");
  9.                 Console.WriteLine(@"[NonSerialized]Temp: "+readStore.temp);
  10.                 Console.WriteLine(@"[Serialized]Store Name:" + readStore.storeName);
  11.  
This is where I was struck up. Or is that due to the explicit type casting which is making the temp variable to appear?
If we do not typecast the variable and use some other procedure to return the object can cause the temp to disappear right?
Aug 25 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Mike Frayn | last post by:
Hello again, Okay, I was reading the following tutorial on serialization of user-defined classes, and it seems pretty straightforward: http://www.sys-con.com/story/?storyid=44199 That...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
3
by: Anders Borum | last post by:
Hello Jon, et all. I am working on a framework with context bound objects (models). The objects expose common functionality, such as the ability to get a serialized Xml representation of an...
2
by: Maximus | last post by:
Hi Everyone, I was using Inprocess session objects, but incase of aspnet process crashes the session objects were lost as a result I decided to shift to out of porocess session objects. For this...
0
by: CJ Taylor | last post by:
hey everyone, thought I would ask this here because the web service groups seems pretty light on conversation. I'm buliding a web service, but keep getting this error. I understand the error,...
4
by: jjkboswell | last post by:
I have an XSD which I have generated a class from using the xsd.exe tool. My XSD contains complex types within it, so that the generated class has member variables which are of types that are also...
2
by: sylvain.ross | last post by:
Hello everybody, I have a very weird problem regarding the serialization of a class that I wrote (named DummyClass in my exemple). My class inherits from DataTable. Then I just try a dummy...
0
by: vairamuthu | last post by:
Why can't we serialize objects which implements IDictionary object
0
by: Shawn B. | last post by:
Greetings, I used the XSD tool to generate a C# class of fields from a very complex Schema that was created in BizTalk 2004. When it encounters one of the properties who's type is a class (I...
0
by: jb489 | last post by:
Hi all, Hope I am posting this in the right forum. I seem to be having a problem when using serialization and web services. <b>Scenario:</b> I have built a web service which includes a...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.