Introducing C# and the .NET Framework - A World of Interoperability Through the CLS
(Page 8 of 12 )
Since each programming language has its own features and drawbacks, the ability to use more than one programming language in your applications can make our life easier as programmers. Remember our example about using C# for the part of your application that needs I/O and using APL for the part that needs complex mathematical calculations? Unless both languages expose the same data types with the same mechanism, they cannot interoperate. Add to this that Microsoft wanted developers to be able to have the code written in one language used in other languages, and you'll find that you can write a class in Managed C++ and derive from it in C#, and so on.
All this is possible with the Common Type System, but the CTS is huge, and there is no one programming language that supports all the types that exist in the CTS; different languages may expose different types. But all languages require the Boolean data type, the integer data type, the floating point data types and a character string data type. In other words, these data types are the core for any programming language today.
Microsoft's solution is the Common Language Specifications (or the CLS). The CLS is a large subset of the data types and rules of the CTS defined for compilers and language designers. It explains what is necessary in order to implement a .NET Compliant Language that can interoperate with other .NET programming languages and third-party .NET Components. If you are designing a compiler for your language to be managed by the runtime, and to use the .NET Framework Class Library, you need to implement at least the CLS specifications to make your programming language integrate with the .NET world through the CLS. Think of the CLS as the minimum set of functionality and data types that any .NET compliant language will support, and that's exactly how these languages interoperate, through the CLS implementation.
Also note that almost all the classes in the .NET Framework Class Libraries (FCL) are CLS-complaint. You will be able to use these classes from any .NET Compliant language. Actually the expression ".NET Framework" indicates a large set of classes that can be used from any .NET Compliant language. There are some classes and structures in the FCL that are not CLS-Compliant, like uint and ulong data types. Also note that third parties ensure that their .NET Components are CLS-Compliant, so they can be used from any .NET Compliant language like C#, VB.NET, Managed C++ and more than 20 other .NET Compliant languages from many of vendors.
The C# Compiler can help you to check for CLS Compatibility through the use of the next attribute:
[assembly: System.CLSCompliant(true)]
You need to put this line outside your namespace or any class declaration. If you are using Visual Studio.NET, you need to put this line in the AssemblyInfo.cs class. It indicates that you are developing a component that can be used from other .NET Compliant languages. The C# Compiler will issue warning when it finds any violation to the rules of the CLS.
For example, C# is a case sensitive language, and you can define two public fields as DotNet and dotnet; C# will understand that they represent two different fields, but with a non-case sensitive language such as Visual Basic.NET will cause the compiler to issue an error if you try to use DotNet and dotnet as identifiers for two separate fields. If you developed a class that contains these two public fields, how do you use this class in a VB.NET program? You can't; you must check your work using the System.CLSCompliant(true) attribute. The C# Compiler will issue a warning to tell you that your class is not CLS-Compliant; thus you will need to change the names of your public fields.
Note that the CLS Compliance mechanism applies only to the public interface of your classes. The internal implementation is not an issue; the C# compiler will not issue a warning if you use two local variables in a method (X and x) because it's internal to the method.
Some of the CLS Rules imply that, if you want to define a CLS-Compliant class, you must inherit a CLS-Compliant class. They also imply that, when you declare an Array Type, the element type must be a CLS-Compliant type, and the Array index must be zero-based (so all the .NET Compliant languages use the same mechanism). Case sensitivity is an issue to the members of CLS-compliant types to avoid using the same identifier with a different casing.
If you have Microsoft Visual Studio .NET 2003 installed on your computer, and you want to read more about the Rules of the CLS and the Specifications, I advice you to navigate to the folder on your hard drive labeled: \Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Tool Developers Guide\docs and take a look at Partitions I, II and maybe III of the specifications.
We will create your first C# Program (the familiar "Hello World" example) with the C# compiler, then with Visual Studio.NET, so you can imagine how Visual Studio.NET can save you a great deal of time developing C# and .NET applications. We will create a Console-Based application to represent our Hello World example, so let's begin.
Next: Where Can I Find the .NET SDK? >>
More C# Articles
More By Michael Youssef