Anonymous types in C# 3.0
- Anonymous types are particularly useful when querying and transforming data with LINQ.
- You create anonymous types by using the new operator together with an object initializer.
- Anonymous types typically are used in the select clause of a query expression to return a subset of the properties from each object in the source sequence.
- Anonymous types contain one or more public read-only properties. No other kinds of class members, such as methods or events, are valid.
- The most common scenario is to initialize an anonymous type with properties from another type.
- If you do not 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 must provide a name for a property that is being initialized with an expression.
Example
class TheCar
private string make;
public string Make
{
get
{ return make; }
Set
{make = value; }
}
Private string model;
Public string Model
{
get { return model; }
Set { model = value; }
}
}
To create an
instance of this class and set the properties we would write
TheCar mycar = new
TheCar ();
mycar.Make = “Fiat”;
mycar.Model = “Fiat Punto”;
Using the
object initialization syntax we can write the following statement
var mycar=new TheCar{Make=”Fiat”,Model=”Fiat punto”};
With anonymous types I would rewrite the statement above to this
var mycar=new {Make=”Fiat”,Model=”Fiat punto”};
Remarks:
- Anonymous types are class types that derive directly from object, and that cannot be cast to any type except object. The compiler provides a name for each anonymous type, although your application cannot access it.
- If two or more anonymous types in the same assembly have the same number and type of properties, in the same order, the compiler treats them as the same type. They share the same compiler-generated type information.
- You cannot declare a field, a property, an event, or the return type of a method as having an anonymous type.
- 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 type object.
- Extension Methods:
- Extension methods allow you to easily extend a type, such as an
integer
orstring
, without re-compiling or modifying the type. In essence, they are a type ofstatic
(shared
in VB) method, but they are called as if the method is native to the type. Extension methods are available from the 3.5 version of the .NET Framework and can be implemented on any type in the .NET Framework or any custom type that you define. - You can use extension methods to extend a class or interface, but not to override them. An extension method with the same name and signature as an interface or class method will never be called.
- One downside to extension methods is if that you create an extension method with the same name as another method in that type, the compiler will bind the method call to the native method, not any extension. An extension method is only called when there is no native method found.
- Remember the following two points while implementing extension methods for a given type:
- An extension method will never be called if it has the same signature as a method defined in the type.
- Extension methods are brought into scope at the namespace level. For example, if you have multiple static classes that contain extension methods in a single namespace named Extensions, they will all be brought into scope by the using Extensions; directive.
No comments :
Post a Comment