c# Argument is 'ref'而参数被声明为“值”;

发布于 2024-11-18 23:35:13 字数 620 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

剪不断理还乱 2024-11-25 23:35:13

在我看来,对委托使用 ref 是一个坏主意。 (老实说,我想说这通常是一个坏主意。让你的方法做一件事并得到一个结果。)

我认为它根本不会起作用 - 但显然它会起作用,只要你提供当您调用 EndInvoke 时,也使用 ref 参数:

using System;

class Program
{
    delegate void Foo(ref int x, string y);

    static void SampleFoo(ref int x, string y)
    {
        Console.WriteLine("Incoming: {0}", x); // 10
        x = y.Length;
    }

    static void Main(string[] args)
    {
        int x = 0;
        int input = 10;
        Foo f = SampleFoo;
        IAsyncResult result = f.BeginInvoke(ref input, "Hello", null, null);
        f.EndInvoke(ref x, result);
        Console.WriteLine("Result: {0}", x); // 5
    }
}

这里的行为可能会令人困惑......如果可能的话,我会避免它。

大多数使用 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 call EndInvoke as well:

using System;

class Program
{
    delegate void Foo(ref int x, string y);

    static void SampleFoo(ref int x, string y)
    {
        Console.WriteLine("Incoming: {0}", x); // 10
        x = y.Length;
    }

    static void Main(string[] args)
    {
        int x = 0;
        int input = 10;
        Foo f = SampleFoo;
        IAsyncResult result = f.BeginInvoke(ref input, "Hello", null, null);
        f.EndInvoke(ref x, result);
        Console.WriteLine("Result: {0}", x); // 5
    }
}

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 be ref? Can you just make the return value of the delegate the new context instead?

情栀口红 2024-11-25 23:35:13

BeginInvoke 可能不希望有 ref 参数。你说的是ref context,即传递对该对象的引用(本身的引用)。您能确认 BeginInvoke 的方法签名吗?

BeginInvoke probably isn't expecting a ref parameter there. Your'e saying ref context, i.e. pass a reference to that object (a reference in itself). Can you confirm the method signature for BeginInvoke?

她说她爱他 2024-11-25 23:35:13

我认为您不完全理解 ref 是如何工作的。

首先,ref(或out)是方法签名的一部分,因此如果在方法中将参数指定为ref参数,那么您必须使用ref,否则您不得使用ref

其次:

public virtual IAsyncResult BeginProcessRequest(RequestContext context, AsyncCallback callback, object state)
{
    return this.process.BeginInvoke(ref context, callback, state);
}

ref不执行任何操作,因为您没有使用 context 的新值任何地方。 ref 类似于 out 参数,只不过它既是“in”又是“out”。普通参数只能被认为是“in”(我正在编造术语“in”)。

I think you don't completely understand how ref works.

First of all, ref (or out) is part of the method signature, so if in the method the parameter is designated as ref parameter then you must use ref otherwise you must not use ref

Secondly in:

public virtual IAsyncResult BeginProcessRequest(RequestContext context, AsyncCallback callback, object state)
{
    return this.process.BeginInvoke(ref context, callback, state);
}

The ref doesn't do anything because you don't use the new value of context anywhere. ref is like an out 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").

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文