2017-04-19 17 views
0

您是否可以給出使用泛型的實際用法 - 逆變法 (如果來自基礎架構和自定義示例,那麼將會很好)。c中是否有任何逆變的實際用法?

謝謝。

+3

的可能的複製[協變和逆變現實世界的例子(http://stackoverflow.com /問題/ 2662369 /協方差和逆變,真實世界的例子) –

回答

0

微軟的文檔使用時必須找到這樣的例子一個非常好的地方:

https://msdn.microsoft.com/en-us/library/dd799517(v=vs.110).aspx

using System; 
using System.Collections.Generic; 

abstract class Shape 
{ 
    public virtual double Area { get { return 0; }} 
} 

class Circle : Shape 
{ 
    private double r; 
    public Circle(double radius) { r = radius; } 
    public double Radius { get { return r; }} 
    public override double Area { get { return Math.PI * r * r; }} 
} 

class ShapeAreaComparer : System.Collections.Generic.IComparer<Shape> 
{ 
    int IComparer<Shape>.Compare(Shape a, Shape b) 
    { 
     if (a == null) return b == null ? 0 : -1; 
     return b == null ? 1 : a.Area.CompareTo(b.Area); 
    } 
} 

class Program 
{ 
    static void Main() 
    { 
     // You can pass ShapeAreaComparer, which implements IComparer<Shape>, 
     // even though the constructor for SortedSet<Circle> expects 
     // IComparer<Circle>, because type parameter T of IComparer<T> is 
     // contravariant. 
     SortedSet<Circle> circlesByArea = 
      new SortedSet<Circle>(new ShapeAreaComparer()) 
       { new Circle(7.2), new Circle(100), null, new Circle(.01) }; 

     foreach (Circle c in circlesByArea) 
     { 
      Console.WriteLine(c == null ? "null" : "Circle with area " + c.Area); 
     } 
    } 
} 

/* This code example produces the following output: 

null 
Circle with area 0.000314159265358979 
Circle with area 162.860163162095 
Circle with area 31415.9265358979 
*/ 
相關問題