I don't understand the difference between Anonymous Class
and object Class
if i have an anonymous type object :
var x = new []
{
new {name = "x", phone = 125},
new {name = "f", phone = 859},
new {name="s",phone=584}
};
i can see how i can iterate through it and get values of the properties doing this x.name ...
but if i change this to an object
var x = new Object[]
{
new {name = "x", phone = 125},
new {name = "f", phone = 859},
new {name="s",phone=584}
};
then if i loop through it i will not be able to access the properties??
i have tried different ways to be able to get the values from this object and i failed, may be i missed something? and what is the point of generating object array
and how can i consume it??
and i know that i can't do something like this : new Object[] {name = 'x' ...
The object array can store any value. For example, this does not generate any error:
var x = new Object[]
{
new {name = "x", phone = 125},
new {name = "f", phone = 859},
new {name = "s", phone = 584}
};
x[0] = "foo";
Whereas the array of the anonymous class is strongly typed to that specific anonymous class. So, this would generate an error:
var x = new[]
{
new {name = "x", phone = 125},
new {name = "f", phone = 859},
new {name = "s", phone = 584}
};
x[0] = "foo"; // Cannot implicitly convert type 'string' to 'AnonymousType#1'
In other words, the compiler ensures that the elements of the array of the anonymous class are only instances of that anonymous class (or null).
This is why when you write a loop like this:
for (var instance in x)
Console.WriteLine(x.name);
It will work when x
is declared as an array of the anonymous class, because the compiler can infer that all the elements of that array are instances of the anonymous class. However, when x
is declared as an array of objects, the compiler cannot infer the actual types of the elements of that array (only that they inherit from Object
, which includes all values in .NET).
In both cases arrays are strongly typed. In both cases, arrays contain objects of the same anonymous type. The only difference that in the first case the array has the type of the anonymous object, so the compiler knows of properties of the objects inside it, while in the second case the compiler does not know of these properties.
I have tried different ways to be able to get the values from this object and i failed, may be i missed something?
The properties, however remain there, and you can access them through reflection or by using dynamic
type of C# 4:
var x = new Object[]
{
new {name = "x", phone = 125},
new {name = "f", phone = 859},
new {name="s",phone=584}
};
var bigPhone = x.Cast<dynamic>().Where(v => v.phone > 500);
foreach (dynamic item in bigPhone) {
Console.WriteLine("name={0}, phone={1}", item.name, item.phone);
}
Note that this solution is not an equivalent alternative to using a statically typed array from your first example. It is merely a work-around to a situation when the static type is lost.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With