带 ref 参数的 C# 静态方法 - 好主意吗?
我最近重构了一些代码,现在有一个静态实用程序类,其方法如下:
const int x = 1;
public static string doWork(ref DataTable dt)
{
decimal total = 0;
foreach (DataRow row in dt.Select("COST_ID = " + x))
{
decimal annual = decimal.Parse(row["Cost"].ToString());
total += decimal.Round(annual, 2);
}
return String.Format("{0:C}", total);
}
为了清楚起见,我简化了示例...
我是否可能会遇到这样做并在其中调用 doWork 方法的任何不良影响? ASP.NET 应用程序的代码隐藏受到许多用户的攻击?有人知道或有参考资料,我可以在其中阅读有关静态方法在性能方面如何工作的信息吗?这会成为某种瓶颈吗?
编辑:
是的,我很抱歉这不是一个很好的例子,所以让我们说更多这样的话:
const int x = 1;
public static string doWork(ref DataTable dt)
{
foreach (DataRow row in dt.Select("COST_ID = " + x))
{
row["Cost"] = 0.0;
}
}
你是说A)我实际上甚至不需要这里的ref,因为Datatable已经由ref传递了,B) “集中”对单个静态方法的所有调用根本不会影响性能。
I recently refactored some code and now have a static utility class with a method like so:
const int x = 1;
public static string doWork(ref DataTable dt)
{
decimal total = 0;
foreach (DataRow row in dt.Select("COST_ID = " + x))
{
decimal annual = decimal.Parse(row["Cost"].ToString());
total += decimal.Round(annual, 2);
}
return String.Format("{0:C}", total);
}
I've simplified the example for clarity...
Am I likely to experience any ill effects of doing this and putting a call to the doWork method in the code behind of an ASP.NET application being hit by many users? Anyone know or have a reference where I can read up on how, performance-wise, the static method will work? Does this become a bottleneck of any kind?
EDIT:
Yes I apologize this was not a very good example, so lets say something more like this:
const int x = 1;
public static string doWork(ref DataTable dt)
{
foreach (DataRow row in dt.Select("COST_ID = " + x))
{
row["Cost"] = 0.0;
}
}
You're saying that A) I don't actually even need the ref here, since Datatable is already passed by ref and B) The performance is not hit at all by "funneling" all calls to the single static method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您唯一需要通过 ref 传递引用类型(例如 Datatable)的情况是您计划将参数分配给类的新实例。在这种情况下不需要 Ref。按值传递。
The only time you ever need to pass a reference type (such as Datatable) by ref is if you plan on assigning the parameter to a new instance of a class. Ref is not needed in this case. Pass it by value.
ref
关键字不用于性能目的。 当您想要更改时使用另一个作用域中的变量指向(简单来说)。在此实例中使用ref
是无关的,并且可能会导致以下问题:未来。我对
ref
的经验法则是:如果您正在使用它,那么您可能不应该使用它。最后,回答您有关性能的问题:使用
ref
不会改变现有方法的性能范围。阅读您的编辑后,以下是您两个问题的直接答案:
ref
只会引起混乱,因为这不是它的预期用途(并且它不用于性能)。const
ID 变量的static
方法不太可能以任何可衡量的方式提高您的场景的性能。The
ref
keyword is not used for performance purposes. It is used when you would like to alter what a variable in another scope points-to (in simple terms). Your use ofref
in this instance is extraneous, and likely to lead to problems in the future.My rule of thumb for
ref
is: if you are using it, you probably shouldn't be.Finally, to answer your question about performance: using
ref
will not change the performance envelope of the method on hand.After reading your edit, here are direct answers to your two questions:
ref
is only going to cause confusion as this is not its intended usage (and it isn't used for performance).static
method with aconst
ID variable is not likely to improve performance in any measurable fashion for your scenario.AFAIK 静态方法并不比实例方法慢。相反,它们实际上可能会稍微快一些,因为您不需要传递隐藏的
this
并可能进行虚拟调用。AFAIK static methods are not slower than the instance ones. On the contrary, they actually may be slightly faster, because you don't need to pass the hidden
this
and possibly do a virtual call.DataTable 已作为引用传递,因此不需要 ref
DataTable is already passed as reference, so ref is not needed
对于您的方法,不需要“ref”关键字。
For your method the "ref" keyword is not required.
DataTable 是通过引用传递的类实例。由于您不更改对对象的引用,因此不需要“ref”关键字。
DataTable is a class instances of what are passed by reference. Since you do not change the reference to an object, the "ref" keyword is not required.
当您使用
ref
时,static
方法并非不会提高性能。有关ref
的更多说明,请参阅此。Not at all the
static
methods will not increase the performance when you useref
. For more clarification aboutref
see This.