简体   繁体   English

如何忽略/删除ac#“out”变量?

[英]How to neglect/drop a c# “out” variable?

Say I have been provided a function that looks something like this: 假设我已经提供了一个看起来像这样的函数:

int doSomething(int parameter, out someBigMemoryClass uselessOutput) {...}

I want to use this function but I don't need the uselessOutput variable at all. 我想使用这个函数,但我根本不需要uselessOutput变量。 How can I get the function to work without using the uselessOutput , preferably without allocating any new memory? 如何在不使用uselessOutput情况下使函数工作,最好不分配任何新内存?

Simply put - you can't. 简单地说 - 你不能。 Either pass a variable and ignore the out afterwards ( if the memory hit from allocating the out parameter is tiny, ie, not a big memory class) or edit the code for the function yourself. 要么传递一个变量,要么忽略out如果从分配out参数得到的内存很小,即不是一个大的内存类),或者自己编辑该函数的代码。

One way I thought of just now actually is to wrap the function too, if its annoying having to pass an ignored out variable all the time. 我认为刚才实际上的一种方式是包裹功能也一样,如果它的恼人不得不通过一个被忽略out的变量所有的时间。 It doesn't get rid of the out for the original function, but it provides us a way to call the function without caring about the out variable at all. 它没有摆脱原始函数的out ,但它为我们提供了一种调用函数的方法,而无需关心out变量。

class OutExample
{
    static void Method(out int i)
    {
        i = 44;
    }
    static void MethodWrapper() 
    {
        int i = 0;
        Method(out i);
    }
    static void Main()
    {
        int value;
        Method(out value);
        // value is now 44

        MethodWrapper();
        // No value needed to be passed - function is wrapped. Method is still called within MethodWrapper, however.
    }
}

This, however, doesn't solve your big memory class issue, should you call this a lot of times. 但是,如果你多次调用它,这并不能解决你的大内存类问题。 For that, you would need to rewrite the function. 为此,您需要重写该功能。 Calling it within the wrapper function would still require the same memory allocation unfortunately. 不幸的是,在包装器函数内调用它仍然需要相同的内存分配。

There is no way to simply omit an out parameter, but you can make your life a little more comfortable by wrapping your method in an extension method that doesn't have the out parameter: 没有办法简单地省略out参数,但是通过将方法包装在没有out参数的扩展方法中,可以让您的生活更舒适:

// let's assume the method in question is defined on type `Foo`:
static class FooExtensions
{
    public static int doSomething(this Foo foo, int parameter)
    {
        someBigMemoryClass _;
        return foo.doSomething(parameter, out _);
    }
}

Then call that extension method instead of the actual instance method: 然后调用该扩展方法而不是实际的实例方法:

Foo foo = …;
int result = foo.doSomething(42);

(And whenever you do want to specify the out parameter, you still can, because the original method is still there.) (每当想指定out参数,你还可以,因为原来的方法还是有的。)

Of course the original method will still produce the unneeded someBigMemoryClass object, which might be a (hopefully short-lived) waste of resources. 当然,原始方法仍然会产生不需要的someBigMemoryClass对象,这可能是(希望是短暂的)浪费资源。 Better to change the original method directly, if that is an option. 如果这是一个选项,最好直接更改原始方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM