Difference between class and struct in C# .Net

1. Classes are reference types and structs are value types.
Since classes are reference type, a class variable can be assigned null.But we cannot assign null to
a struct variable, since structs are value type.
2. When you instantiate a class, it will be allocated on the heap.When you instantiate a struct, it gets created on the stack.
3. You will always be dealing with reference to an object ( instance ) of a class. But you will not be dealing with references to an instance of a struct ( but dealing directly with them ).
4. When passing a class to a method, it is passed by reference. When passing a struct to a method, it’s passed by value instead of as a reference.
5. You cannot have instance Field initializers in structs.But classes can have
example:
class MyClass
{
int myVar =10; // no syntax error.
public void MyFun( )
{
// statements
}
}
struct MyStruct
{
int myVar = 10; // syntax error.
public void MyFun( )
{
// statements
}
}
6. Classes can have explicit parameterless constructors. But structs cannot have
7. Classes must be instantiated using the new operator. But structs can be
8. Classes support inheritance.But there is no inheritance for structs.
( structs don’t support inheritance polymorphism )
9. Since struct does not support inheritance, access modifier of a member of a struct cannot be protected or protected internal.11. A class is permitted to declare a destructor.But a struct is not
12. classes are used for complex and large set data. structs are simple to use.

Advertisements

49 thoughts on “Difference between class and struct in C# .Net

  1. Nice explanation about the basic difference.
    But also tell me that when to use a structure?
    Any practical example?

    • Many times you have small classes that serves as collection of variables and you don’t have inheritance or polymorphism; in such case structs are ideal to use.

      Structs may degrade performance when you pass then to methods, particularly if they are not very small.

  2. This post is good,but please give example or more explanation for each point so that this post will become best…………….

  3. What exactly do you mean by this:
    8. Classes support inheritance.But there is no inheritance for structs.
    ( structs donโ€™t support inheritance polymorphism ) ?

    I’m asking because I found this in the .net framework
    public struct Int64 : IComparable, IFormattable, IConvertible, IComparable, IEquatable

    Nice post
    Thanks

    • Inheritance polymorphism means inherting from classes isnt possible.
      what you have mention is rite. all are interfaces and struct can inherit from interfaces but not classes..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s