What is the difference between "value types" and "Reference types"?

Asked

Viewed 199 times

2

I didn’t quite understand these classifications among the types that a variable can contain that are value types (types of value) and Reference types (reference types) that exists in the language C#, in what it says to specification:

There are two types in C#: reference types and value types. Value type variables directly contain your data as reference type variables store references to your data, the latter being known as objects. With reference types, it is possible for two variables to reference the same object and thus possible for operations in one variable to affect the object referenced by the other variable. With value types, variables have their own copy of the data, and it is not possible for operations in one to affect the other (except ref for out parameter variables and).

What do you mean? in the bold part in my opinion (if I’m wrong, correct me) is saying which variables value types are variables that have their value assigned clearly, for example:

int n1 = 1000; // value types

As variables Reference types are variables that refer to a data defined elsewhere, for example:

int n1 = 1000; // value types
int n2 = n1; // reference types

I do not know if above is relatively right, if not what are the differences between these classifications?

1 answer

4


It would be interesting to read Memory allocation in C# - Value types and reference types.

Your example is wrong, the two variables have types by value, maybe that’s why you don’t understand.

In n1 you are storing a value at a memory position, which is located on the stack and named through a variable. Then the code creates another variable called n2 and says that its value is the same as it was in n1 and therefore a copy is done (this is done with the allocation operator =). They are two completely different objects that happen to have the same value.

Think of a white car Fiat 147 year 1978 you have. I have a car with those same features but it’s not the same car.

When you create a type by reference, as the object is in another place and the variable has only one reference to it then it may be that two variables point to the same object.

var s1 = "teste";
var s2 = s1;

This case there will be only one object , which will be allocated in heap, containing a text containing the word teste and a reference to that object is stored in the variable s1.

Soon after another variable called s2 is created and through the allocation operator = a copy of the variable’s value is copied. But what is the value of the variable`S1? It is the reference for the object with the text. Then it is made an equal copy, but as the type is by reference only that, the reference, which is copied. With the copy there are two references in two different variables that happen to have the same value, and therefore point to the same single object.

If you want to have two completely different objects there you have to make a copy of the object, not just the reference. Almost always do not want this, and today there is something ready to do that is considered safe and useful (can do manually).

How do I know these variables are by reference? Because the type of the value placed on them is a type by reference. All string is by reference. You have to know what it is by looking at the documentation. When you create your types, depending on the way you declare it, it will be one way or another. And you have to learn all this, step by step.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.