4

I recently refactored some code and now have a static utility class with a method like so:

const int x = 1;
public static string doWork(ref DataTable dt)
{
    decimal total = 0;
    foreach (DataRow row in dt.Select("COST_ID = " + x))
    {
        decimal annual = decimal.Parse(row["Cost"].ToString());
        total += decimal.Round(annual, 2);
    }
    return String.Format("{0:C}", total);
}

I've simplified the example for clarity...

Am I likely to experience any ill effects of doing this and putting a call to the doWork method in the code behind of an ASP.NET application being hit by many users? Anyone know or have a reference where I can read up on how, performance-wise, the static method will work? Does this become a bottleneck of any kind?

EDIT:

Yes I apologize this was not a very good example, so lets say something more like this:

const int x = 1;
public static string doWork(ref DataTable dt)
{
    foreach (DataRow row in dt.Select("COST_ID = " + x))
    {
        row["Cost"] = 0.0;
    }
}

You're saying that A) I don't actually even need the ref here, since Datatable is already passed by ref and B) The performance is not hit at all by "funneling" all calls to the single static method.

mikey
  • 5,090
  • 3
  • 24
  • 27

7 Answers7

8

The only time you ever need to pass a reference type (such as Datatable) by ref is if you plan on assigning the parameter to a new instance of a class. Ref is not needed in this case. Pass it by value.

contactmatt
  • 18,116
  • 40
  • 128
  • 186
6

The ref keyword is not used for performance purposes. It is used when you would like to alter what a variable in another scope points-to (in simple terms). Your use of ref in this instance is extraneous, and likely to lead to problems in the future.

My rule of thumb for ref is: if you are using it, you probably shouldn't be.

Finally, to answer your question about performance: using ref will not change the performance envelope of the method on hand.


After reading your edit, here are direct answers to your two questions:

  1. Correct, using ref is only going to cause confusion as this is not its intended usage (and it isn't used for performance).
  2. Correct, using a static method with a const ID variable is not likely to improve performance in any measurable fashion for your scenario.
Community
  • 1
  • 1
user7116
  • 63,008
  • 17
  • 141
  • 172
2

AFAIK static methods are not slower than the instance ones. On the contrary, they actually may be slightly faster, because you don't need to pass the hidden this and possibly do a virtual call.

Vlad
  • 35,022
  • 6
  • 77
  • 199
  • 3
    ...and unless you're writing device drivers, **It Doesn't Matter**. I would never micro-optimize at that level. – TrueWill Jun 14 '11 at 14:59
1

For your method the "ref" keyword is not required.

Eric Petroelje
  • 59,820
  • 9
  • 127
  • 177
TcKs
  • 25,849
  • 11
  • 66
  • 104
1

DataTable is already passed as reference, so ref is not needed

Denis Biondic
  • 7,943
  • 5
  • 48
  • 79
  • Thank you, what is the rule of thumb in determining reference/value passing of various objects? Does it have anything to do with the "classes are reference types" statement in class vs. structs http://msdn.microsoft.com/en-us/library/aa288471%28v=vs.71%29.aspx are all objects default by reference and therefore DataTable is default by reference? – mikey Jun 14 '11 at 15:13
  • I found it, "Reference Types" and "Value Types" very straightforward, thank you everyone all the replies were right on. – mikey Jun 14 '11 at 15:25
1

DataTable is a class instances of what are passed by reference. Since you do not change the reference to an object, the "ref" keyword is not required.

Optillect Team
  • 1,737
  • 2
  • 11
  • 12
0

Not at all the static methods will not increase the performance when you use ref. For more clarification about ref see This.

avirk
  • 3,050
  • 7
  • 38
  • 57