views:

401

answers:

4

Hello, I would like to ask if i can create a anonymous type variable and later on i can add more Properties? like var x = new { Name = "Ahmed" }; and want to add Age to it? how i can 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

A: 

No, you cant.

2nd question: No such thing, it might refer to compiler generated class which you have no access to.

leppie
+3  A: 

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.

Justin Niessner
A: 

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.

bua
A: 

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!

Joshua