Regarding Serialization in .net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raakhiparimkayala
    New Member
    • Aug 2010
    • 4

    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
    Code:
    [Serializable]
        public class Store
        {
            private int sCount;
            public int stockCount
            {
                get { return sCount; }
                set { sCount = value; }
            }
    
            [B][I][U][NonSerialized][/U][/I][/B]public int temp;
            public  string storeName = "My Local Store";
    
            public Store()
            {
                stockCount = 0;
            }
    
        }
    What happens to the temp variable? According to theory, it should not serialize the temp variable. That means if we serialize using BinarySerialize r, 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.WriteLi ne at the end of program in main

    Any help is much appreciated. Thanks heaps in advance.
    Last edited by raakhiparimkayala; Aug 24 '10, 03:39 PM. Reason: Make the question much clear
  • Joseph Martell
    Recognized Expert New Member
    • Jan 2010
    • 198

    #2
    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.

    Comment

    • raakhiparimkayala
      New Member
      • Aug 2010
      • 4

      #3
      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
      Code:
      Store readStore;
      FileStream flStreamRead = new FileStream("MyStore.dat", FileMode.OpenOrCreate, FileAccess.Read);
      BinaryReader br = new BinaryReader(flStreamRead);
      BinaryFormatter binFormatterDSerialize = new BinaryFormatter();
                      readStore = (Store) binFormatterDSerialize.Deserialize(flStreamRead);
                      flStreamRead.Close();
                      readStore.temp = 120;
                      Console.WriteLine("After Deserialization:\n");
                      Console.WriteLine(@"[NonSerialized]Temp: "+readStore.temp);
                      Console.WriteLine(@"[Serialized]Store Name:" + readStore.storeName);
      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?

      Comment

      Working...