Sobes.tech
Junior — Middle

Millised viidetüübid on C# keeles rakendatud?

sobes.tech AI

Vastus AI-lt

In C#, reference types are types that are stored in the managed heap, and variables of these types contain a reference to the object, not the object itself. The main reference types in C#:

  • class — classes, for example string, user-defined classes.
  • interface — interfaces.
  • delegate — delegates, which represent methods.
  • array — arrays, for example int[].
  • string — string, although it looks like a primitive, it is a reference type.

Example:

class Person {
    public string Name;
}

Person p1 = new Person();
p1.Name = "Иван";
Person p2 = p1; // p2 and p1 refer to the same object
p2.Name = "Пётр";
Console.WriteLine(p1.Name); // Will output "Пётр"

Here, it is visible that when assigning variables of reference types, only the reference is copied, not the object itself.