Reputation: 6030
Can I create an anonymous type variable and later on add more Properties?
E.g.
var x = new { Name = "Ahmed" };
and want to add Age
to it?
how can I do this?
Another question: i saw on some blogs a type AnonymousType
what is the name space for this class? here is am example http://www.codeproject.com/KB/cs/AnonymousTypesInCSharp.aspx
Upvotes: 3
Views: 2468
Reputation: 1
This is a perfectly logical question... I work with Java and C# both and this is a big pet peeve of mine... Matter of fact almost every language out there has some sort of Anonymous type lol... PHP, JavaScript, C#, vb (all), and the list goes on..
It's a very useful feature to have when you're basically throwing data into an object and you don't need a class for it created.
Matter of fact this is exactly what LINQ utilizes..
var someObject (can be interable/Enumerable) =
from p in products
where p.id == 123
select new { productid };
blah blah you get the idea.. It's very useful... :-)
Cheers!
Upvotes: 0
Reputation: 4870
No, and It would not be efficient for Your application. Are You convinced that You don't want to introduce some kind of class?
using System;
class Generic{
public void doSomething(){}
private string name;
private string _othreFeature;
}
You could extend it with new features, whenever You want.
Upvotes: 0
Reputation: 245429
First question - you can't.
Second question - AnonymousType is the type the author of that article created. You have to download the source for his project to use that type.
Upvotes: 6
Reputation: 117250
No, you cant.
2nd question: No such thing, it might refer to compiler generated class which you have no access to.
Upvotes: 0