C# - Value Types Versus Reference Types

Search for a command to run...

No comments yet. Be the first to comment.
Access modifiers determine the accessibility of a type or type member, indicating which parts of a C# program can access those types and their members. By protecting types and members from unintended access, access modifiers contribute to achieving e...
ASP.NET Core Configuration Guide Overview ASP.NET Core uses a layered configuration system where later sources override earlier ones for the same key. Configuration Priority Order Priority Source
Legacy Delphi applications often struggle with outdated UI/UX, performance issues, and monolithic architectures that make refactoring difficult. However, a full rewrite is not always feasible due to time, cost, and the risk of disrupting critical bus...
Entity Framework Core (EF Core) is a powerful and flexible object-relational mapping (ORM) framework for .NET applications, enabling developers to work with relational databases using strongly-typed .NET objects. It simplifies data access by abstract...

In this article we are going to cover some advance scenarios where Options pattern is very useful. We have covered the basic understanding of Options pattern into Options pattern in ASP.NET Core: Introduction article. Please go through it if not fami...

In this article we will learn about Options pattern in ASP.NET Core for dealing with application configurations. The Options pattern uses the C# classes to access and manage the configurations thus providing more flexibility and type safe approach to...

C# types can be divided mainly into two categories: value types and reference types. In this article, we would be having a closer look at these two types.
Most of the c# built-in types such as numeric types, char types and boolean types are value types.
custom structs and enum types are also value types.
content of a value-type variable or constant is simply a value.
The assignment operator always copies the value.
int a = 10;
int b = 7;
b = a; // copies the value of a into b
Console.WriteLine(" a = {0}, b = {1}", a, b); // a = 10, b = 10
a = 15; // b won't change
Console.WriteLine(" a = {0}, b = {1}", a, b); // a = 15, b = 10
custom value types
custom value types can be created using struct or enum .
public struct Point { public int X; public int Y; }
Point p1 = new Point();
p1.X = 7;
Point p2 = p1; // Assignment causes copy
Console.WriteLine (p1.X); // 7
Console.WriteLine (p2.X); // 7
p1.X = 9; // Change p1.X
Console.WriteLine (p1.X); // 9
Console.WriteLine (p2.X); // 7
A reference type has two parts: an object and the reference to that object.
content of a reference-type variable or constant is a reference to an object that contains the value.
a class type, an interface type, an array type, a delegate type, or the dynamic type are all reference types.
public class Point { public int X, Y; }
Point p1 = new Point();
p1.X = 7;
Point p2 = p1; // Copies p1 reference
Console.WriteLine (p1.X); // 7
Console.WriteLine (p2.X); // 7
p1.X = 9; // Change p1.X
Console.WriteLine (p1.X); // 9
Console.WriteLine (p2.X); // 9
null can be assigned to reference types without any issue. assigning null to a reference type indicates that the reference points at nothing.
a value type cannot have null [Except Nullable Value types ]
int x = null // compile time errorValue types occupy the memory required to store the field for example 4 bytes for an integer field. However, since the reference type has two parts, it requires separate allocations for reference and object.That's it for this article. Thanks for reading it. Please share your thoughts into comment section.