C# – What is boxing and unboxing in .net it

Boxing_Unboxing-1When we move a value type to reference type the data is moved from the stack to the heap. When we move reference type to a value type the data is moved from the heap to the stack.

This movement of data from the heap to stack and vice-versa creates a performance hit.

When the data moves from value types to reference types its termed as ‘Boxing’ and the vice versa is termed as ‘UnBoxing’.

If you compile the above code and see the same in ILDASM you can see in the IL code how ‘boxing’ and ‘unboxing’ looks, below figure demonstrates the same.
Boxing_Unboxing-2

Performance implication of Boxing and unboxing

In order to see how the performance is impacted we ran the below two functions 10,000 times. One function has boxing and the other function is simple. We used a stop watch object to monitor the time taken.

The boxing function was executed in 3542 MS while without boxing the code was executed in 2477 MS. In other words try to avoid boxing and unboxing. In project you always need boxing and unboxing , use it when it’s absolutely necessary.
Boxing_Unboxing-3

Share this:
Share

XML – Rules of XML serialization

  1. XML Serialization serializes the public fields and properties of a class, or the parameters and return values of methods into an XML stream.
  2. XML Serialization does not include methods, indexers, private fields, or read-only properties (except read-only collections). Because XML is an open standard, the resulting XML stream can be processed by any application on any platform. For example: ASP.NET Web Services use XML Serialization to create XML streams to pass as data throughout the Internet or Intranets. Conversely, deserialization takes such streams and constructs an object.
  3. The following items can be serialized using XmlSerialzer:
    • Public read/write properties.
    • Public fields.
    • Classes that implement ICollection or IEnumerable.
    • XmlElement objects.
    • XmlNode objects.
    • DataSet objects.
  4. XmlSerialzer gives complete control over serializing an object into XML. For example, XmlSerialzer enables you to:
    • Specify whether a field or a property should be encoded as an element or as an attribute.
    • Specify which XML namespace to use.
    • Specify the name of an element or attribute if a field / property name is inappropriate.

XML Serialization Considerations

Following should be considered when using XmlSerialzer class:

  1. Type identity and assembly information is not included. In other words, XML serialization does not maintain type fidelity. To maintain type fidelity use binary serialization instead.
  2. Only public properties and fields can be serialized. To serialize non-public data use binary serialization instead.
  3. A class must have a default constructor to be serialized with the XmlSerialzer class.
  4. Methods cannot be serialized.
  5. XmlSerialzer class can serialize classes that implement IColleciton and IEnumerable differently if they meet certain requirements.
Share this:
Share

C# – Rules of Private Constructor

  1.  private Constructor is used when you don’t want to create an object for the class.These are commonly used in classes that contain static members only.If a class has one or more private constructors and no public constructors, then other classes (except nested classes) are not allowed to create instances of this class.
  2. We cannot inherit from classes that are having only private constructors. The reason is suppose if you are inheriting from a base class which has only private constructors and if you create an object for the derived class then the base class constructor will be called. Because the base class contains only private constructors and  due to ‘private’ access modifier  it is not accessible from the derived  class. So it will give you an error with a syntax as follows ‘Error 1 ’ConsoleApplication2.Program.Program()’ is inaccessible due to its protection level’
  3. Where do you find Private constructors are useful?

    Private constructors can also be useful for:  classes containing only static utility methods  classes containing only constants  type safe enumerations.

Share this:
Share