views:

84

answers:

2

I'm having a doubt regarding the memmory allocation for anonymous type variable.

if i declare a variable int Vaiable_Name it will allocate 4 bytes

but in case of Anonymous types , what will happen , and when the memory will get deallocated ?

Need we deallocate that manually??

for example

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

Here how much bytes will be allocated for X?

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

This is not anonymous type. It is a collection initializer which creates a list containing 3 elements and then filters the initially created list.

Anonymous types would behave and consume the same memory as their non-anonymous type equivalent.

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

would be the same as if we had a type:

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

...
var foo = new Foo 
{
    Prop1 = "value1",
    Prop2 = "value2"
};
Darin Dimitrov
@ Darin Dimitrov : What about `X`?
Pramodh
@Pramodh x is not a variable, its a delegate.
RPM1984
@RPM1984: No, X is the parameter in the lambda expression, which ends up being a parameter in a generated method.
Jon Skeet
Youre right, i was referring to the first part of the lambda (X =>). i was under the impression that lamba is saying "X" is a delegate, to which X!= "1" is handling.
RPM1984
@RPM1984: No, it's saying that X is a parameter within the delegate. The whole lambda expression is turned into the delegate. See my answer for the transformation that the compiler performs.
Jon Skeet
+6  A: 

You haven't actually shown any anonymous types. You've shown a lambda expression. In this case, the compiler will effectively have created an extra method for you, like this:

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();

... except actually, the compiler will create a single delegate instance in this case, and cache it. (And of course it will translate the uses of extension methods into normal calls to Enumerable.Where and Enumerable.ToList.)

So X ends up as a string parameter, effectively. At execution time, there's no such thing as a lambda expression (leaving expression trees aside). There's just a delegate created using the generated method.

Now if you were using anonymous types, like this:

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

then that would just create a new class, containing a string variable and an int variable, and with the same memory footprint as a normal class containing a string (which is a reference, of course) and an int.

Jon Skeet
@ Jon Skeet : i'm asking about the `X` in that LAMBDA expression
Pramodh
@Pramodh: And I've shown what happens to it. It effectively becomes a parameter in the generated method.
Jon Skeet
@ Jon Skeet :Thank you ... If i use `for` loop and `if` condition instead of the above lambda exprassion , which will use more memory lanbda or `for - if` loop ?
Pramodh
@Pramodh: Given the complexity of what actually happens under the hood, it's very hard to say *exactly* what will occur after JITting. But the difference will be inconsequential. You're worrying about the wrong thing here - make the code as readable as possible.
Jon Skeet
@ Jon Skeet : Thank you... :-)
Pramodh
Haha when I saw the question I immediately thought this is a Jon Skeet question
Jaco Pretorius
@ Jaco Pretorius : i dint get u....
Pramodh