Sobes.tech
Junior — Middle

Can you explain the concept of reference data types?

sobes.tech AI

Answer from AI

Reference data types in C# are types that store not the data itself, but a reference to the memory location where the data is stored.

Main features:

  • When assigning a variable of a reference type, the reference is copied, not the object.
  • Multiple variables can refer to the same object.
  • Changes through one reference are reflected in all others.

Examples of reference types: classes, strings, arrays, delegates.

Example:

class Person { public string Name; }

Person p1 = new Person { Name = "Ivan" };
Person p2 = p1; // p2 refers to the same object
p2.Name = "Peter";
Console.WriteLine(p1.Name); // Will output "Peter"

Here, it is clear that changes through p2 affect p1, as both variables refer to the same object.

Can you explain the concept of reference data types… - sobes.tech