跨多个文件在C#中定义类型别名

Mik*_*ley 11 c# typedef using

在C++中,可以很容易地写出以下内容:

#ifdef FAST
typedef Real float;
#endif

#ifdef SLOW
typedef Real double;
#endif

#ifdef SLOWER
typedef Real quad;
#endif
Run Code Online (Sandbox Code Playgroud)

在一些常见的头文件中,所以我可以简单地编写一个版本的代码和#define相应的版本来获得不同的二进制文件.

我知道在C#中你可以做类似的事情:

using Real = double;
Run Code Online (Sandbox Code Playgroud)

这样你就可以获得类似于typedef的语义.但有可能做一些类似于上面的C++代码,我不必在每个文件中写入?

Mar*_*ell 6

如果您希望它使用内置的IL运算符,则不是 - 您必须按文件执行此操作.但是,如果您不需要(我怀疑您这样做),您可以将其封装在struct:

public struct Real {
     private readonly REAL_TYPE value;
     public(REAL_TYPE value) { this.value = value; }
     // TODO add lots of operators (add, multiply. etc) here...
}
Run Code Online (Sandbox Code Playgroud)

(其中REAL_TYPE是声明using单个文件中的别名Real)

为了我的钱,不值得.如果静态操作符的使用速度相对较慢,那么如果它是原位操作则会导致IL操作.