相关疑难解决方法(0)

编译通用接口与通用抽象类和params关键字

考虑以下示例 - 这使用一个接口:

public interface IAlgorithm<TResult, TInput>
{
    TResult Compute(TInput input);
}

class A : IAlgorithm<int, byte[]>
{
    // Notice the use of params...not strictly what the interface specifies but it works.
    public int Compute(params byte[] input)
    {
        // no sane developer would go to this length to prove a point
        return input[0];
    }
}

A inst = new A();
Console.WriteLine(inst.Compute(1, 2, 3));
// 1
Run Code Online (Sandbox Code Playgroud)

这个例子显示了一个接口,它的方法implementation(params byte[])deos与接口契约(byte[])不完全匹配......但是它有效!

考虑以下示例 - 它使用抽象类:

public abstract class Algorithm<TResult, TInput>
{ …
Run Code Online (Sandbox Code Playgroud)

.net c# abstract-class compiler-errors interface

11
推荐指数
0
解决办法
69
查看次数

标签 统计

.net ×1

abstract-class ×1

c# ×1

compiler-errors ×1

interface ×1