c# – How do pointers in C differ from references in C #?

Question:

Is it true that pointers in C are analogous to reference types in C #?

Answer:

Conceptually, yes. They solve the problem of pointing to an object. But there are differences in implementation.

Pointers contain the address. Links are, in fact, an abstraction that says nothing about their meaning (although in fact, the CLR also puts an address in them).

A reference in C # always refers to a valid object or is equal to null , while a pointer can point to a random memory area. (thanks for @VladD comment)

In addition, C pointers are "static", and the object in the dotnet can be moved by the garbage collector, i.e. links change their meaning.

Another difference follows from this fact: in the case of pointers, it is possible to safely do arithmetic (for example, refer to the i-th element of an array, having a pointer to the array and knowing the size of the element), in the case of references, not.

Pointers also exist in C #. They are equivalent to pointers in C.

Scroll to Top