Course Content
C# Beyond Basics
C# Beyond Basics
Class Objects vs Struct Objects
Class objects can also be stored in Arrays, Dictionaries and Lists just like structs however it is important to note that class objects are passed by reference while struct objects are passed by value.
This concept is important because passing simple values like int
, float
or double
into methods is different from passing complex objects and structures into methods. Let's look at an example to understand this concept better.
We will create a simple struct object and pass it into a method. We will change its value from inside the method and see what happens to the value of the original object:
index
using System; // Defining a simple structure struct Example { public int value; } public class ConsoleApp { static void exampleMethod(Example passedObject) { passedObject.value += 10; Console.WriteLine($"passedObject: {passedObject.value}"); } public static void Main(string[] args) { // Creating and initialising an object Example originalObject = new Example(); originalObject.value = 7; // Passing that object into a method exampleMethod(originalObject); // Checking the value of the originalObject Console.WriteLine($"originalObject: {originalObject.value}"); } }
The output shows that the value of passedObject
was updated while the value of originalObject
remained unchanged. This shows that when we pass a struct object into a method, a duplicate of that object is created and passed into the method, or in other words, it is passed by value.
Now let's use the exact same code, only changing the term struct
to class
:
index
using System; // Defining a simple class class Example { public int value; } public class ConsoleApp { static void exampleMethod(Example passedObject) { passedObject.value += 10; Console.WriteLine($"passedObject: {passedObject.value}"); } public static void Main(string[] args) { // Creating and initialising an object Example originalObject = new Example(); originalObject.value = 7; // Passing that object into a method exampleMethod(originalObject); // Checking the value of the originalObject Console.WriteLine($"originalObject: {originalObject.value}"); } }
You'll notice that the output has changed - both the passedObject
and the originalObject
have the same value this time. This proves that when we pass a class object into a method, it passes a reference to the original object into the method, making changes to passedObject
changes originalObject
.
This property also applies when assigning objects to variables. Following is an example that shows the behavior of a struct object:
index
using System; struct Num { public int value; } public class ConsoleApp { public static void Main(string[] args) { // Creating and initiating 'a' Num a = new Num(); a.value = 7; // Creating 'b' and setting it equal to a. Num b = a; Console.WriteLine($"a is {a.value} and b is {b.value}"); // Now we change the value of 'b' to something else b.value = 9; Console.WriteLine($"a is {a.value} and b is {b.value}"); } }
The same code but this time for a class object:
index
using System; class Num { public int value; } public class ConsoleApp { public static void Main(string[] args) { // Creating and initiating 'a' Num a = new Num(); a.value = 7; // Creating 'b' and setting it equal to a. Num b; b = a; Console.WriteLine($"a is {a.value} and b is {b.value}"); // Now we change the value of 'b' to something else b.value = 9; Console.WriteLine($"a is {a.value} and b is {b.value}"); } }
Thanks for your feedback!