开发者

Benefits Gained by Passing WebControls By Ref

Are there any performance benefits to be gained from passing objects like WebControls by ref? I'm specifica开发者_开发百科lly thinking of such things as validation methods that modify a control's look & feel (background-color, CSSClass etc.) ...


Nope. The only benefit of passing a reference type variable by reference is if you want to be able to change the value of the caller's variable, i.e. change which object it refers to. For example:

// Creates a new label if necessary, and sets the text to Stuff
public void Foo(ref Label label)
{
    if (label == null)
    {
        label = new Label();
    }
    label.Text = "Stuff";
}

Personally I try to avoid ref where possible: it tends to indicate that the method is doing too much.


When you do a

Button btn = new Button();

the btn variable is not the newly created button, instead it's a reference to the button. So if I want to have a method that makes a buttons text bold, this is enough:

public void MakeButtonBold(Button button)
{
  button.Font.Bold = true;
}

because, even if the method uses pass-by-value, I get a copy of the reference to the button. Then when I dereference it using the . operator, the object I get is the same, since both the reference outside the method and it's copy within the method point to the same object.

However if I do something like this

public void ReplaceButtonWithBold(Button button)
{
  button = new Button();
  button.Font.Bold = true;
}

it wont work, because I discarded the passed reference, when I set it to a new button.

In contrast, when I use a ref parameter, then a reference is created to the variable that is used for the parameter, and that reference is passed to method. So if ReplaceButtonWithBold used a ref parameter, my code would work as intended.

So, the rule of thumb should be only to use ref parameters if you explicitly want to replace the whole passed object with something different, and not when you want to change some properties. From a performance perspective, a normal method needs to make a copy of the reference, and the ref method need to make a new reference to the original reference, so either way you have to use time and memory to create and manage a reference


No, no performance benefits.

As these are already reference types, this is not needed and will only make your code more cumbersome to write and use.


It is highly unlikely for this to ever matter, but below is some code I played with to test this:

public class popo
{
    public int X;
    public int Y;
}

public static bool foo(popo x)
{
    x.X = 10;
    return x.X == x.Y;
}

public static bool foo(ref popo x)
{
    x.X = 10;
    return x.X == x.Y;
}

static void Main(string[] args)
{
    Stopwatch sw = new Stopwatch();
    sw.Stop();
    sw.Reset();
    sw.Start();
    popo pio = new popo();
    bool luka = true;
    for (long i = 0; i < 100000000; ++i)
    {
        luka = luka ^ foo(pio);
    }
    sw.Stop();
    Trace.WriteLine(sw.ElapsedMilliseconds);
}

Results (in milliseconds):

Release Val: 948
Release Ref: 1065  
Debug Val: 2451  
Debug Ref: 2550  

Obviously this is not real code. There could be situations where passing a reference type by reference is faster. There are cases where using a faster CPU can slow your application down. Still, this does provide a quick and dirty hint that ref will probably be a little slower. It also demonstrates that it doesn't matter: look how many iterations I needed to get to see even a ~100 ms difference in execution time.

It is highly unlikely that the efficiency of ref vs. value for classes will be relevant to your code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜