C #: allocating memory for anonymous variables

I have doubts about the distribution of memmory for an anonymous type variable.

if I declare a variable int Vaiable_Name, it will highlight4 bytes

but in the case of Anonymous typeswhat will happen, and when will the memory be freed?

Does it need to be freed manually?

eg

List<String> MyList=new List<String>{"0","1","0"}.Where(X=>X!="1").ToList();

There byteswill be allocated X?

+3
source share
2 answers

You did not show any anonymous types. You showed a lambda expression. In this case, the compiler will effectively create an additional method for you, for example:

private static bool SomeUnspeakableName(string X)
{
    return X != "1";
}

Then your code will be translated into this, effectively:

List<String> MyList=new List<String>{"0","1","0"}
       .Where(new Func<string, bool>(SomeUnspeakableName))
       .ToList();

... , . (, , Enumerable.Where Enumerable.ToList.)

, X . , - ( ). .

, , :

var anon = new { Name = "Jon", Age = 34 };

, string int, , , string ( , ) a int.

+8
List<String> MyList = new List<String>{"0","1","0"}.Where(X=>X!="1").ToList();

. , , 3 , .

, .

var foo = new 
{
    Prop1 = "value1",
    Prop2 = "value2"
};

, :

class Foo 
{
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
}

...
var foo = new Foo 
{
    Prop1 = "value1",
    Prop2 = "value2"
};
+2

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


All Articles