GOTCHA 30 Common Language Specification Compliance isn t the default


GOTCHA #30 Common Language Specification Compliance isn't the default

When I first started learning C#, I heard that it has some C++ features that are not in Java. My first thought was templates. I was quite disappointed to find instead that one of the supported features is operator overloading. It's quite complicated in C++ [Stroustrup00, Cline99]; it gives you many ways to shoot yourself in the foot. Having offered courses on C++ for over 12 years, I know the pain people go through just learning operator overloading. Getting it right is another story [Meyers96, Meyers98]. Overloading is supposed to make your code intuitive and easier to understand, but it often has the opposite effect.

Unfortunately, in my opinion, C# took what was complicated in C++ and made it worse. For instance, you can overload the true operator. This requires that you overload the false operator as well. Arbitrarily overloading operators increases the complexity of your code and reduces its readability; its worth is questionable.

Furthermore, not all .NET languages support operator overloading. If you write a C# class with overloaded operators, those operators are not accessible from VB.NET. If you have merely overloaded the operators and have not provided equivalent regular methods, your code is not considered to be Common Language Specification (CLS) compliant. CLS compliance requires you to provide a regular method for each overloaded operator.

CLS Compliance

Proper implementation of overloaded operators (and their equivalent methods) is only one aspect of CLS compliance. If your type is to be fully usable in any .NET language, it must expose only features that are common to all languages. The basic language features needed for applications have been defined in a set of conventions called the Common Language Specification (CLS).

The Common Type System (CTS) supports a rich set of types found in many programming languages. However, not all types supported in the .NET CTS are interoperable or CLS-compliant. In this regard, the CLS rules define a subset of the CTS. By claiming CLS compliance you are indicating that your code follows the CLS rules and restrictions. This affects how you expose all the public and protected members of your public types. However, code that is private, not exposed, or local to your implementation can use any language-specific features. Furthermore, only assemblies marked with the CLSCompliant attribute are considered CLS-compliant.

The Common Language Infrastructure (CLI) is an ECMA-335 standard that defines a runtime environment for applications written in multiple high-level languages to run. For a thorough discussion of these concepts, refer to "ECMA-CLI" in the section "on the web" in the Appendix. Also refer to [Box03].

To make sure your code is CLS-compliant, add [Assembly: System.CLSCompliant(true)] to the AssemblyInfo.cs file of a C# project, or add <Assembly: CLSCompliant(True)> to AssemblyInfo.vb for VB.NET. Then run the assembly through the FxCop tool. (See "FxCop" in the section "on the web" in the Appendix for details about this tool.)


Consider the example of delegates in .NET. Typically a C# programmer would use the += operator to add or register a delegate. In VB.NET, you can realize the same goal using either the Combine() method or the special AddHandler statement. Similarly, the Remove() method is equivalent to the C# -= operator on delegates. Providing regular methods for each overloaded operator allows users of your class to take advantage of the operators in languages that support overloading, while making it fully usable in languages that don't.

If you want your code to be CLS-compliant, then for each operator you must provide a regular method as well. Operator overloading does not reduce the code size. In fact, you will end up writing more methods to accommodate languages that don't support it.

IN A NUTSHELL

Always provide a regular method for each overloaded operator. Mark your assembly with the CLSCompliant attribute if you intend to provide language interoperability. Then use the FxCop tool to confirm that you are CLS-compliant.

SEE ALSO

Gotcha #31, "Optional parameters break interoperability."



    .NET Gotachas
    .NET Gotachas
    ISBN: N/A
    EAN: N/A
    Year: 2005
    Pages: 126

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net