How can i insert a new object to anonymous array?
var v = new[]
{
new {Name = "a", Surname = "b", Age = 1},
new {Name = "b", Surname = "c", Age = 2}
};
I know first of all we set the array's limit(size).
I convert it to List. To insert a new object.
v.ToList().Add(new { Name = "c", Surname = "d", Age = 3 });
But still i have 2 elements in v variable. Where has the third element gone?
But i can't assign to another List variable.
List newV = v.ToList();
An array in Java without any name is known as an anonymous array. It is an array just for creating and using instantly. Using an anonymous array, we can pass an array with user values without the referenced variable. Properties of Anonymous Arrays: We can create an array without a name.
An anonymous object is basically a value that has been created but has no name. Since they have no name, there's no other way to refer to them beyond the point where they are created. Consequently, they have “expression scope,” meaning they are created, evaluated, and destroyed everything within a single expression.
The "anonymous array" you refer to is actually an initializer list. Now that C++11 is out, this can be done with the built-in initializer_list type. You theoretically can also use it as a C-style initializer list by using extern C , if your compiler parses them as C99 or later.
You create an anonymous array by using square brackets like this: $arrayref = [ 'one', 'two', 'three' ]; There exists an array that has no name but is available via the $arrayref reference, which is just like any other array reference in all other respects.
.ToList()
produces a new list object, adding all the elements of the input source, your array, into it. As such, the original array isn't changed at all.
You cannot add elements to an existing array, it has a fixed size, the only thing you can do is put a new array back into the variable.
I haven't tried it, but try this:
var l = v.ToList();
l.Add(...); // your existing add code
v = l.ToArray();
But, note that at this point you should look at why you want to use anonymous types in the first place, I would seriously think about just creating a named type, and using a list to begin with.
Note that you cannot write List l = v.ToList();
as the type of the list is generic (it will return some List<some-anonymous-type-here>
, not just List
. With anonymous types, you need to use var
.
It is not a "List" but a generic list typed on your anonymous type. Because of this, you will not be able to write out the type explicitly for this operation. You must either use "var newV = v.ToList()" or type it as an IEnumerable, which is a non-generic interface that generic lists implement.
Your above code for adding a new item might not be doing what you think, either. Right now it is not adding a new item to v, but creating a new list, adding an item to that list, and then the list is gone because you have no reference to it. You need to convert v to a list, then add the item.
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