Code Issues specific to C# and VB language keywords
We are going to review the CodeRush code issues dedicated to C# language limitations for the following keywords: ‘base’, ‘this’, ‘yield’, ‘params’ and ‘Me’, ‘ParamArray’ keywords in Visual Basic. Here’s a brief overview of keywords:
The ‘this’ (C#) or ‘Me’ (VB) keyword is used within a class’ code to refer to the current instantiated object.
By using the ‘base’ keyword, a derived class can access members of the base class. Calling the base class members from within an overridden member can be used to combine functionality.
The ‘yield return’ and ‘yield break’ keywords are used in an iterator block to yield a value to the enumerator object or enumerable object of an iterator or to signal the end of the iteration.
The ‘params’ (C#) or ‘ParamArray’ (VB) keywords specify a parameter array declaration which indicates that any number of parameters of the indicated type may be used in the method call, allowing for optional parameters.
Here are the code issues provided by CodeRush specific to the keywords above.
Keyword this (Me) is not valid in a static member
Cause:
The ‘this’ keyword refers to an object which is an instance of a type. Since static members are independent of any instance of the containing class, the ‘this’ keyword is meaningless and therefore is not allowed.
Sample:
How to fix:
- make the initial member non-static by applying the Make Member Non-static refactoring:
- remove the ‘this’ keyword and use the name of the current class instead:
Keyword base is not valid in a static member
Cause:
This is a similar code issue as above, when static members may not reference members of the base class.
Sample:
How to fix:
- make the initial member non-static by applying the Make Member Non-static refactoring.
- remove the ‘base’ keyword and use the name of the base class instead if appropriate:
- create an instance of the base type and call the required member if possible:
The params parameter must be the last parameter in a formal parameter list
Cause:
The ‘params’ keyword defines an optional array of a variable number of parameters (arguments). There can be only one argument with the ‘params’ keyword, and it must appear at the end of the argument list.
Sample:
How to fix:
- move the ‘params’ parameter to the end of the parameters list;
- remove the ‘params’ parameter modifier.
The params parameter must be a single dimensional array
Cause:
The ‘params’ keyword defines an optional array of a variable number of parameters. The ‘params’ keyword must describe a single-dimension array according to the language specification.
Sample:
How to fix:
- make the ‘params’ parameter a single-dimension array;
- remove the ‘params’ keyword specifier.
Cannot yield in the body of a try block with a catch clause
Cause:
Yield statements are not permitted inside a try block with a clause. This occurs because there are problems implementing the correct behavior of the iterators in the compiler.
Sample:
How to fix:
- move the yield statement outside of the try block.
Cannot yield in the body of a catch clause
Cause:
Yield statements are not permitted inside a catch clause body.
Sample:
How to fix:
- move the yield statement outside of the catch clause body.
Cannot yield in the body of a finally clause
Cause:
Yield statements are not permitted inside a finally clause body.
Sample:
How to fix:
- move the yield statement outside of the finally clause body.