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.
-
Recent Posts
-
Recent Comments
- Adesh Rajput on Difference between Abstract class and Interface in C# .Net
- Adesh Rajput on Difference between Abstract class and Interface in C# .Net
- Kwex on Log4Net Tutorial in C# .net (How can I show log in a file?)
- bhavya on Log4Net Tutorial in C# .net (How can I show log in a file?)
- Varun Aggarwal on ADO.NET – DataReader(Connected) and DataSet(Disconnected) in C# .Net with Examples
-
Authors
This post is a real good one, man…
hi
u r answre is nice becouse u r thinkig indepth yhats good
haram khor saale kuch bhi samajh nhi aya………………………tujhe kaise aa gya…??????/
Thanks for the post…..
It is really helpful for me………..
Exterior lights and hallway lights are usually on timers and turn themselves off after 20 seconds or so. ,
One more thing I would like to add that is GC. due to no allocation from heap GC will not work on Struct
This article is too good man.
Really good answer. Thanks a lot.
Thanks for this info, this is just like what I need.
Really awesome explanation.Expecting d same in future.
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.
Nice post
This post is good,but please give example or more explanation for each point so that this post will become best…………….
Explanation is so far good please give Examples in natural way
It is very knowledge for me………………
Be continue with these kinds of knowledge
your answer are so nice. I exactly found what I want
its good for me because it solve my all problem regarding struct and class
i am a beginer and ur post helped me to understand the difference even better
Your answer is good .
Thanks a lot…
It’s very helpfull for me for my college assignment;-)….
This is my expected answer.its really good
Ur ans is good.. Can help with some examples
Was really helpful.. Thanks:)
thankssssssssssssssssss it was helpful.
very helpful.. thanks
Thank U. It was very important tome because it is brief & understanderble.
Thannks….
Thanks a lot.This is very helpful for me. Keep on answering this kind of enlightening us about this kind of questions.
gud explanation
realy it’s nice description,thanks
Thanks for writing such useful things which is very useful for new comer in development field
thanks 4 writing such useful things but explain each point in detail that to learn more depth
Nice post. I think all points are covered in it.
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