This website utilizes cookies to enhance user experience. Kindly provide your consent for cookie usage. Accept
How Value Types and Reference Types Work in C#?

20 October, 2024

Thank you to our sponsors who help keep this blog post free for the reader:

This month's issue is proudly sponsored by Zohirul Alam Tiemoon.

Zohirul Alam Tiemoon is a magician of OOP. He offers some excellent online courses. Whether you're a beginner or have some experience, his course will greatly benefit your programming career.

Value Type:
In C#, value types store data directly in memory. Common value types include int, float, bool, and structs. When you assign one value type to another, a copy of the value is made, meaning each variable holds its own data.

Value types copy data during assignment.
Changes to one variable do not affect the other.
Stored in stack memory, which makes access faster.

Image
Reference Type:

In C#, reference types store a reference (or memory address) to the actual data, not the data itself. Common reference types include classes, arrays, and strings. When you assign one reference type variable to another, both variables point to the same memory location.

Image
Image

Student st1 = new Student();
Here, = is the assignment operator. By st1, we create a reference type variable that will hold a value.

By new Student(), memory is allocated for a new Student object. Initially, the values in this memory will be null or 0.

Then, st1 holds the reference to this object, meaning st1 is pointing to or linking the object.

Now, what does st1 = st2 mean?

It means that now both st1 and st2 will point to the same object. When the line st1 = st2 is executed, st1 will point to the same object that st2 is pointing to. And the object that st1 was previously pointing to will become orphaned, as it no longer has a reference, and it will be garbage collected.

Therefore, the object now has two references (st1 and st2).

Remember, this is not copying. The values are not being copied. Now, if st2 is set to null, st2’s reference will become null, but st1 will not change. So, this is not copying; this is passing a reference.

Summary:

If an object has no reference, it becomes an orphan object.

Multiple references can point to the same object.

A reference can point to one object at a time or remain unreferenced, not pointing to any object.

This is how reference types work.


About the Blogs

As a dedicated .NET developer, I maintain a Patreon account where I share exclusive content related to .NET development. There, you will also gain access to the codebase of this blog post. By becoming a Patreon member, you will have the opportunity to explore and learn from my projects firsthand.

If you have found my contributions helpful in any way, I kindly ask you to consider becoming a Patreon supporter. Your support enables me to continue producing high-quality content, empowering developers like yourself to enhance their skills and stay up to date with the latest developments in the .NET ecosystem. Thank you for considering joining my Patreon community!


Recent Posts

Confidently Build Production-Ready CRUD Using N-Layer Architecture

25 February, 2024

Confidently Build Production-Ready CRUD Using Clean Architecture

25 February, 2024


Share This Article On:
An error has occurred. This application may no longer respond until reloaded. Reload 🗙