Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i insert a new object to anonymous array?

Tags:

c#

linq

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();
like image 264
uzay95 Avatar asked Feb 04 '10 13:02

uzay95


People also ask

What is an anonymous array?

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.

Can an object be anonymous?

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.

What is anonymous array in C++?

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.

How do I create an anonymous array in Perl?

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.


2 Answers

.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.

like image 169
Lasse V. Karlsen Avatar answered Oct 15 '22 00:10

Lasse V. Karlsen


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.

like image 41
Chris Pitman Avatar answered Oct 15 '22 00:10

Chris Pitman