Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Class Objects vs Struct Objects | Introduction to Object-Oriented Programming (OOP)
C# Beyond Basics

book
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:

cs

index

copy
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}");
}
}
123456789101112131415161718192021222324252627
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:

cs

index

copy
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}");
}
}
123456789101112131415161718192021222324252627
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:

cs

index

copy
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}");
}
}
12345678910111213141516171819202122232425
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:

cs

index

copy
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}");
}
}
12345678910111213141516171819202122232425
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}"); } }

1. Class objects are:

2. The code below demonstrates the usage of class objects with lists and dictionaries. The code is explained in the comments. Read the code and choose the option which shows the correct output. The code might be lengthy, but it is a good code reading exercise.

question mark

Class objects are:

Select the correct answer

question mark

The code below demonstrates the usage of class objects with lists and dictionaries. The code is explained in the comments. Read the code and choose the option which shows the correct output. The code might be lengthy, but it is a good code reading exercise.

using System;
using System.Collections.Generic;

class Player {
public int highscore;
}

public class ConsoleApp
{
public static void Main(string[] args)
{
// Creating a list of Player objects.
List<Player> players = new List<Player>();
// Creating an empty variable to temporarily store objects
Player tmp;
// Creating a new object and storing it into temp
tmp = new Player();
tmp.highscore = 1;
// Adding the temp object into the list.
players.Add(tmp);
// Repeating the process for two more objects.
tmp = new Player();
tmp.highscore = 64;
players.Add(tmp);
tmp = new Player();
tmp.highscore = 97;
players.Add(tmp);
// Two empty objects to store the best and the worst player.
// Note that these objects are not initiated yet.
Player bestPlayer;

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 5
We use cookies to make your experience better!
some-alt