Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I specify a meaningful name for an anonymous class in C#?

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?

like image 767
Prashant Cholachagudda Avatar asked Apr 27 '09 13:04

Prashant Cholachagudda


People also ask

What is an anonymous type in C?

Updated on: May 2, 2020. In C#, an anonymous type is a type (class) without any name that can contain public read-only properties only. It cannot contain other members, such as fields, methods, events, etc. You create an anonymous type using the new operator with an object initializer syntax.

What is the difference between an anonymous type and a regular data type?

The compiler gives them a name although your application cannot access it. From the perspective of the common language runtime, an anonymous type is no different from any other reference type, except that it cannot be cast to any type except for object.

How do I make an anonymous object?

You create anonymous types by using the new operator together with an object initializer. For more information about object initializers, see Object and Collection Initializers. The following example shows an anonymous type that is initialized with two properties named Amount and Message .

Which of the following is used to create anonymous type?

We can create anonymous types by using “new” keyword together with the object initializer. As you can see from the below code sample, the type is store in a var and has two data items.


10 Answers

public class Employee {}

like image 92
Chad Grant Avatar answered Oct 02 '22 13:10

Chad Grant


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.

like image 39
Chris Avatar answered Oct 02 '22 14:10

Chris


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" };
like image 22
Nick Berardi Avatar answered Oct 02 '22 12:10

Nick Berardi


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" };
like image 32
Dead account Avatar answered Oct 02 '22 14:10

Dead account


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

like image 23
Mia Clarke Avatar answered Oct 02 '22 13:10

Mia Clarke


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

like image 20
Damian Avatar answered Oct 02 '22 13:10

Damian


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.

like image 34
Luis Abreu Avatar answered Oct 02 '22 14:10

Luis Abreu


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.

like image 28
jcopenha Avatar answered Oct 02 '22 14:10

jcopenha


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.

like image 23
JaredPar Avatar answered Oct 02 '22 12:10

JaredPar


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.

like image 28
Mark Carpenter Avatar answered Oct 02 '22 14:10

Mark Carpenter