Identifiers should differ by more than case (CA1708, Microsoft.Naming)

by Mark Zhou 20. October 2010 21:10

Let’s first take a look at the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using
System.Text;

namespace
Demo
{
   
class Program
    {
       
internal static string name = string
.Empty;

       
public static string Name { get { return
name; } }

       
static void Main(string
[] args)
        {
           
Program.name = Program.Name;
        }
    }
}

This is a pretty simple piece of code: first, I declared an internal, static filed name on the Program type, then I declared a public, static property Name on it. At last, in the Main method, I attempted to assign the value of the Name property to the name field. This code can actually run by press F5 in Visual Studio IDE, works without any problem.

But how, if you are making a class library for all targeted Common Language Specification (CLS) developers? For example, your team uses both C#, F# and Visual Basic (both of which are CLS-compliant languages) in your product; suppose your team were to use C# to develop class libraries for all the other languages, and if you wrote code like the above sample in C# then make it a library, when referenced from other languages, this code may NOT run as expected!

The reason is, in the code, identifiers Name and name only differs by case. In C#, all identifiers are case-sensitive, that is, C# treats the Name and name as two different identifiers; however other languages, like Visual Basic or PowerShell, might be case-insensitive (which will treat Name and name as one identifier). Once you intend to reference a class library that is made by a case-sensitive language in a case-insensitive language which contains any identifier differred each other only by case, it is highly possible that the case-insensitive language treats them as a same identifier and then result the application in an unpredictable state. To explain more, consider the above example again, the code

Program.name = Program.Name;

If running in Visual Basic, a probably StackOverflowException will be thrown. That’s because Visual Basic is case-insensitive and it treats name and Name as one identifier; thus it is possible that the identifier Name represents the Name property so that access the above code will cause an infinite recursion on the property’s getter.

Do not assume the library being developed would only use in C#. So it is wisely to avoid any of the identifier which differs only by case. This can make your class library robust and extensible.

This rule only fires on the public (public, internal, protected) visible members of a type, or public namespaces, types, and method/type parameters.

In any case, do not suppress this rule, this enables your code able to use across other .NET programming languages.

To get more information about this rule, see this MSDN link for CA1708.

Tags: , , ,

Pingbacks and trackbacks (1)+

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading


Translate This Page

About Mark

Mark is a developer who works for building base class libraries and tools for developers.

Mark's Awards

Microsoft Community Contributor

Month List

Who visit this site

Recent Comments

Comment RSS