c# Argument is 'ref'而参数被声明为“值”;
public virtual IAsyncResult BeginProcessRequest(RequestContext context, AsyncCallback callback, object state)
{
return this.process.BeginInvoke(**ref context**, callback, state);
}
public virtual RequestContext EndProcessRequest(IAsyncResult result)
{
RequestContext context = null;
this.process.EndInvoke(**ref context**, result);
return context;
}
上述两种方法在我的项目中引起了一些警告。我不确定我是否理解他们。警告是:
参数为“ref”,而参数声明为“value”
,警告的位置是 Invoke 调用中的第一个参数(上下文)。有人认为这有什么问题或者对此问题有一些建议吗?
这些双星号是警告的原因。我在编辑器上点击了“粗体”,它就这么做了,所以我就这么做了。星号不在我的代码中。
public virtual IAsyncResult BeginProcessRequest(RequestContext context, AsyncCallback callback, object state)
{
return this.process.BeginInvoke(**ref context**, callback, state);
}
public virtual RequestContext EndProcessRequest(IAsyncResult result)
{
RequestContext context = null;
this.process.EndInvoke(**ref context**, result);
return context;
}
The two methods above are causing some warnings in my project. I'm not sure I understand them. The warning is:
Argument is 'ref' while parameter is declared as 'value'
and the location of the warning is the first parameter (context) in the Invoke calls. Does anyone see anything wrong with this or have some advice about the issue?
Those double asterisks are the cause of the warnings. I hit "bold" on the editor and it did that so I just went with it. The asterisks are not in my code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在我看来,对委托使用 ref 是一个坏主意。 (老实说,我想说这通常是一个坏主意。让你的方法做一件事并得到一个结果。)
我认为它根本不会起作用 - 但显然它会起作用,只要你提供当您调用
EndInvoke
时,也使用ref
参数:这里的行为可能会令人困惑......如果可能的话,我会避免它。
大多数使用
ref
是由于不了解参数传递在 C# 中的工作原理...有可能是这种情况吗?你真的需要第一个参数是ref
吗?您可以将委托的返回值改为新的上下文吗?Using
ref
with delegates is a bad idea, IMO. (I'd say it's normally a bad idea anyway, to be honest. Make your methods do one thing and have one result.)I didn't think it would work at all - but apparently it does, so long as you supply the
ref
parameter when you callEndInvoke
as well:The behaviour here is potentially confusing... I would avoid it if at all possible.
Most uses of
ref
are due to not understanding how parameter passing works in C#... is it possible that that's the case here? Do you really need the first parameter to beref
? Can you just make the return value of the delegate the new context instead?BeginInvoke
可能不希望有ref
参数。你说的是ref context
,即传递对该对象的引用(本身的引用)。您能确认BeginInvoke
的方法签名吗?BeginInvoke
probably isn't expecting aref
parameter there. Your'e sayingref context
, i.e. pass a reference to that object (a reference in itself). Can you confirm the method signature forBeginInvoke
?我认为您不完全理解
ref
是如何工作的。首先,
ref
(或out
)是方法签名的一部分,因此如果在方法中将参数指定为ref
参数,那么您必须使用ref
,否则您不得使用ref
其次:
ref
不执行任何操作,因为您没有使用context
的新值任何地方。ref
类似于out
参数,只不过它既是“in”又是“out”。普通参数只能被认为是“in”(我正在编造术语“in”)。I think you don't completely understand how
ref
works.First of all,
ref
(orout
) is part of the method signature, so if in the method the parameter is designated asref
parameter then you must useref
otherwise you must not useref
Secondly in:
The
ref
doesn't do anything because you don't use the new value ofcontext
anywhere.ref
is like anout
parameter except that it is both "in" and "out". Normal parameters can be thought of as "in" only (I'm making up the term "in").