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

Conteúdo do Curso

C# Beyond Basics

C# Beyond Basics

1. Additional Structures & File Handling
2. Structs & Enumerators
3. Introduction to Object-Oriented Programming (OOP)
4. OOP Essentials
5. OOP Principles

bookClass 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
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
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
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
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.
Class objects are:

Class objects are:

Selecione a resposta correta

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.

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.

Selecione a resposta correta

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 5
We're sorry to hear that something went wrong. What happened?
some-alt