class Test<string, T> : System.Collections.Concurrent.ConcurrentDictionary<string, T>
上面的代码返回了两个错误:
需要 CS1001 标识符
CS1003 语法错误,预期为“,”
但是,在 Test 中将 string
大写不会产生错误。
class Test<String, T> : System.Collections.Concurrent.ConcurrentDictionary<string, T>
谁能解释为什么当 string
是小写时编译器会产生这些错误,而大写时不会产生这些错误?
最佳答案
当你写 class Test<string, T>
您正在尝试定义一个新的类型参数并将该类型参数命名为 string
. string
不是类型的有效标识符,因为它是语言中的保留字。 String
不是语言中的保留字;它是您提供新类型参数的有效名称,因此代码有效。
或者至少,在您现在将新类型别名命名为 String
的意义上“有效” .但是您实际上并不想这样做;你不想有一个新的类型参数命名 String
,你只想让你的字典的键是 String
,所以根本不要定义新的类型参数:
class Test<T> : System.Collections.Concurrent.ConcurrentDictionary<string, T>
在相关说明中,您可能不应该继承自 ConcurrentDictionary
根本。它不是设计用于继承的类型。您可能只想编写一个,并拥有自己的类型,该类型仅将 ConcurrentDictionary 作为字段。
关于c# - 从 ConcurrentDictionary 继承时出现编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45241276/