为什么System.Math拥有的extern方法SIN,COS等?方法、Math、System、extern

2023-09-04 05:16:03 作者:宁静的分手夜

[MethodImpl(MethodImplOptions.InternalCall)]
public static extern double Sin(double a);

什么原因呢?

What are the reasons for this?

推荐答案

MethodImplOptions.InternalCall 表示该方法是由公共语言运行库本地实现的。这是有道理的数学运算,因为他们通常是严重目标平台进行了优化。例如,在x86架构,有一个单一的指令,计算正弦和余弦值。被管理的实现是不可能的,以便能够直接利用这样的指令

MethodImplOptions.InternalCall means the method is implemented natively by the common language runtime. It makes sense for mathematical operations as they are usually heavily optimized for the target platform. For instance, in x86 architecture, there is a single instruction that computes sine and cosine. A managed implementation is unlikely to be able to directly utilize such instructions.