Home > Code Analysis > Code Issues specific to C# and VB language keywords

Code Issues specific to C# and VB language keywords

May 30th, 2012

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:

CodeRush - Keyword this is not valid in a static member

How to fix:

CodeRush - Make Member Non-static preview

  • remove the ‘this’ keyword and use the name of the current class instead:

CodeRush - Keyword this is not valid in a static member - fix

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:

CodeRush - Keyword base is not valid in a static member

How to fix:

  • remove the ‘base’ keyword and use the name of the base class instead if appropriate:

CodeRush - Keyword base is not valid in a static member - fix#1

  • create an instance of the base type and call the required member if possible:

CodeRush - Keyword base is not valid in a static member - fix#2

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:

CodeRush - The params parameter must be last in a format parameters list

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:

CodeRush - The params parameter must be a single-dimensional array

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:

CodeRush - Cannot yield in the body of a try block with a catch clause

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:

CodeRush - Cannot yield in the body of a catch clause

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:

CodeRush - Cannot yield in the body of a finally clause

How to fix:

  • move the yield statement outside of the finally clause body.
—–
Products: CodeRush Pro
Versions: 12.1 and up
VS IDEs: 2008 and up
Updated: Nov/19/2012
ID: C163

Similar Posts: