不幸的是,你不能这样做。C/C++中typedef的主要C#替代方案通常是
类型推断
,例如使用
var
关键字,但在许多情况下仍然必须键入泛型定义。几乎所有的C#程序员都使用Visual Studio或其他IDE,这是有原因的,因为在许多情况下,这些IDE可以避免他们键入所有内容。
我真的不会太推荐“作为typedef使用”模式,因为我预计它会让大多数C#程序员感到陌生和惊讶。此外,我认为无论如何都必须在每个文件中包含“psuedo typedef”这一事实大大降低了它的实用性。
当然,你可以考虑做的一件事是用你想要typedef的东西制作实际的类,例如这样:
public class ConfigValue : List<string>
{
}
public class ConfigKey
{
private string s;
public ConfigKey(string s)
{
this.s = s;
}
// The implicit operators will allow you to write stuff like:
// ConfigKey c = "test";
// string s = c;
public static implicit operator string(ConfigKey c)
{
return c.s;
}
public static implicit operator ConfigKey(string s)
{
return new ConfigKey(s);
}
}
public class ConfigSection : Dictionary<ConfigKey, ConfigValue>
{
}
但这当然是小题大做了,除非你还有其他理由想要制作具体的类。