How are two lines with the same value in it stored in memory?

string first="Example";
string second="Example";

Both lines have the same hash code, and both links are the same.

My question is: do these row variables have one copy of the data in the memory location?

string third="Example";
static string fourth="Example";
const string fifth="Example";

Is there a difference between the two above sets?

+4
source share
1 answer

In this particular example, all lines are different links with the same value, with one object string. This is because the ldstrIL command puts literal strings and reuses them, rather than allocating them for ldstr.

You can check this with ReferenceEquals(first, second), etc.

, const , ldstr -, , .

: , "Example" . ( ) , .

+4

Source: https://habr.com/ru/post/1648646/


All Articles