Junior — Middle
What reference data types are implemented in C#?
sobes.tech AI
Answer from AI
In C#, reference data types are types that are stored in the managed heap, and variables of such types contain a reference to an object, not the object itself. The main reference types in C#:
- class — classes, for example
string, user-defined classes. - interface — interfaces.
- delegate — delegates that represent methods.
- array — arrays, for example
int[]. - string — a string, although it looks like a primitive, it is a reference type.
Example:
class Person {
public string Name;
}
Person p1 = new Person();
p1.Name = "Ivan";
Person p2 = p1; // p2 and p1 refer to the same object
p2.Name = "Peter";
Console.WriteLine(p1.Name); // Will output "Peter"
It is evident that when assigning reference type variables, the reference is copied, not the object itself.