2

次のような値型 (構造体) の配列である型を含む一般的な制約を作成したいと思います。

public class X<T> where T : struct[]

または多分

public class X<T, U>
    where U : struct
    where T : U[]

しかし、これは機能しません。System.Array は型制約として使用できないようです。

では、ジェネリック パラメータを構造体の配列に制限するにはどうすればよいでしょうか。

最初の回答後に更新:

public interface IDeepCopyable<T>
{
    T DeepCopy(T t);
}


public class DeepCopyArrayOfValueTypes<T> : IDeepCopyable<T>
    where T : is an array of value types
{
    T Copy(T t) {...}
}
4

1 に答える 1

13

その必要はありません。

に制約してから 、型パラメーターを使用するときの代わりに: struct書き込みます。T[]T

public class DeepCopyArrayOfValueTypes<T> : IDeepCopyable<T[]>
    where T : struct
{
    public T[] DeepCopy(T[] t) {...}
}
于 2013-12-04T15:40:45.267 に答える