Benefits of Passing WebControls by Link

Are there any performance benefits from passing objects such as WebControls by reference? I specifically think about things like validation methods that change the appearance of the control (background-color, CSSClass, etc.) ...

+3
source share
4 answers

Nope. The only advantage of passing a variable of a reference type by reference is if you want to change the value of the variable of the caller, that is, change the object to which it refers. For instance:

// 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 refwhere possible: it tends to indicate that the method does too much.

+7

Button btn = new Button();

btn - , . , , , :

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

, pass-by-value, . , ., , , , , .

, -

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

, , .

, ref, , , . , ReplaceButtonWithBold ref, .

, ref, - , . , ref ,

+2

, .

, , .

+1

, - , , , :

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);
}

( ):

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

, . , . , . , , ref, , . , : , ~ 100 .

, ref .

+1

Source: https://habr.com/ru/post/1777924/


All Articles