Thursday, April 29, 2010

Shallow copy and deep copy in .NET

A shallow copy creates a new instance of the same type as the original object, and then copies the nonstatic fields of the original object. If the field is a value type, a bit-by-bit copy of the field is performed. If the field is a reference type, the reference is copied but the referred object is not; therefore, the reference in the original object and the reference in the clone point to the same object.
Deep copies duplicate everything. A deep copy of a collection is two collections with all of the elements in the original collection duplicated. I mean a deep copy of an object duplicates everything directly or indirectly referenced by the fields in the object.

Well, sometimes definitions bring us to a uneasy situation where we found ourselves bit confused, puzzled and baffled. So need to get this concept step by step, I’m sure by this way we will be able to elaborate it more and more.

-Consider two objects, A and B, which each refer to two separate memory blocks.




For Shallow copy
While copying A into B, we are trying to attach B to the same memory block as A.




“Be careful while getting it, as we are copying A to B, I mean neither we are trying to copy the address nor we are in the process of paasing the reference.” Simply we are in the process of copying Object A’s data into B. This results in a situation in which some data is shared between A and B.

Thus modifying the one will alter the other. The original memory block of B is now no longer referred to from anywhere.



The advantage of shallow copies is that their execution speed is fast and does not depend on the size of the data.
For deep copy
In deep copy process as the data is actually copied over. So,




The result will have, A with original data in separate memory block (earlier) and B will have A’s duplicate data in separate memory block.



The advantage is that A and B do not depend's on each other but at the cost of a slower and more expensive copy.
Shallow Vs Deep Copy : Now for the implementation, let’s create a console application “ShallowVsDeepCopyPrj” and create two separate classes Product and ProductCatalog within that application, in both classes I have implemented ICloneable interface.

public class Product:ICloneable {

                  private String m_ProductName,m_ProductDesc;
                  private String prodName = "ABC of C#";
                  private ProductCatalog m_ProductCatalog = new ProductCatalog("ABC of C#", 
                  "AVAILABLE");

                  public Product() { this.Setm_ProductDesc="Test Your C# Skill";

                 this.Setm_ProductName = prodName; }
                 public String Setm_ProductDesc { set { m_ProductDesc = value; } get { return
                 m_ProductDesc; } }
                 public String Setm_ProductName { set { m_ProductName = value; } get { return
                 m_ProductName; } }
                public ProductCatalog Setm_ProductCatalog { set { m_ProductCatalog = value; } get { return  
                m_ProductCatalog; } }
                Object ICloneable.Clone() { return this.Clone(); }

                public virtual Product Clone() {
                          // Start with a flat, memberwise copy
                         Product shallowCopy_Prod = this.MemberwiseClone() as Product;
                         // Then shallow-copy everything that needs the- pecial attention
                         shallowCopy_Prod.Setm_ProductName = this.Setm_ProductName;
                         shallowCopy_Prod.Setm_ProductDesc = this.Setm_ProductDesc;
                         shallowCopy_Prod.Setm_ProductCatalog = this.Setm_ProductCatalog;
                         return shallowCopy_Prod; }         }

public class ProductCatalog : ICloneable {
                        private String m_ProductName, m_ProductStatus;
                        public ProductCatalog(string pName,string pCat) {
                                  this.Setm_ProductName = pName;
                                  this.Setm_ProductStatus = pCat; }

                       public String Setm_ProductStatus { set { m_ProductStatus = value; } get { return
                       m_ProductStatus; } }
                       public String Setm_ProductName { set { m_ProductName = value; } get { return
                       m_ProductName; } }

                      Object ICloneable.Clone() { return this.Clone(); }
                     
                      public virtual ProductCatalog Clone() {
                      // Start with a flat, memberwise copy
                     ProductCatalog deepCopy_ProdCat = this.MemberwiseClone() as ProductCatalog;
                     return deepCopy_ProdCat;     }
    }
-Now implementing these two class in main method like this

Product m_Product = new Product();  // Starts

try {
         Product cloneProd = (Product)m_Product.Clone();
         m_Product.Setm_ProductCatalog.Setm_ProductName = "Know The Facts - FIJ";
         m_Product.Setm_ProductCatalog.Setm_ProductStatus = "NOT AVAILABLE";

        Console.WriteLine("Source - Object A --" + m_Product.Setm_ProductName +
                      "--" + m_Product.Setm_ProductDesc + "--" +
                      m_Product.Setm_ProductCatalog.Setm_ProductName + "--" +
                      m_Product.Setm_ProductCatalog.Setm_ProductStatus);

       Console.WriteLine("Destination - Object B --" +
                    cloneProd.Setm_ProductName + "--" + cloneProd.Setm_ProductDesc + "--" +
                    cloneProd.Setm_ProductCatalog.Setm_ProductName + "--" +
                    cloneProd.Setm_ProductCatalog.Setm_ProductStatus);

} catch (Exception ex) {
                   Console.WriteLine(ex.Message.ToString()); }
Console.ReadLine();    //Ends
Will bring you output:
Source-Object A --ABC of C#--Test Your C# Skill--Know The Facts - FIJ--NOT AVAILABLE
Destination-Object B --ABC of C#--Test Your C# Skill--Know The Facts - FIJ--NOT AVAILABLE

As we tried to copy the object A’s data into object B by shallow copy process. It means change in any object will be reflected both places, and the same thing you can see in text “Know The Facts - FIJ--NOT AVAILABLE” part in source and Destination object.Now for deep copy approach simply we need to replace public virtual Product Clone() method by
public virtual Product Clone() {

          // Start with a flat, memberwise copy
          Product deepCopy_Prod = this.MemberwiseClone() as Product;
          // Then deep-copy everything that needs the - special attention
         deepCopy_Prod.Setm_ProductName = this.Setm_ProductName;
         deepCopy_Prod.Setm_ProductDesc = this.Setm_ProductDesc;
         deepCopy_Prod.Setm_ProductCatalog = this.Setm_ProductCatalog.Clone();
         return deepCopy_Prod; }

And after executing modified code, Output will be
Source-Object A--ABC of C#--Test Your C# Skill--Know The Facts - FIJ--NOT AVAILABLE
Destination - Object B --ABC of C#--Test Your C# Skill--ABC of C#--AVAILABLE

By analysing red font we can say that change's in one object will not alter another one as it is a deep copy.
Well,It was a simple story about shallow and deep copy.......enjoy coding

Friday, April 23, 2010

Overriding – ToString () – A Simplest approach to understand it

In C# Overriding can be achieved by use of the "override" keyword. To override a method means to replace it with a new way of handling data; I mean replacing the default behavior or extending it.

Here's an example of what I mean:
public class SimpleCar
 {
      int vwidth, vheight; //Private Data Member
      public int cWidth {  set { vwidth = value; } get { return vwidth; } }
      public int cHeight { set { vheight = value; } get { return vheight; } }
      public int cArea {  get { return (vwidth * vheight); }}
     //Not Overrided built-in ToString() method
 }
Moving forward with overriding,We will take help of “ToString()” method.As we know ToString is a built-in method that returns the name of the type of the object(in case of custom type).

Have a look at the Main() method which is used to call SimpleCar-
   namespace ConsoleApplication1 {
      class Program {
            static void Main(string[] args) {
                    SimpleCar mySimpleCar1 = new SimpleCar();
                    mySimpleCar1.cWidth = 13;
                    mySimpleCar1.cHeight = 15;

                   Console.WriteLine("The area occupied by {0}, width={1} and height={2} is: 
                   {3}.", mySimpleCar1, mySimpleCar1.cWidth, mySimpleCar1.cHeight,  mySimpleCar1.cArea);

                   Console.ReadLine(); }
       }
   }

Exact output will be:
The area occupied by ConsoleApplication1.SimpleCar, width=13 and height=15 is: 195.

*Here ‘ConsoleApplication1.SimpleCar’ name comes through the help of ToString() method, As I explained earlier. Now we will simply override built-in ToString() method in our code and then we’ll see what we get.

public class SimpleCar {
       int vwidth, vheight; //Private Data Member
       public int cWidth { set { vwidth = value; } get { return vwidth; } }
       public int cHeight { set { vheight = value; } get { return vheight; } }
       public int cArea { get { return (vwidth * vheight); } }

       //Overrided built-in ToString() method
       //Here I have provided an alias for the class(-type-)
       public override string ToString()  { return "Overrided SimpleCar"; }
}

The only change between this method and any other method is the use of the ‘override’ keyword. It is as simple as earlier. Now when the same Main() method is executed, the output will be:

The area occupied by Overrided SimpleCar, width=13 and height=15 is: 195.

I hope you get the reason, How ‘Overrided SimpleCar’ came into the picture.By overriding – I mean we have replaced the default behavior of built-in ToString () method.
-As of now we get bit understanding of overriding; now we need to delve into overloading and overriding together.
-
*Hasbro Power Tour Electric Guitar (Black)

Thursday, April 22, 2010

Abstraction vs Encapsulation


Abstraction is the process of taking a concept, identifying the attributes and behaviour and being able to turn that concept into code(Class) such that you will get an abstract view of the concept while Encapsulation is the process of organizing the required data types, methods to access them and defines a way to communicate through outer world, which mean's to get the job done.

In Object oriented programming abstraction is extremely important as by working more abstractly we can solve more and more difficult problems because we don’t have to concentrate on the lower level details.
  • “In another term we can say Abstraction comes first, Encapsulation satisfies what abstraction promise to do so.”
-Abstraction ask's to concentrate on “What” system does and encapsulation states “How” system does.


Elaborating topic more, For Abstraction we can say if we are not willing to know the cumbersome detailed information (because it might be too complex...) and willing to consider only meaningful data! Which help’s to "simplify" complex task and get clear arrangement/management.

While encapsulation shakes hands with if we didn’t want to let others access some important data and hence we put them within class and declare using access specifier. As a result, only the member functions can access them.

-Here I left one topic untouched i.e. information hiding, I will try to relate information hiding with this topic in next post.
-
*ASUS UL30A-X5 Thin and Light 13.3-Inch Black Laptop (12 Hours of Battery Life)