【发布时间】:2015-04-14 15:05:35
【问题描述】:
我有以下示例代码。为什么我必须为 T 指定一个类型?这是我可以利用的东西吗?我不明白为什么枚举定义取决于类型参数?根据类型,枚举的内容如何成为其他内容?因此,在我的语法糖的支持下,我想保留类型的定义。
public class SomeGenericClass<T>
{
public enum InitialisationMode
{
UseDefaultValues,
DoOtherThings
}
public SomeGenericClass(InitialisationMode initMode = InitialisationMode.UseDefaultValues)
{
}
}
public class SomeOtherClass
{
public void DoAction()
{
//Why do I have to specify the generic type arguments when using the enum value?
var genericClass = new SomeGenericClass<int>(SomeGenericClass<int>.InitialisationMode.DoOtherThings);
//I Would expect to be able to use the enum like the line below:
genericClass = new SomeGenericClass<int>(SomeGenericClass.InitialisationMode.DoOtherThings);
}
}
我知道我可以通过做这样的事情来解决这个问题,或者将枚举放在一个作用域上:
public class SomeGenericClass<T>
{
public SomeGenericClass(SomeGenericClass.InitialisationMode initMode = SomeGenericClass.InitialisationMode.UseDefaultValues)
{
}
}
public abstract class SomeGenericClass
{
public enum InitialisationMode
{
UseDefaultValues,
DoOtherThings
}
}
public class SomeOtherClass
{
public void DoAction()
{
//I Would expect to be able to use the enum like the line below:
var genericClass = new SomeGenericClass<int>(SomeGenericClass.InitialisationMode.DoOtherThings);
}
}
【问题讨论】:
-
T没有在任何地方使用。如果不使用泛型参数,那么声明它的原因是什么? -
在需要公开使用枚举时,很好奇在类中声明枚举的原因。为什么不在课堂之外声明它并避免这个问题?
-
嗯,他们是规则。为什么不直接解决问题并将枚举类型移到课堂之外?
-
@haim770 由于此代码的核心目的是展示如何引用该枚举,所有其他代码都掩盖了代码的示例性目的。