Difference between Classes and Structs

Posted on

Image result for C#Difference between Classes and Structs

1) The base class for all the classes is System.Object while structs are derived from System.ValueType
2) The class are reference type where as struct are reference types. The reference type values are stored in heap memory while the value types are stored in stack.
3) Classes are mainly used to store large amount of data while structs are used to store small data.
4) It is not possible to inherit a struct while class are inherited.
5) A Struct can’t be null while a class can.
6) A class can have a destructor while a struct can’t.
7) It is not possible to declare a struct as abstract while a class can be abstract.
8) In Struct only the following methods can be overridden which belong to the type object.
a) Equals()
                              b) GetHashCode()
                              c) GetType()
                             d) ToString()
9) In classes declared events are automatically locked and because of that they are thread safe, where as in structs the events can’t be locked.
10) A class can have a non-parameterised constructor as private while struct can’t.

struct Me
{
private Me()// compile-time error
{
}
}

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

11) A static constructor can be invoked in a class but not in the case of a structure as in the following:

struct myStructure
{
static myStructure()
{
Console.WriteLine(“This is me a structure”);
}
}
class myClass

{
static myClass()
{
Console.WriteLine(“This is me a class”);
}
}

class Program
{
static void Main(string[] args)
{
myStructure s =new myStructure();//Nothing happen
myClass c =new myClass();//Will out put This is me a class
Console.Read();
}
}

Image result for Difference between Classes and Structs

12) In a class we can declare volatile fields while in struct it is not possible.
13) It is possible to check the size of a struct using sizeof() while it is not possible for classes.
14) If no values are given to the variables declared in a class, they are automatically initialised to 0/false/null respective to there datatype whereas in structure it is not possible.
15) When we assign a value to the variabloe of struct type, it creates the copy of the value being assigned which is different from the class which copies the reference but not the object identified by the reference.
16) Both classes and structs support boxing and unboxing operation but the key differene between them is that boxing and unboxing copies the struct value either into or out of the boxed instance. Thus following a boxing or unboxing operation changes made to the unboxed struct are not reflected in the boxed struct.
17) In classes it is possible to instantiate member variables directly but in structs its not.

struct myStructure
{
publicstring x = 2;//Not allowed
}
class myClass
{
publicstring x = 2;//Allowed
}
18) The approach for using System.Object.Equals() method is different in both classes and struct.

struct StructurePerson
{
publicstring FirstName;
publicstring LastName;
}
class ClassPerson
{
publicstring FirstName;
publicstring LastName;
}

class Program
{
static void Main(string[] args)
{
StructurePerson strX =new StructurePerson();
strX.LastName = “Bejaoui”;
strX.FirstName = “Bechir”;
StructurePerson strY =new StructurePerson();
strY.LastName = “Bejaoui”;
strY.FirstName = “Bechir”;

if (strX.Equals(strY))
{
Console.WriteLine(“strX = strY”);
}
else
{
Console.WriteLine(“strX != strY”);
}//This code displays strX = strY

ClassPerson clsX =new ClassPerson();
clsX.LastName = “Bejaoui”;
clsX.FirstName = “Bechir”;
ClassPerson clsY =new ClassPerson();
clsY.LastName = “Bejaoui”;
clsY.FirstName = “Bechir”;

if (clsX.Equals(clsY))
{
Console.WriteLine(“clsX = clsY”);
}
else
{
Console.WriteLine(“clsX != clsY”);
}//This code displays clsX != clsY
Console.Read();
}
}

*****************************************************************************************************

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.
Helpline: 9814666333, 8699444666
Email: info@technocampus.co.in

Please fill the form and we shall contact you soon.

Advertisements

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 )

Google+ photo

You are commenting using your Google+ 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