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.

About Md Shaik Sadi

I am Md. Shaik Sadi, got my B.Sc.(Engg) degree in Computer Science and Engineering and started my carrer as a trainee developer. I have worked on different phases of software development life cycle. Recently, I am working on asp.net/2 and want to stablish my career on this platfrom.
This entry was posted in Computer Science and tagged , , , . Bookmark the permalink.

35 Responses to Difference between class and struct in C# .Net

  1. shiman says:

    This post is a real good one, man…

  2. prasad says:

    hi

    u r answre is nice becouse u r thinkig indepth yhats good

  3. srikanth says:

    Thanks for the post…..
    It is really helpful for me………..

  4. Mark61 says:

    Exterior lights and hallway lights are usually on timers and turn themselves off after 20 seconds or so. ,

  5. Tarun says:

    One more thing I would like to add that is GC. due to no allocation from heap GC will not work on Struct

  6. Suresh says:

    This article is too good man.

  7. Tilahun says:

    Really good answer. Thanks a lot.

  8. Arly says:

    Thanks for this info, this is just like what I need.

  9. Charles says:

    Really awesome explanation.Expecting d same in future.

  10. Ramakant says:

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

    • prasanna says:

      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.

  11. venika says:

    Nice post

  12. praveen says:

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

  13. nag says:

    Explanation is so far good please give Examples in natural way

  14. It is very knowledge for me………………
    Be continue with these kinds of knowledge

  15. satvir says:

    your answer are so nice. I exactly found what I want

  16. avee says:

    its good for me because it solve my all problem regarding struct and class

  17. ISLAM is true says:

    i am a beginer and ur post helped me to understand the difference even better

  18. Mohan Prakash says:

    Your answer is good .

  19. Vells Dicosta says:

    Thanks a lot…
    It’s very helpfull for me for my college assignment;-)….

  20. Sapna Jain says:

    This is my expected answer.its really good

  21. Siva says:

    Ur ans is good.. Can help with some examples

  22. poo says:

    Was really helpful.. Thanks:)

  23. Nermeen says:

    thankssssssssssssssssss it was helpful.

  24. PDK says:

    very helpful.. thanks :)

  25. err says:

    Thank U. It was very important tome because it is brief & understanderble.

  26. Sankar.V says:

    Thannks….

  27. satya says:

    Thanks a lot.This is very helpful for me. Keep on answering this kind of enlightening us about this kind of questions.

  28. Rahul says:

    gud explanation

  29. krupalini says:

    realy it’s nice description,thanks

  30. Shamim says:

    Thanks for writing such useful things which is very useful for new comer in development field

  31. wajeeda unnisa says:

    thanks 4 writing such useful things but explain each point in detail that to learn more depth :-)

  32. prasanna says:

    Nice post. I think all points are covered in it.

  33. vic_ch says:

    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

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 )

Connecting to %s