Structures Versus Classes
So what’s the difference between a class and a structure? Structures are value types like int, double, and string. When you copy a value of structures to other variables, it copies its values to the other variable and not its address or reference. Classes are reference types like all the classes in the .NET Framework. Let’s demonstrate the difference of the two by looking at an example.
using System;
namespace StructureVsClass
{
struct MyStructure
{
public string Messageone { get; set; }
}
class MyClass
{
public string Messageone { get; set; }
}
class Program
{
static void Main()
{
MyStructure structure1 = new MyStructure();
MyStructure structure2 = new MyStructure();
structure1.Messageone = "ABC";
structure2 = structure1;
Console.WriteLine("Showing that structure1 " +
"was copied to structure2.");
Console.WriteLine("structure2.Messageone = {0}", structure2.Messageone);
Console.WriteLine("
Modifying the value of structure2.Messageone...");
structure2.Messageone = "123";
Console.WriteLine("
Showing that structure1 was not affected " +
"by the modification of structure2");
Console.WriteLine("structure1.Messageone = {0}", structure1.Messageone);
MyClass class1 = new MyClass();
MyClass class2 = new MyClass();
class1.Messageone = "ABC";
class2 = class1;
Console.WriteLine("
Showing that class1 " +
"was copied to class2.");
Console.WriteLine("class2.Messageone = {0}", class2.Messageone);
Console.WriteLine("
Modifying the value of class2.Messageone...");
class2.Messageone = "123";
Console.WriteLine("
Showing that class1 was also affected " +
"by the modification of class2");
Console.WriteLine("class1.Messageone = {0}", class1.Messageone);
}
}
}
Example 1 – Structures Vs Classes
Showing that structure1 was copied to structure2. structure2.Message = ABC Modifying the value of structure2.Message... Showing that structure1 was not affected by the modification of structure2 structure1.Message = ABC Showing that class1 was copied to class2. class2.Message = ABC Modifying the value of class2.Message... Showing that class1 was also affected by the modification of class2 class1.Message = 123
We made a structure and a class that we will use to demonstrate their differences. We provided a Message property for each of the two. We then create two instances for each of them. We assigned a value for the Message property of structure1. We then assign the value of structure1 to structure2 so everything inside structure1 will be copied to structure2. To prove that everything was copied from structure1, we showed the value of structure2‘s Message property and you can see that it is the same as the structure1‘s Message property value. To show that structures are value types, we assigned another message to the Message property of structure2. The Message property of structure1 is not affected because the structure2 is a separate copy of structure1.
Now to demonstrate the behavior of reference types and classes. Classes pass their address and not their value when they’re being assigned to other variables. Therefore, when you edit a property of the object that receive the address of the original object, the property of that original object is also modified. When you’re passing an object as an argument to a method, only the address of that object is passed. Any modifications to that object inside that method will also reflect the original object that passed its address.