Question

I just cant seem to understand these pillars of .NET.

Was it helpful?

Solution

CTS (Common Type System) So you can say CTS describes how types are declared, used and managed in the runtime and facilitates cross-language integration, type safety, and high performance code execution.

CLS (Common Language Specification) The Common Language Specification (CLS) is an agreement among language designers and class library designers to use a common subset of basic language features that all languages have to follow.

As you can see in the image CLS is a subset of CTS

alt text http://www.microsoft.com/taiwan/msdn/columns/DoNet/images/NET_CTS1.gif

OTHER TIPS

As already mentioned, CLS is subset of CTS. But there is much more in the actual definition of these terms.

I suggest reading CLR via C#.

Some examples would certainly help here.

One of the key things that is not CLS-compliant is unsigned numbers (e.g., uint). Another is function pointers (delegates). If it's not going to make sense on both ends of the line, then it's not "common"; CLS defines a set of common types that work not only within the CLR, but also within certain common and well-specificed interop scenarios.

If you want to enforce CLS compliance, you can add the "CLSCompliant" attribute to an assembly by adding it to the assemblyinfo.cs file in a project.

[assembly: CLSCompliant(true)]

You can also add it as an attribute on a class.

[CLSCompliant(true)]
public class HospitalLocationEntity : EntityBase
{
    ...
}

Doing these things will cause the C# compiler (or VB, with appropriate VB syntax on the attributes) to raise compile errors for CLS compliance violations.

Also, adding the [ScriptService] and [ScriptMethod] attributes to web services (.asmx) will cause the service to generate JSON service output, and will require that the data used for service responses be marked as CLSCompliant at class and assembly levels.

<System.Web.Services.WebService()> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
<ScriptService()> _
Public Class HospitalLocationService
    Inherits System.Web.Services.WebService
    <WebMethod()> _
    <ScriptMethod()> _
    Public Function GetAll() As List(Of HospitalLocationEntity)
        Return (New HospitalLocation()).GetAll().Data
    End Function
End Class

Common Type System

CTS is a formal specification describing type properties:

  • How types should be laid out in IL.
  • Each type can consist of 0 or more following members: property, field, method and event.
  • Access modifiers (and C# equivalents): private (private), family (protected), family and assembly (N/A), assembly (no modifier/internal), family or assembly (protected internal), public (public)
  • Type inheritance, virtual methods
  • Each type must inherit from System.Object
  • Each language implements only a subset of CTS features.

Common Language Specification

CLS facilitates interoperability between .NET languages at the IL level:

  • It is a compiler specification that determines what IL the compilers must emit in order for the code to be interoperable among .NET languages.
  • CLS describes a common set of functionalities that the program is allowed to use and still be interoperable with programs written in other .NET languages.
  • CLS features are a subset of CTS features.
  • CLS is of importance only if you wish to write your programs in multiple languages.
  • An important thing to note is that CLS must be adhered to only by the pieces of code marked as public, since these are the only ones that are available to other programs.
  • If we annotate the code with [assembly: CLSCompliant(true)] attribute, then the compiler will check if the code is CLS compliant.

For example:

  • CLS states that member names cannot be differentiated by case: thus Foo() and foo() are the same thing.
  • unsigned int cannot be used as it is not implemented by all .NET languages
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top