07 August 2015

Struct Vs Class in C#


  1. Structs may seem similar to classes

  2. Members of a class are private by default and members of struct are public by default.

  3. When deriving a struct from a class/struct, default access-specifier for a base class/struct is public. And when deriving a class, default access specifier is private.

  4. Structures can’t have modifiers like abstract, sealed, and static whereas classes can have.

  5. Both structure and class can have partial modifiers.

  6. A structure couldn't have a destructor but in class it is possible.

  7. Classes can be inherited whereas structures not. By default structures are sealed, that is the reason structures are not supporting inheritance.

  8. You will always be dealing with instance of a class. But you can’t deal with instance of a struct (but dealing directly with them).

  9. Field/Member can't be instantiated within structures but classes allow as in the following:
    struct myStructure
    {
    string x = 2;//Not allowed
    }
    class myClass
    {
    string x = 2;//Allowed
    }


  10. Process of converting structure type into object type is called boxing and process of converting object type into structure type is called unboxing.
    Ex: int a=10;
    Object ob = (object) a; //Boxing
    a= (int) obj; //Unboxing

  11. Classes are Reference types (When you instantiate a class, it will be allocated on the heap) and Structures are Values types (lives on stack). So a class variable can be assigned null. But we cannot assign null to a struct variable.
    Static Public void Main (string [] arg) {
    MyClass obj1 =new MyClass ();
    obj1.DataMember=10;
    MyClass obj2 =obj1;
    obj2.DataMember=20;

    }
    In the above program, “MyClass obj2 =obj1” instruction indicates that both variables of type MyClass obj1 and obj2 will point to the same memory location. It basically assigns the same memory location into another variable of same type.
    So if any changes that we make in any one of the objects type MyClass will have an effect on another since both are pointing to the same memory location.
    “obj1.DataMember=10” at this line both the object’s data members will contain the value of 10. obj2.DataMember=20 at this line both the object’s data member will contains the value of 20. Eventually, we are accessing datamembers of an object through pointers.
    Unlike classes, structures are value types. For example:

    Structure MyStructure {
    Public Int DataMember; //By default, accessibility of Structure data
    //members will be private. So I am making it as
    //Public which can be accessed out side of the structure.

    }

    Static Public void Main (string [] arg){
    MyStructure obj1 =new MyStructure ();
    obj1.DataMember=10;
    MyStructure obj2 =obj1;
    obj2.DataMember=20;

    }
    In the above program, instantiating the object of MyStructure type using new operator and storing address into obj variable of type MyStructure and assigning value 10 to data member of the structure using “obj1.DataMember=10”. In the next line, I am declaring another variable obj2 of type MyStructure and assigning obj1 into that. Here .NET C# compiler creates another copy of obj1 and assigns that memory location into obj2.
    So whatever change we make on obj1 will never have an effect on another variable obj2 of type MyStructrue. So that’s why we are saying Structures are value types.

  12. All struct types implicitly inherit from the class System.ValueType

  13. In general, classes can be used when you have more complex data. And if you think that these data to be modified after creating an instance of class, then classes are absolute methods.

  14. Structs can be used for small data structures. If developer feels that data members of structure cannot be modified after creating structure, then having structure will suit.

  15. The “this” keyword gives different meaning in structure and class. How?
    In class, “this” keyword is classified as value type of class.
    In structure, “this” keyword is classified as variable type of structure.

  16. Structure must always have the default parameter-less constructor defined as public but a class might have one, so you can't define a private parameter-less constructor in structure as in the following:
    struct Me
    { private Me()// compile-time error
    { } }

    class Me
    { private Me()// runs Ok
    { } }


  17. A structure can't be abstract, a class can.

  18. Structure can inherit from interface not from any class or from other structure

No comments: