开发者

Declaring reused variables at class or method level when they're required

开发者 https://www.devze.com 2022-12-08 06:02 出处:网络
Does RAII apply to C#? More specifically, when declaring variables, is there any difference internally between declaring a variable used in different methods at the class level (what I do but not thre

Does RAII apply to C#? More specifically, when declaring variables, is there any difference internally between declaring a variable used in different methods at the class level (what I do but not thread safe) or at each method level where it is declared/instantiated several times as soon as it is needed and then goes out of scope?

For example, a SqlConnection object may be used in different methods of a class - should it be declared at the top/class level or separately in each method?

class Test
{
   SqlConnection conn;
   ..开发者_如何学JAVA..

Instantiated in a using block and then goes out of scope in several different methods.

Or...

public void A()
{
  SqlConnection conn = new SqlConnection();
}

And likewise for several methods.

Thanks


It should be declared in the right place.

If it's not actually shared between methods, it should be internal to the method - regardless of whether 20 methods need connection objects. You don't make a single lp variable in your class just because several methods need to do loops (this isn't FORTRAN).

And yes, there is completely a difference internally.


RAII applies differently for memory-managed languages like C#. The Wikipedia article primarily applies to C++. The Java section is more appropriate.

For unmanaged resources in C#, check out the Dispose Pattern.

EDIT: SqlConnection implements IDisposable, so you'll want to do one of the following:

  • Make it local and dispose of it immediately with a using block
  • Make it a field of your class and implement the Dispose Pattern on your class
  • Pass an instance to your class and make the caller responsible for disposing of it
  • Some other technique where some class/method is responsible for disposing of it


There are differences: (as a rule, declare it where you need it)

For reference types: when the conn variable goes out of scope, the sql connection is eligible for garbage collection. So, in the first example, if you don't dispose/null conn when you are finished, you will keep your connection instance around, even if you aren't using it.

For Value types: (ints, structs etc) the local variable is allocated on the stack, but if you declare it at the class level, it is using heap memory.

You could conceivably reduce the efficiency of the garbage collector -- if you allocate a lot of objects at the class level, and keep them around for longer than you need them, then they are more likely to make it to Generation 2. You could (concievably) be in a position where your (inexpensive) gen 0 collections don't satisfy, and the runtime starts performing more gen 2 collections (expensive)

Beyond that, it's just good practice to delcare them where you use them. it makes the code more understandable, and reduces the potential for errors caused by side-effect.

0

精彩评论

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