Creating dynamic anonymous type variables

Is it possible to create an anonymous type variable and then add additional properties?

eg. var x = new { Name = "Ahmed" };and want to add to it Age? How can i do this?

Another question: I saw in some blogs the type AnonymousType, what is the namespace for this class? here is an example http://www.codeproject.com/KB/cs/AnonymousTypesInCSharp.aspx

+3
source share
4 answers

The first question is you cannot.

The second question - an anonymous type - is the type of the article created by the author. You must download the source for your project in order to use this type.

+6

, .

2- : , , .

0

, . , - ?

using System;
class Generic{

   public void doSomething(){}

   private string name;
   private string _othreFeature;

}

You can expand it with new features whenever you want.

0
source

This is a completely logical question ... I work with Java and C #, and this is my big pet ... In fact, almost every language there has a kind of anonymous type lol ... PHP, JavaScript, C #, vb (all), and the list goes on ..

This is a very useful feature when you basically throw data into an object, and you don't need a class to create it.

In fact, this is exactly what LINQ uses ..

var someObject (can be interable/Enumerable) = 
    from p in products
    where p.id == 123
    select new { productid };

blah blah, you get the idea .. This is very useful ... :-)

Hurrah!

0
source

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


All Articles