c# - 为什么我必须在泛型类的静态方法调用上使用 <T>

标签 c# generics

我正在关注 this泛型类的例子。 因为我不想用测试代码填充项目的主要功能,所以我想创建一个运行代码示例的静态展示功能。

我的代码:

namespace Syntax
{
    public class GenericClass<T>
    {
        private class Node
        {
            private T data;
            private Node next;

            public Node(T t)
            {
                next = null;
                data = t;
            }

            public Node Next { get { return next; } set { next = value; } }
            public T Data { get { return data; } set { data = value; } }
        }


        private Node head;
        public GenericClass()
        {
            head = null;
        }

        public void AddHead(T t)
        {
            Node n = new Node(t);
            n.Next = head;
            head = n;
        }

        public IEnumerator<T> GetEnumerator()
        {
            Node current = head;
            while (current != null)
            {
                yield return current.Data;
                current = current.Next;
            }
        }


        public static void Showcase()
        {
            GenericClass<int> list = new GenericClass<int>();

            for(int x = 0; x <10; x++)
            {
                list.AddHead(x);
            }

            System.Console.WriteLine("\nPrinting generic class using ints.");
            foreach (int i in list)
            {
                System.Console.Write(i + ", ");
            }

            GenericClass<char> listChars = new GenericClass<char>();
            string abc = "abcdefghijklmnopqrstuvw";
            foreach (char c in abc)
            {
                listChars.AddHead(c);
            }

            System.Console.WriteLine("\n\nPrinting generic class using chars.");
            foreach (var c in listChars)
            {
                System.Console.Write(c + ", ");
            }
        }
    }
}

除了静态 Showcase 方法外,它与链接示例几乎完全相同。

现在在我的主要方法中我可以调用:

GenericClass<int>.Showcase();

我可以更改代码:

GenericClass<string>.Showcase();
GenericClass<char>.Showcase();

而且它仍然有效。

如果我能做到

GenericClass<int> test = new GenericClass<int>();
test.Showcase();

它需要一个类型,因为我可以在测试中调用其他方法,这对我来说是完全有意义的。

我看不到添加类型的好处,添加随机类的需要只会导致我头脑困惑,为什么静态方法的语法不是 genericClass<T>.StaticMethod();例如,将类型添加到静态方法 dosnt add anythig(或者是吗?)。那么为什么我需要使用 <T>在泛型类上调用静态方法时?

最佳答案

因为您还没有创建一个名为 GenericClass 的类,您已经创建了一个名为 GenericClass<T> 的开放通用类.

没有什么可以阻止您创建类 GenericClass在同一个命名空间中,没有什么可以阻止你创建一个名为 GenericClass<T1,T2> 的类.所有这些都可以存在于同一个命名空间中,并且它们之间没有显式或隐式关系,除非您声明一个。

所以,如果你想在“在一个类型参数中是通用的 GenericClass 类”上调用静态方法,你必须以某种方式说出来,并且你已经找到了如何做到这一点 - 通过提供一个类型参数。

有人可能会争辩说,如果静态方法不使用类型参数,它就是多余的——那么为什么不能只使用仍然开放的类型参数来调用它呢?好吧,首先是因为这必须是新语法才能实现1。其次,如果您的方法访问任何静态字段会怎样?对于泛型类型,用作类型参数的每个唯一类型都会导致存在一组新的静态字段。


why isnt the syntax for static methods genericClass<T>.StaticMethod(); for example as ...

以上是在我最初的回答之后添加的,我希望这已经在下面的脚注 1 中得到解决。但如果不清楚,这个简单的语法将不起作用。你需要发明一些新的语法,因为你可能有:

class Abc<T> {
   void DoSomething(){
       GenericClass<T>.StaticMethod();
   }
}

namespace X {
   class T {
   }
   class Abc {
      void DoSomething(){
          GenericClass<T>.StaticMethod();
      }
   }
}

在以上两个例子中T已经由外部范围定义。因此,您需要一些其他方式来表达“我不想为此泛型的第一个类型参数提供类型”。


1例如能够说GenericClass<T>.Showcase .允许它的任何新语法都不可能如此简单。因为 a) 可能有一个泛型类型参数 T在调用上下文的范围内,或者 b) 泛型类型参数的名称可能与调用上下文范围内的其他类型名称冲突。

关于c# - 为什么我必须在泛型类的静态方法调用上使用 <T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42342380/

相关文章:

c# - 服务堆栈: Difference between GetSession and SessionAs<t>

c# - ASP.Net MVC 如何用浏览器中无法解析的内容替换 HTML 标签

java - 无法使用通配符泛型类型向 Java 集合添加值

java - 使用泛型或 FooClass.CLASS 调用静态方法?

java - 通用对类

c# - C# 中的 XML - hasAttribute、getAttribute 不存在吗?为什么?

c# - 在 CustomAction 期间从 WiX 检索变量

java - 使用数组的通用队列

c# - SEHException .net 难题

class - Scala 生成器模式 : illegal cyclic reference involving type T