Monday, March 18, 2013

Shallow Copy vs Deep Copy

Hi,

Shallow copy : is where the value type members of an object are copied to a new object where as for reference type objects the reference of the original object is stored in new object so any changes in new object will be reflected to the old object.


Deep Copy: Is where the value type & reference type members of an object are copied to a new object so any changes in the new object does not reflect to the old object.



A Deep Copier sample method which can be very useful.

 using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;    
     
/// <summary> /// Provides a method for performing a deep copy of an object. /// Binary Serialization is used to perform the copy. /// </summary>

public static class CopierFactory
{

/// <summary>
/// Perform a deep Copy of the object.
/// </summary>
/// <typeparam name="T">The type of object being copied.</typeparam>
/// <param name="sourceobject">The object instance to copy.</param>
 /// <returns>The copied object.</returns>

public static T Clone<T>(T sourceobject)
{
  if (!typeof(T).IsSerializable)
  {
    throw new ArgumentException("The type must be serializable.", "source");
  }
 
  // Don't serialize a null object, simply return the default for that object

 if (Object.ReferenceEquals(sourceobject, null))
  {
    return default(T);
  }

IFormatter formatter = new BinaryFormatter();
  Stream memorystream = new MemoryStream();
  using (memorystream )
  {
     formatter.Serialize(memorystream , sourceobject);
     memorystream.Position =0;
     return (T)formatter.Deserialize(memorystream );
  }
}
}

Static Members in Memory

Where are static members stored in .net?

They are stored in Heap, irrelevant of it being of a reference type or a value type. This heap is a special heap called "High  Frequency Heap" which is unique for each application domain.


 

Thursday, March 14, 2013

Disadvantages of Inheritence

Hi,

Till now I have never thought as Inheritence will also have some disadvantages. But I tried to find the other side of complexity of inheritance and I get to know many other details which you might be knowing but have never noticed it.

  • Inheritance results in a very tight binding between a superclass and its subclasses.

  • Removing or swapping out a superclass will usually break subclasses. Subclasses are entirely dependant on their superclass to function effectively.

  • It's inflexible. You are burdened by methods and behavior defined in a more general superclass, making down-the-line changes and customization difficult. Decisions made early on haunt the entire class hierarchy. See the fragile base class problem. [1]

  • Inheritance relationships generally can't be altered at runtime.

  • In many OOP languages, you can only inherit from a single class without introducing the problems associated with multiple inheritance. This can be restrictive. For example, if you have a Person class, with the subclasses Student and Employee. What if you have a person who is both a Student and an Employee?
[1] http://en.wikipedia.org/wiki/Fra...

Hope this little thing might have given you a chance to look at the world of oops in a different manner.