7,30 €
This Wrox Blox is a value-packed resource to help experienced .NET developers learn the new .NET release. It is excerpted from the Wrox books: Professional C# 4 and .NET 4, Professional ASP.NET 4, and WPF Programmer's Reference by Christian Nagel, Bill Evjen, Scott Hanselman, and Rod Stephens, and includes more than 100 print book pages drawn from these three key titles. It is an excellent resource to help .NET developers get up to speed fast on .NET 4, C# 4.0, ASP.NET 4, and WPF, providing all the information needed to program with the important new features, including: C# Dynamic Types and Parallel Tasks; ASP.NET Ajax, Chart Controls, MVC, and Object Caching; and key WPF principles as developers move from WinForms to WPF. In addition, it provides examples built with the native Visual Studio 2010 tools that developers are comfortable with.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 173
Veröffentlichungsjahr: 2010
Table of Contents
Cover
Title Page
Professional C# 4 and .NET 4
Covariance and Contra-variance
Tuples
The Dynamic Type
Code Contracts
Tasks
Parallel Class
Cancellation Framework
Taskbar and Jump List
Professional ASP.NET 4 in C# and VB
Chart Server Control
ASP.NET AJAX Control Toolkit
Extending <outputCache>
.NET 4’s New Object Caching Option
Historical Debugging with IntelliTrace
Debugging Multiple Threads
ASP.NET MVC
Using WCF Data Services
Creating Your First Service
Building an ASP.NET Web Package
WPF Programmer’s Reference
Code-behind Files
Example Code
Event Name Attributes
Resources
Styles and Property Triggers
Event Triggers and Animation
Templates
Skins
Printing Visual Objects
Printing Code-Generated Output
Data Binding
Transformations
Effects
Documents
Three-Dimensional Drawing
About the Authors
Copyright
Advertisement
Covariance and Contra-variance
Previous to .NET 4, generic interfaces were invariant. .NET 4 adds an important extension for generic interfaces and generic delegates with covariance and contra-variance. Covariance and contra-variance are about the conversion of types with argument and return types. For example, can you pass a Rectangle to a method that requests a Shape? Let’s get into examples to see the advantages of these extensions.
With .NET, parameter types are covariant. Assume you have the classes Shape and Rectangle, and Rectangle derives from the Shape base class. The Display() method is declared to accept an object of the Shape type as its parameter:
publicvoidDisplay(Shapeo){}
Now you can pass any object that derives from the Shape base class. Because Rectangle derives from Shape, a Rectangle fulfills all the requirements of a Shape and the compiler accepts this method call:
Rectangler=newRectangle{Width=5,Height=2.5};
Display(r);
Return types of methods are contra-variant. When a method returns a Shape it is not possible to assign it to a Rectangle because a Shape is not necessarily always a Rectangle. The opposite is possible. If a method returns a Rectangle as the GetRectangle() method,
publicRectangleGetRectangle();
the result can be assigned to a Shape.
Shapes=GetRectangle();
Before version 4 of the .NET Framework, this behavior was not possible with generics. With C# 4, the language is extended to support covariance and contra-variance with generic interfaces and generic delegates. Let’s start by defining a Shape base class and a Rectangle class:
publicclassShape
{
publicdoubleWidth{get;set;}
publicdoubleHeight{get;set;}
publicoverridestringToString()
{
returnString.Format("Width:{0},Height:{1}",Width,Height);
}
}
Pro C# 4 9780470502259 code snippet Variance/Shape.cs
publicclassRectangle:Shape
{
}
Pro C# 4 9780470502259 code snippet Variance/Rectangle.cs
Covariance with Generic Interfaces
A generic interface is covariant if the generic type is annotated with the out keyword. This also means that type T is allowed only with return types. The interface IIndex is covariant with type T and returns this type from a read-only indexer:
publicinterfaceIIndex<outT>
{
Tthis[intindex]{get;}
intCount{get;}
}
Pro C# 4 9780470502259 code snippet Variance/IIndex.cs
If a read-write indexer is used with the IIndex interface, the generic type T is passed to the method and also retrieved from the method. This is not possible with covariance — the generic type must be defined as invariant. Defining the type as invariant is done without out and in annotations.
The IIndex<T> interface is implemented with the RectangleCollection class. RectangleCollection defines Rectangle for generic type T:
publicclassRectangleCollection:IIndex<Rectangle>
{
privateRectangle[]data=newRectangle[3]
{
newRectangle{Height=2,Width=5},
newRectangle{Height=3,Width=7},
newRectangle{Height=4.5,Width=2.9}
};
publicstaticRectangleCollectionGetRectangles()
{
returnnewRectangleCollection();
}
publicRectanglethis[intindex]
{
get
{
if(index<0||index>data.Length)
thrownewArgumentOutOfRangeException("index");
returndata[index];
}
}
publicintCount
{
get
{
returndata.Length;
}
}
}
Pro C# 4 9780470502259 code snippet Variance/RectangleCollection.cs
The RectangleCollection.GetRectangles() method returns a RectangleCollection that implements the IIndex<Rectangle> interface, so you can assign the return value to a variable of the type. Because the interface is covariant, it is also possible to assign the returned value to a variable of . does not need anything more than a has to offer. Using the variable, the indexer from the interface and the property are used within the loop:
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Lesen Sie weiter in der vollständigen Ausgabe!
Tausende von E-Books und Hörbücher
Ihre Zahl wächst ständig und Sie haben eine Fixpreisgarantie.
Sie haben über uns geschrieben: