Why does a static method on a generic type require a Type parameter?

前端 未结 4 1934
说谎
说谎 2021-01-18 08:42
public class BinarySearchTree
where T : IComparable
{
    public static BinarySearchTree InitializeSampleCharacterBST()
    {
        v         


        
4条回答
  •  执笔经年
    2021-01-18 09:19

    You're forgetting that type parameters don't only appear in the parameter/return type of a method. They can also appear in the implementation:

    public static BinarySearchTree InitializeSampleCharacterBST()
    {
        var forSomeReason = new T();
    

    By placing your method inside a static class with a type parameter, you are saying that the implementation of the method may (now or in some future revision) depend upon that type parameter.

    If this isn't the case, you've put the method in the wrong place.

提交回复
热议问题