Making Your Code CLS Compliant |   | | | |  | ADVERTISEMENT
At the virtual BlackBerry Technical Seminar 2008, you can ask your development questions directly of Research In Motion® (RIM) experts, and take advantage of learning opportunities designed uniquely for BlackBerry solution developers. Register Today! | |  | | |  |
Is your C# code in line with the .NET Command Language Specification (CLS)? In this article, Sriram shows us how to check and fix our code for compliance. Introduction
If you are writing .Net classes, which will be used by other .Net classes irrespective of the language they are implemented, then your code should conform to the CLS [Common Language Specification]. This means that your class should only expose features that are common across all .Net languages. The following are the basic rules that should be followed when writing a CLS complaint C# code.
1. Unsigned types should not be part of the public interface of the class. What this means is public fields should not have unsigned types like uint or ulong, public methods should not return unsigned types, parameters passed to public function should not have unsigned types. However unsigned types can be part of private members.
2. Unsafe types like pointers should not be used with public members. However they can be used with private members.
3. Class names and member names should not differ only based on their case. For example we cannot have two methods named MyMethod and MYMETHOD.
4. Only properties and methods may be overloaded, Operators should not be overloaded.
The above-mentioned rules should be followed only for the types and member that are publicly exposed by your program. Private classes, private methods and local variables need to follow the rules.
By default the C# complier does not check for CLS compliance of the code. We should explicitly make the C# compiler check for CLS compliance by using the CLSCompliantAttribute class. By specifying the value of true to this attribute we specify that the C# compiler should check for the CLS compliance of the code. The CLSCompliantAttribute can be applied to assemblies, modules, types, and members.
For marking an entire assembly as CLS compliant the following syntax is used
using System; [assembly:CLSCompliant(true)]
For marking a particular method as CLS compliant the following syntax is used
[CLSCompliant(true)] public void MyMethod()
If you mark an assembly as CLSCompliant and if any of the publicly exposed types of members are not CLS compliant then the compiler would raise an error as shown in the following example.
Example
using System; //setting CLSCompliant attribute to true [assembly:CLSCompliant(true)] [CLSCompliant(true)] public class Test { public void MyMethod() { } //error because methods differ only in their case public void MYMETHOD() { } static void Main() { } }
Compiling the above code will result in the following error:
error CS3005: Identifier 'Test.MYMETHOD()' differing only in case is not CLS-compliant
A program section cannot be marked as CLScompliant if the section, which encloses it, is not marked as CLScompliant. For example, a class cannot be marked as CLSCompliant if the assembly is not marked for CLScompliant.
Example
using System; //setting CLSCompliant attribute to true [CLSCompliant(true)] public class Test { public void MyMethod() { } //error because methods differ only in their case public void MYMETHOD() { } static void Main() { } }
Compiling the above code will result in the following error:
'Test' cannot be marked as CLS compliant because the assembly is not marked as compliant
You can also mark particular section of a CLS compliant class as non-CLS compliant by setting the CLSCompliantAttribute to false for the particular section. The compiler will not enforce CLS compliance for sections, which are marked as non-CLS compliant.
Example
using System; //setting CLSCompliant attribute to true [assembly:CLSCompliant(true)] [CLSCompliant(true)] public class Test { public void MyMethod() { } //Not an error because the methods is not CLS complaint [CLSCompliant(false)] public void MYMETHOD() { } static void Main() { } }
Compiling the above code will not result in an error, as the method is marked as non-CLS compliant.
Following are the some situations which may cause the C# compiler to raise an error
1. If your class methods that are identical but differ only by ‘out’ or ‘ref’.
2. If a publicly exposed member starts with an underscore (_).
3. If an abstract member is marked for non-CLS compliance within a CLS compliant class.
Conclusion
If you develop applications that conform to the CLS, your program will be interoperable with a wide range of .Net supported programming languages. Therefore, your application is likely to have a wider customer base than a non-CLS-compliant version of your application. You can make sure that the C# programs you develop are CLS compliant by using the CLSCompliantAttribute, which will cause the compiler to raise errors if the program is not CLS compliant.| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More C# Articles More By Sriram Vaideeswaran developerWorks - FREE Tools! | As businesses grow increasingly dependent upon Web applications to provide services to customers, employees and partners, these complex applications become more difficult to secure. Although traditional security solutions protect Internet infrastructure layers, they do not guard against HTTP and HTML attacks. Many organizations that conduct security testing still deploy applications that allow attackers to manipulate their logic and wreak havoc on their business. To mitigate this risk, development and delivery teams must address Web application security throughout the lifecycle, addressing the many layers detailed in this paper. FREE! Go There Now!
| | | | Visit IBM developerWorks to download a free trial version of Lotus Quickr 8.0, which enables collaboration by transforming the way everyday business content such as documents, rich media, photos, and video can be shared. Lotus Quickr makes it faster and easier to share content of all types (not just documents) within virtual teams. It is designed to make it easier to collaborate across organizational boundaries, while continuing to work within the context of familiar desktop applications. FREE! Go There Now!
| | | | Visit IBM developerWorks to download a free trial of the Rational Host Access Transformation Services (HATS) Toolkit. The HATS toolkit provides a set of plug-ins for the IBM Rational Software Delivery Platform to help you easily extend your legacy applications. HATS makes your 3270 and 5250 applications available as HTML through the most popular Web browsers, while converting your host screens to a Web look and feel and it also enables you to develop new Web, portal, and rich-client applications. FREE! Go There Now!
| | | | Analysts, architects, and developers who have existing COBOL or PL/I skills and want to extend those skills to deploy new workloads on the mainframe can use the IBM Enterprise Modernization Sandbox for System z to find hands-on walkthroughs of common real world scenarios. The scenarios provide examples of how to rapidly design, create, assemble, test, and deploy high-quality Web, Web services, portal, and SOA applications for IBM CICS, IBM IMS, and IBM WebSphere Application Server. FREE! Go There Now!
| | | | Learn how to do more with your reusable assets with the free Rational Asset Manager eKit. The eKit includes demos on how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse. Plus you’ll find white papers and a Webcast that discuss the challenges of a Service Oriented Architecture and how Rational Asset Manager can provide quick and effective solutions. FREE! Go There Now!
| | | | Join this Rational Talks to You teleconference on December 4 at 1:00 pm ET to discuss how Rational Method Composer can help meet your compliance objectives. Get your questions answered! FREE! Go There Now!
| | | | This paper is about the critical role that a discipline called integrated requirements management can play in helping to ensure that your business goals and IT investments are continuously aligned—whether you are sourcing, integrating, building or maintaining software. It also looks at ways that automated IBM Rational® products can work together to help you use requirements in the very best way. FREE! Go There Now!
| | | | Try the latest version of IBM Rational Manual Tester V7.0.1 by downloading a free trial from IBM developerWorks. This manual test authoring and execution tool promotes test step reuse to reduce the impact of software change on testers and business analysts and addresses the needs of teams performing at least a portion of their testing manually. FREE! Go There Now!
| | | | Get a free trial download of the latest version of IBM Rational Method Composer V7.2 which helps you deliver customized yet consistent process guidance to your project teams and IT organization, and includes the latest version of IBM Rational Unified Process (RUP), which has provided process guidance to teams since 1996. FREE! Go There Now!
| | | | Visit IBM developerWorks to try the IBM SOA Sandbox for connectivity. The SOA Sandbox for connectivity provides a trial environment with the tooling and components to help you explore how to effectively connect your infrastructure and integrate all of the people, processes and information in your company. Use the hosted sandbox to explore SOA techniques that streamline connecting existing IT assets together, as well as learn how to connect them to new business logic. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |
| | | | | | | |  | | | |