In c# what does 'where T : class' mean?

前端 未结 10 1944
太阳男子
太阳男子 2020-11-29 18:53

In C# what does where T : class mean?

Ie.

public IList DoThis() where T : class
相关标签:
10条回答
  • 2020-11-29 19:23

    It is a generic type constraint. In this case it means that the generic type T has to be a reference type (class, interface, delegate, or array type).

    0 讨论(0)
  • 2020-11-29 19:27

    That restricts T to reference types. You won't be able to put value types (structs and primitive types except string) there.

    0 讨论(0)
  • 2020-11-29 19:35

    it means that the type used as T when the generic method is used must be a class - i.e. it cannot be a struct or built in number like int or double

    // Valid:
    var myStringList = DoThis<string>();
    // Invalid - compile error
    var myIntList = DoThis<int>();
    
    0 讨论(0)
  • 2020-11-29 19:35

    'T' represents a generic type. It means it can accept any type of class. The following article might help:

    http://www.15seconds.com/issue/031024.htm
    
    0 讨论(0)
提交回复
热议问题