Coding Helpers – Reverse For Loop
When working with arrays and lists enumerating all of its items we usually create a for loop statement as follows:
Code Issues specific to declaring types
The CSharp and Visual Basic programming language specifications have several restrictions on how type elements such as a class, structure, and delegate are declared in the code. Let’s review code issues that demonstrate an incorrect type declaration, and see the error before the compiler informs us about it when we build the code.
Read more…
Reordering method parameters with Visual Studio and CodeRush
Sometimes you may find that various method parameters are ordered illogically, or when method parameters appear in a different order relative to one another, similar methods. In this case, if you prefer the parameters appear in a consistent and logical order, you must not only change the order in the method signature but also update all calls to the method. Take into account complex cases when you can move return values of a function into our parameters and vice versa. Refactoring such complex cases is a rather complex and time-consuming task, especially if there are too many calls.
How to safely rename a public API member and mark it obsolete
There are times when public API members must be deprecated or its signature must be changed. In this case, we can not remove or modify public members, because other application that depend on these APIs may become broken and/or uncompilable. If you’d like to supersede a public member, you can use a special attribute called ‘Obsolete’. The Obsolete attribute marks an API member as one that is no longer recommended for use.
How to create a method or constructor overload using CodeRush
The process of creating more than one method in a class with the same name is called as method overloading. Method overloading allows the developer to define several methods with the same name but with a different set of parameters. This way, one does not have to remember the names of multiple functions that serve a similar core purpose. When a call is made to one of these overloaded methods, the compiler automatically determines which of the methods should be used according to the arguments used in the call.
Promoting locals and constant value expressions into parameters
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.
How to set the default encoding for all files saved in Visual Studio
In Visual Studio, you can use the File -> Advanced Save Options menu item to save the file in the chosen encoding: