Difference Between Struct and Class in C#
In this tutorial, we will discuss the differences and similarities between structures and classes in C#.
Structure in C#
The struct
keyword defines a structure in C#. Similar to a class, a structure can have variables, properties, and functions in it. We can define constructors for structures in C#. But, we cannot define default constructors for structures in C#. We cannot define destructors for structures in C#. Structures can also implement interfaces in C#. The main difference between structures and classes is that we cannot use a structure as the base of other structures or classes for inheritance. The following code example shows us how to define a simple structure using the struct
keyword in C#.
struct MyStructure {
public int x;
};
In the above code, we define a structure with the struct
keyword in C#. Structures can be used by creating an instance. The code for creating an instance of a structure is given below.
MyStructure S = new MyStructure();
In the above code, we created an instance of the structure MyStructure
with the new
keyword in C#.
Class in C#
The class
keyword defines a class in C#. Similar to a structure, a class can have variables, properties, and functions in C#. We can also define default constructors and destructors for classes in C#. A key feature of classes is inheritance. So, a class can be used as the base of other classes for inheritance. Another unique feature of classes is polymorphism. The following code example shows us how to define a simple class using the class
keyword in C#.
class MyClass {
int x;
public MyClass(int a) {
this.x = a;
}
public void setx(int a) {
this.x = a;
}
public void show() {
Console.WriteLine("The value of x = {0}", x);
}
}
In the above code, we define a class with the class
keyword in C#. Classes can be used by creating an instance. The code for creating an instance of a class is given below.
MyClass C = new MyClass(100);
In the above code, we created an instance of the class MyClass
with the new
keyword in C#.
Difference Between Structure and Class in C#
The structures are value-type variables and live in the stack. The classes are reference-type variables and live in the heap with reference to that heap inside the stack.
The structures cannot have null values, whereas the classes can have null values. It means that a value-type variable must have a value, whereas a reference-type variable might or might not point to a memory address. The difference between structures and classes in C# is demonstrated in the coding example below.
MyStructure S = new MyStructure();
MyStructure S2 = S;
S.x = 100;
S2.x = 200;
Console.WriteLine("Structure 1 = {0}", S.x);
Console.WriteLine("Structure 2 = {0}", S2.x);
Output:
Structure 1 = 100
Structure 2 = 200
We first created an instance S
of the structure MyStructure
and then created another copy of the same instance - S2
. After that, we initialized the values inside both instances.
In the output, we get two different values for two different copies of the instance. This means that there are two separate copies of the MyStructure
structure inside the stack. Copying a value-type variable into another variable creates a separate copy of all the contents of the value-type variable.
In the following code example, we will do the same thing with the MyClass
class.
MyClass C = new MyClass(100);
MyClass newC = C;
newC.setx(100);
newC.setx(200);
C.show();
newC.show();
Output:
The value of x = 200
The value of x = 200
We first created an instance C
of the class MyClass
and then copied that instance into another instance, newC
, of the MyClass
class. After that, we set the value of variable x
inside both instances with the setx()
function.
In the output, we get the same values of the variable x
inside both instances. This means that both C
and newC
instances point to the same memory location inside the heap. It only copies the reference of the reference-type variable when copying a reference-type variable into another variable.
Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.
LinkedIn