Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 c语言中的逆变式有什么实际用法吗#_C# 4.0 - Fatal编程技术网

C# 4.0 c语言中的逆变式有什么实际用法吗#

C# 4.0 c语言中的逆变式有什么实际用法吗#,c#-4.0,C# 4.0,你能给出使用泛型求逆的实际用法吗 (如果同时从基础设施和自定义示例中选择,则效果会更好) 谢谢。微软的文档曾经是找到此类示例的好地方: 使用系统; 使用System.Collections.Generic; 抽象类形状 { 公共虚拟双区{get{return 0;}} } 类圆:形状 { 私人双r; 公共圆(双半径){r=radius;} 公共双半径{get{return r;}} 公共覆盖双区域{get{return Math.PI*r*r;}} } 类ShapeAreaComparer:S

你能给出使用泛型求逆的实际用法吗 (如果同时从基础设施和自定义示例中选择,则效果会更好)


谢谢。

微软的文档曾经是找到此类示例的好地方:

使用系统;
使用System.Collections.Generic;
抽象类形状
{
公共虚拟双区{get{return 0;}}
}
类圆:形状
{
私人双r;
公共圆(双半径){r=radius;}
公共双半径{get{return r;}}
公共覆盖双区域{get{return Math.PI*r*r;}}
}
类ShapeAreaComparer:System.Collections.Generic.IComparer
{
int IComparer.Compare(形状a、形状b)
{ 
如果(a==null)返回b==null?0:-1;
返回b==null?1:a.Area.CompareTo(b.Area);
}
}
班级计划
{
静态void Main()
{
//您可以通过ShapeAreaComparer,它实现了IComparer,
//即使SortedSet的构造函数期望
//IComparer,因为IComparer的类型参数T为
//逆变的。
分类集圆圈按区域=
新的分类数据集(新的ShapeAreaComparer())
{新圆圈(7.2),新圆圈(100),空,新圆圈(.01)};
foreach(圆形区域中的圆形c)
{
Console.WriteLine(c==null?“null”:“带区域的圆圈”+c.area);
}
}
}
/*此代码示例生成以下输出:
无效的
面积为0.000314159265358979的圆圈
面积为162.86016316095的圆圈
以面积31415.9265358979圈
*/
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
 */