小编NNN*_*NNN的帖子

C#SortedSet <T>和相等

我对SortedSet的行为有点疑惑,请看下面的例子:

public class Blah
{
    public double Value { get; private set; }

    public Blah(double value)
    {
        Value = value;
    }
}

public class BlahComparer : Comparer<Blah>
{
    public override int Compare(Blah x, Blah y)
    {
        return Comparer<double>.Default.Compare(x.Value, y.Value);
    }
}

public static void main()
{
    var blahs = new List<Blah> {new Blah(1), new Blah(2), 
                                new Blah(3), new Blah(2)}

    //contains all 4 entries
    var set = new HashSet<Blah>(blahs); 

    //contains only Blah(1), Blah(2), Blah(3)
    var sortedset = new SortedSet<Blah>(blahs, new BlahComparer()); …
Run Code Online (Sandbox Code Playgroud)

c# equals sortedset

12
推荐指数
2
解决办法
9024
查看次数

派生类的C#方法作为基础构造函数中的委托

为什么以下C#不合法?是否存在正确的解决方法?

public class Base
{
    public Base(Func<double> func) { }
}

public class Derived : Base
{
    public Derived() : base(() => Method()) <-- compiler: Cannot access non-static method 'Method' in static context
    {
    }

    public double Method() { return 1.0; }
}
Run Code Online (Sandbox Code Playgroud)

c# static delegates

8
推荐指数
1
解决办法
3804
查看次数

标签 统计

c# ×2

delegates ×1

equals ×1

sortedset ×1

static ×1