Blog for Junior Developers C#/.NET

Sunday, October 20, 2024

In C#, we deal with two types, namely the value type, also known as the simple type, and the reference type. It is necessary to understand what these types really are, how to use them, and what are the differences between them. In this article, I will introduce you to this topic, because understanding how the value type and reference type work is very important. You absolutely must know them if you want to become a .NET Developer.

value-type-and-reference-type-in-csharp.png

Value type (simple)


A value type is an element that extends System.ValueType and is a simple type such as int, long, decimal, double, bool, etc.

These are types with a specific size in memory. Declaring a variable immediately creates a specific size of memory for it. They are stored on the stack. See an example:

var minNumber = 10;
var maxNumber = 20;
minNumber = maxNumber;
maxNumber = 21;
Console.WriteLine(minNumber);

Console.ReadLine();


Now consider for a moment what result will be displayed on the console. Let's run the applications. As expected, the minNumber variable has the value 20. By assigning the value of the maxNumber variable to minNumber, we actually copied what is on the stack, i.e. the value. Therefore, if the value of the maxNumber variable was changed in the next step, the minNumber variable remained unchanged. Such an operation looks a bit different when we operate on reference types.



Reference type


A reference type is an element inheriting from the System.Object and System.String classes, i.e. mainly classes, lists, arrays, or strings.

A reference to memory is placed on the stack, but the memory area to which the reference leads is on the heap. This means that its size in memory is not fixed at the time of declaration. There is only a reference to a specific location. They are removed from memory using the Garbage Collector. Let's consider the example of copying values ​​again, this time in the case of reference types.

var employee1 = new Employee { FirstName = "1" };
var employee2 = new Employee { FirstName = "2" };
employee1 = employee2;
employee2.FirstName = "3";
Console.WriteLine(employee1.FirstName);


Please run the application and verify the result. This time the result may not be so obvious to you, but the screen will display 3.

As you can see in the case of reference types, the object reference was copied during the assignment and both objects now point to the same reference, i.e. they point to the same place in memory. Hence in this case the result is 3, even though the value of the FirstName property of the employee1 object was not explicitly changed, but the value of the reference was changed via employee2.


Default values


As for default values, in the case of reference types it is null, and in the case of value types, for numbers it is 0, for bool false.

Value types can also contain nulls, but if they are created as a so-called nullable type. That is, if you do something like this:

int number = null;


This will give you an error, because int cannot be null. However, if you create int as a nullable type, then you can assign null to a variable of this type.

int? number = null;


Similarly, you can create a nullable type for bool, decimal, double, long and other value types.


SUMMARY


This is what a value and reference type looks like in C#. You can try to create a few additional examples related to using these types, so as to consolidate this knowledge, because as I mentioned at the beginning, it will be crucial in further learning programming.

If you liked this article, be sure to join my community. Sign up for the free newsletter, where every week I share valuable materials, especially regarding C# and the .NET platform (free subscription - newsletter).

Author of the article:
Kazimierz Szpin

KAZIMIERZ SZPIN
Software Developer C#/.NET, Freelancer. Specializes in ASP.NET Core, ASP.NET MVC, ASP.NET Web API, Blazor, WPF and Windows Forms.
Author of the blog CodeWithKazik.com

Previous article - 16 Common Beginner Programmer Mistakes You Need to Avoid
Next article - What is VAR Implicit Type in C#?
Dodaj komentarz

Search engine

© Copyright 2024 CodeWithKazik.com. All rights reserved. Privacy policy.
Design by Code With Kazik and Modest Programmer.