Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit focus mode

Anonymous types

  • 2023-11-29
Feedback

In this article

Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first. The type name is generated by the compiler and is not available at the source code level. The type of each property is inferred by the compiler.

You create anonymous types by using thenew operator together with an object initializer. For more information about object initializers, seeObject and Collection Initializers.

The following example shows an anonymous type that is initialized with two properties namedAmount andMessage.

var v = new { Amount = 108, Message = "Hello" };// Rest the mouse pointer over v.Amount and v.Message in the following// statement to verify that their inferred types are int and string.Console.WriteLine(v.Amount + v.Message);

Anonymous types are typically used in theselect clause of a query expression to return a subset of the properties from each object in the source sequence. For more information about queries, seeLINQ in C#.

Anonymous types contain one or more public read-only properties. No other kinds of class members, such as methods or events, are valid. The expression that is used to initialize a property cannot benull, an anonymous function, or a pointer type.

The most common scenario is to initialize an anonymous type with properties from another type. In the following example, assume that a class exists that is namedProduct. ClassProduct includesColor andPrice properties, together with other properties that you are not interested in:

class Product{    public string? Color {get;set;}    public decimal Price {get;set;}    public string? Name {get;set;}    public string? Category {get;set;}    public string? Size {get;set;}}

The anonymous type declaration starts with thenew keyword. The declaration initializes a new type that uses only two properties fromProduct. Using anonymous types causes a smaller amount of data to be returned in the query.

If you don't specify member names in the anonymous type, the compiler gives the anonymous type members the same name as the property being used to initialize them. You provide a name for a property that's being initialized with an expression, as shown in the previous example. In the following example, the names of the properties of the anonymous type areColor andPrice. The instances are item from theproducts collection ofProduct types:

var productQuery =    from prod in products    select new { prod.Color, prod.Price };foreach (var v in productQuery){    Console.WriteLine("Color={0}, Price={1}", v.Color, v.Price);}

Tip

You can use .NET style ruleIDE0037 to enforce whether inferred or explicit member names are preferred.

It is also possible to define a field by object of another type: class, struct or even another anonymous type. It is done by using the variable holding this object just like in the following example, where two anonymous types are created using already instantiated user-defined types. In both cases theproduct field in the anonymous typeshipment andshipmentWithBonus will be of typeProduct containing its default values of each field. And thebonus field will be of anonymous type created by the compiler.

var product = new Product();var bonus = new { note = "You won!" };var shipment = new { address = "Nowhere St.", product };var shipmentWithBonus = new { address = "Somewhere St.", product, bonus };

Typically, when you use an anonymous type to initialize a variable, you declare the variable as an implicitly typed local variable by usingvar. The type name cannot be specified in the variable declaration because only the compiler has access to the underlying name of the anonymous type. For more information aboutvar, seeImplicitly Typed Local Variables.

You can create an array of anonymously typed elements by combining an implicitly typed local variable and an implicitly typed array, as shown in the following example.

var anonArray = new[] { new { name = "apple", diam = 4 }, new { name = "grape", diam = 1 }};

Anonymous types areclass types that derive directly fromobject, and that cannot be cast to any type exceptobject. The compiler provides a name for each anonymous type, 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.

If two or more anonymous object initializers in an assembly specify a sequence of properties that are in the same order and that have the same names and types, the compiler treats the objects as instances of the same type. They share the same compiler-generated type information.

Anonymous types support non-destructive mutation in the form ofwith expressions. This enables you to create a new instance of an anonymous type where one or more properties have new values:

var apple = new { Item = "apples", Price = 1.35 };var onSale = apple with { Price = 0.79 };Console.WriteLine(apple);Console.WriteLine(onSale);

You cannot declare a field, a property, an event, or the return type of a method as having an anonymous type. Similarly, you cannot declare a formal parameter of a method, property, constructor, or indexer as having an anonymous type. To pass an anonymous type, or a collection that contains anonymous types, as an argument to a method, you can declare the parameter as typeobject. However, usingobject for anonymous types defeats the purpose of strong typing. If you must store query results or pass them outside the method boundary, consider using an ordinary named struct or class instead of an anonymous type.

Because theEquals andGetHashCode methods on anonymous types are defined in terms of theEquals andGetHashCode methods of the properties, two instances of the same anonymous type are equal only if all their properties are equal.

Note

Theaccessibility level of an anonymous type isinternal, hence two anonymous types defined in different assemblies are not of the same type.Therefore instances of anonymous types can't be equal to each other when defined in different assemblies, even when having all their properties equal.

Anonymous types do override theToString method, concatenating the name andToString output of every property surrounded by curly braces.

var v = new { Title = "Hello", Age = 24 };Console.WriteLine(v.ToString()); // "{ Title = Hello, Age = 24 }"
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNo

In this article

Was this page helpful?

YesNo