可以参考数学在C#中会缩短? [英] Can Math references be shortened in C#?

查看:185
本文介绍了可以参考数学在C#中会缩短?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VB.NET,我可以让我的数学代码很多清洁通过导入System.Math和直接引用其方法:

In VB.NET, I could make my math code a lot cleaner by Importing System.Math and referencing its methods directly:

Imports System.Math
[...]
Return New Vector3(Sin(az) * Cos(el), Cos(az) * Cos(el), Sin(el))

但我不认为C#可以以隐式访问它们的方法使用类,所以我必须做的是这样的:

But I don't think C# can Use classes in order to access their methods implicitly, so I have to do something like:

using System;
[...]
return new Vector3(Math.Sin(az) * Math.Cos(el), Math.Cos(az) * Math.Cos(el), Math.Sin(el));



但是,这是丑陋的;它需要自己的滚动条!有什么办法来写C#中的东西,看起来像我的VB.NET代码?我可以写的正弦和余弦当地的包装方法,但不会减少的性能(因为函数调用的开销)?而且,这需要编写的包装功能,我用我使用他们在每类中的每个数学函数;这并不尽如人意无论是。

But that's ugly; it needs its own scroll bar!. Is there any way to write something in C# that looks like my VB.NET code? I could write local wrapper methods for Sin and Cos, but wouldn't that reduce performance (because of the overhead of function calls)? And, that would require writing wrapper functions for every Math function I'm using in every class I'm using them in; that's not so desirable either.

推荐答案

您可以给 System.Math 与使用指令别名:

You could give System.Math an alias with the using directive:

using M = System.Math;

return new Vector3(M.Sin(az) * M.Cos(el), M.Cos(az) * M.Cos(el), M.Sin(el));

这是我得到的最好的。

我可以写正弦和余弦当地的包装方法,但不会减少的性能(因为函数调用的开销)?

I could write local wrapper methods for Sin and Cos, but wouldn't that reduce performance (because of the overhead of function calls)?

您可以,在这一点上,你可以使用泛型和其他细微,使之更加方便,但你仍然会引用这样一个图书馆,除非所有的数学是在同一个班级发生其中,该代码正在发生的方法。

You could, and at that point you could use generics and other niceties to make it even more convenient, but you would still be referencing a library like this, unless all the math was happening in the same class where this code is occuring as methods.

的头顶性能问题是回答的东西,更多的堆栈帧这个简单的是非明显的标准应用。如果你是在一个紧凑的循环,那么,这将是一个问题。

The question of "performance from overhead" is answered with "additional stack frames for something this simple are non-noticeable for standard applications". If you were in a tight loop, then yes, that would be an issue.

这篇关于可以参考数学在C#中会缩短?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆