简体   繁体   中英

Use of ref and this to change a variable value

I came across ref and this parameter keywords lately, and I can't find any good comparison performance wise.

void main()
{
    int original = 1;

    /* Which one is best? */
    original = DoMaths(original, 2);
    DoMaths(ref original, 2);
    original = original.DoMaths(2);
}

int DoMaths(int original, int arg)
{
    return original * arg;
}

void DoMaths(ref int original, int arg)
{
    original *= arg;
}

int DoMaths(this int original, int arg)
{
    return original * arg;
}

I came across ref and this parameter keywords lately, and I can't find any good comparison performance wise

You found no good comparison because no meaningful comparison is possible.

The this keyword, as used in your code example, has literally no effect on runtime performance. It is strictly a marker signaling to the compiler that the method being declared is an extension method. The code you posted won't even compile (extension methods are required to be static , and they are legal only in a class that is also static ), but when they are declared and used correctly, they affect only the compile-time semantics of the code, allowing you to write the call to the method as if it was an instance method called using the first parameter (marked with this ).

Otherwise, it's exactly like calling any other static method, and has the exact same performance characteristics. And comparing performance between a static method and one using a by-reference parameter (which via ref or out ) is also meaningless. The two are not even mutually exclusive.

Even if you were to conceive of a question comparing the use of ref to some other otherwise-comparable scenario, it is generally impossible for anyone else to answer the question in a useful way, especially without a good, minimal , complete code example that clearly illustrates the question. Even with such an example, a useful answer is unlikely because such a code example is necessarily divorced from the real-world context in which the actual performance is presumably a concern. Ie any answer would be primarily of academic value.

In a forum like this, the best and only reasonable answer to a question that takes the form of "which of these performs better?" is "which performed better when you tested it in your real-world scenario?"

Finally, note that passing by-reference significantly changes the semantics of the method. As such, even where performance is a concern and even if it actually happens that passing by-reference improves performance, one should be extremely wary of using ref solely as a performance-enhancement measure. It is extremely unusual for a single method call to be a significant performance bottleneck, and even less likely that passing by-reference could produce a significant enough difference in performance as compared to a semantically-correct implementation to justify harming the expressiveness of the code in that way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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