Restrict generic T to specific types

前端 未结 3 1685
迷失自我
迷失自我 2021-01-06 01:42

I have the following method:

public static IResult Validate(this T value) {
} // Validate

How can I restrict T t

3条回答
  •  星月不相逢
    2021-01-06 02:31

    You can only do this:

    public static IResult Validate(this T value) where T: int
    {
        //validate
    } 
    

    Only classes and interfaces can be used as constraint.

提交回复
热议问题