Microsoft Visual C# Step by Step (Developer Reference) by John Sharp
Author:John Sharp [Sharp, John]
Language: eng
Format: azw3, epub
Publisher: Pearson Education
Published: 2015-10-28T04:00:00+00:00
* * *
Note
Only interface and delegate types (which are covered in Chapter 18) can be declared as covariant. You do not specify the out modifier with generic classes.
* * *
Contravariant interfaces
Contravariance follows a similar principle to covariance except that it works in the opposite direction; it enables you to use a generic interface to reference an object of type B through a reference to type A as long as type B derives from type A. This sounds complicated, so it is worth looking at an example from the .NET Framework class library.
The System.Collections.Generic namespace in the .NET Framework provides an interface called IComparer, which looks like this:
Click here to view code image
public interface IComparer<in T>
{
int Compare(T x, T y);
}
A class that implements this interface has to define the Compare method, which is used to compare two objects of the type specified by the T type parameter. The Compare method is expected to return an integer value: zero if the parameters x and y have the same value, negative if x is less than y, and positive if x is greater than y. The following code shows an example that sorts objects according to their hash code. (The GetHashCode method is implemented by the Object class. It simply returns an integer value that identifies the object. All reference types inherit this method and can override it with their own implementations.)
Click here to view code image
class ObjectComparer : IComparer<Object>
{
int IComparer<Object>.Compare(Object x, Object y)
{
int xHash = x.GetHashCode();
int yHash = y.GetHashCode();
Download
Microsoft Visual C# Step by Step (Developer Reference) by John Sharp.epub
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Hello! Python by Anthony Briggs(9913)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9795)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8295)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7778)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Windows APT Warfare by Sheng-Hao Ma(6818)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6547)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6414)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6409)
Kotlin in Action by Dmitry Jemerov(5062)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4316)
Functional Programming in JavaScript by Mantyla Dan(4037)
Solidity Programming Essentials by Ritesh Modi(3992)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3782)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3723)
The Ultimate iOS Interview Playbook by Avi Tsadok(3699)
