44

We all know that when we create an anonymous class like this:

var Employee = new { ID = 5, Name= "Prashant" };

...at run time it will be of type:

<>f__AnonymousType0<int,string>

Is there any way to specify a meaningful name to such classes?

Matthew Lock
  • 13,144
  • 12
  • 92
  • 130
Prashant Cholachagudda
  • 13,012
  • 23
  • 97
  • 162
  • 18
    http://dictionary.reference.com/browse/anonymous – Cerebrus Apr 27 '09 at 13:19
  • 7
    it's really not a stupid question, I'm with Jon skeet on this one, we need named anonymous types. taken from http://stackoverflow.com/questions/534690/linq-to-sql-return-anonymous-type – Maslow Aug 04 '09 at 02:36
  • 1
    I'm not sure, but there are no anonymous classes in C#. There are anonymous types, and this makes very big difference. – Damian Jul 01 '11 at 21:53
  • use codedom, there is an article: http://msdn.microsoft.com/en-us/library/y2k85ax6.aspx –  Nov 25 '12 at 04:36

10 Answers10

130
public class Employee {}

Chad Grant
  • 44,326
  • 9
  • 65
  • 80
40

It's an anonymous type, that defeats the purpose. Those objects are designed to be temporary. Hell, the properties are even read-only.

Sorry, I'm being a smart-ass. The answer is no, there is no way to tell the compiler what name to use for an anonymous type.

In fact, the names of the types generated by the compiler use illegal characters in their naming so that you cannot have a name collision in your application.

Chris
  • 6,702
  • 8
  • 44
  • 60
36
public class Employee {
    public int ID { get; set; }
    public string Name { get; set; }
}

Then use the following syntax

var employee = new Employee { ID = 5, Name = "Prashant" };
Nick Berardi
  • 54,393
  • 15
  • 113
  • 135
26

Actually, if you're not afraid of getting extremely nitty gritty, you could use TypeBuilder to build your own runtime type based on your anonymous type, which will allow you to specify a name for the type. Of course, it is much easier to just declare a class as almost everyone else in this thread suggested, but the TypeBuilder way is far more exciting. ;)

TypeBuilder

Mia Clarke
  • 8,134
  • 3
  • 49
  • 62
24

Make it a regular class with a name?

public class Employee
{
    public int ID;
    public string Name;
}


var Employee = new Employee { ID = 5, Name= "Prashant" };
Dead account
  • 19,587
  • 13
  • 52
  • 82
8

The answer in Java World would be a local class (defined in a method), which are absent in C#.

Damian
  • 2,930
  • 6
  • 39
  • 61
  • 1
    I was actually looking for this exact functionality when I found this thread. I have only worked a couple years in Java as opposed to ~10 years in .NET. Being able to create a local class (from an interface or a base class) is definitely a very useful feature in Java that is missing in .NET. – MikeJansen Jun 15 '12 at 15:18
4

Yes, you are creating an Anonymous Class , if you want your class to have a name, i.e. Not Anonymous, then declare a regular class or struct.

jcopenha
  • 3,935
  • 1
  • 17
  • 15
4

Since it's anonymous, you cannot name it. If you need to know the name of a type, then you must really create a class.

Luis Abreu
  • 4,008
  • 9
  • 34
  • 63
4

No, there is no way to give a meaningful type name to these classes as you've declared them. Anonymous Types are just that, anonymous. There is no way to explicitly "name" the type in code without resorting to very ugly hacks.

If you really need to give the type a name you will need to explicitly declare and use a new type with the properties you need.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
2

I think that, by definition, Anonymous Types can't have a name, just that which the compiler gives it. If you're looking for more meaningful design-time information on Anonymous Types then you're expecting too much from the compiler.

Mark Carpenter
  • 17,445
  • 22
  • 96
  • 149