Posts Tagged ‘Locals’

Promoting locals and constant value expressions into parameters

July 31st, 2012 Comments off

There are times when you realize that a local variable or a field reference within a method would be more useful if it was a parameter. Having a new parameter on a method will increase its flexibility for consumers. To convert the local variable, you should remove its declaration from the body of the method, add it as a parameter and replace all occurrences of a local to a new parameter. The same steps must be performed in the case of field references. Furthermore, all method references in the entire solution should be updated to pass a new value as an argument to a method with a new signature.

Read more…

Declaring locals using CodeRush consume-first declaration features

December 16th, 2011 Comments off

Local variable declarations are the most often used types of declarations in code. A local variable is a type of variable declared by local variable declaration inside a block the variable is intended to be local to. The local variable declaration explicitly defines the type of the variable that has been declared along with the identifier that names the variable. You can also declare implicitly typed local variables, whose type is inferred by the compiler from the expression.

Read more…

Refactorings for implicitly-typed local variables

September 27th, 2011 4 comments

Implicitly-typed local variables are variables declared without specifying the type explicitly. The type of such variables is being inferred from the expression that is used to initialize the variable at the time the code is compiled. Implicitly-typed variables are really useful for LINQ that creates anonymous types in queries, and for which you want to assign variables. However, implicitly-typed locals can be used with any variable declaration to enhance the readability, for example:

Refactor! Implicit variable sample

Read more…

Code Issues – Undisposed local

July 13th, 2011 Comments off

The Undisposed local code issue of the warning type highlights local variables that implement the System.IDisposable interface and are not explicitly disposed. The IDisposable interface was designed to provide a standard way to release unmanaged resources by calling its Dispose method. If the object is IDisposable, it is a good idea to dispose of it when you no longer need it, especially if the object uses unmanaged resources. These are resources that the .NET garbage collector does not manage on our behalf and is unable to clean-up automatically. They include items such as streams, files, database connections, handles and other operating system objects. If the memory and system resources that they use are not properly released, a program may suffer from memory leaks or problems due to locked resources.

Read more…