Wednesday, 16 July 2014

Difference between structure and classes in c#.



Class:
A class is a reference type. When an object of the class is created, the variable to which the object is assigned holds only a reference to that memory. When the object reference is assigned to a new variable, the new variable refers to the original object. Changes made through one variable are reflected in the other variable because they both refer to the same data.

Declaration of Class


<access specifier> class  class_name
{
    // member variables
    <access specifier> <data type> variable1;
    <access specifier> <data type> variable2;
    ...
    <access specifier> <data type> variableN;
    // member methods
    <access specifier> <return type> method1(parameter_list)
    {
        // method body
    }
    <access specifier> <return type> method2(parameter_list)
    {
        // method body
    }
    ...
    <access specifier> <return type> methodN(parameter_list)
    {
        // method body
    }
}


























Example:


class ExampleClass
{
    public int[] _value1;
    public int[] _value2;
    public ExampleClass(int[] value1, int[] value2)
    {
     this._value1 = value1;
     this._value2 = value2;
    }
}


Structure:

A struct is a value type. When a struct is created, the variable to which the struct is assigned holds the struct's actual data. When the struct is assigned to a new variable, it is copied. The new variable and the original variable therefore contain two separate copies of the same data. Changes made to one copy do not affect the other copy.

Declaration of Struct



public struct MyStruct {
    // variables
}





Example: 

public struct Book
{
    public decimal price;
    public string title;
    public string author;
}
 

Differences between the Struct and Class:


The struct is value type in C# and it inherits from System.ValueType.
The class is reference type in C# and it inherits from the System.Object Type.
The struct value will be stored on the stack memory.
The class object is stored on the heap memory. The object will be under garbage collection and automatically removed when there is no reference to the created objects.
The struct use the array type and it’s good to use for read only and light weight object.
The class uses the collection object type and it can perform all the operations and designed for complex data type storage.
The struct can't be base type to the classes and also to the other structure.
The class can inherit another class, interface and it can be base class to another class.
The struct can only inherit the interfaces.
The class can inherit the interfaces, abstract classes.
The struct can have only constructor.
The class can have the constructor and destructor.
The struct can instantiated without using the new keyword.
The new keyword should be used to create the object for the class.
The struct can't have the default constructor.
The class will have the default constructor.
The struct is by default sealed class hence it will not allow to inherit. It can't use the abstract, sealed, base keyword.
The class can be declared as abstract, sealed class.
The struct can't use the protected or protected internal modifier.
The class can use all the access modifiers.
The struct can't initialize at the time of declaration.
The class can have the initializes fields.

No comments:

Post a Comment