Question

Quoted from A Brief Introduction To IL code, CLR, CTS, CLS and JIT In .NET

CLS stands for Common Language Specifications. It is a subset of CTS. CLS is a set of rules or guidelines which if followed ensures that code written in one .NET language can be used by another .NET language. For example one rule is that we cannot have member functions with same name with case difference only i.e we should not have add() and Add(). This may work in C# because it is case-sensitive but if try to use that C# code in VB.NET, it is not possible because VB.NET is not case-sensitive.

Based on above text I want to confirm two points here:

  1. Does the case-sensitivity of IL is a condition for member functions only, and not for member properties?
  2. Is it true that C# wouldn't be inter-operable with VB.NET if it didn't take care of the case sensitivity?
Was it helpful?

Solution

The restriction on case-sensitivity applies to all members (fields, methods, properties) visible to other assemblies. Thus, the following is OK:

[CLSCompliant(true)]
public class C
{
    private int i;
    public int I { get { return i; } set { i = value; } }
}

but the following isn't:

[CLSCompliant(true)]
public class C
{
    public int i;
    public int I { get { return i; } set { i = value; } }
}

and the following isn't either:

[CLSCompliant(true)]
public class C
{
    protected int i;
    public int I { get { return i; } set { i = value; } }
}

since a VB.NET class might want to inherit from class C.


If the second example above were in a C# library and a VB.NET project would try to use it, the following code would not compile:

Dim c As New C()
Console.WriteLine(c.I)

This is the compile-time error that the VB.NET compiler would throw:

'I' is ambiguous because multiple kinds of members with this name exist in class 'C'.

OTHER TIPS

Does the case-sensitivity of IL is a condition for member functions only, and not for member properties?

C# properties are implemented internally as special methods called accessors so the above quote applies to both methods and properties.

Is it true that C# wouldn't be inter-operable with VB.NET if it didn't take care of the case sensitivity?

Theoretically, it may be possible to write C# that is compatible with VB.NET. However, it is not just case sensitivity. For example, the many keywords appear in one language and not the other need special syntax (which C# and VB.NET both provide). Unsigned types cannot be exposed and operators cannot be overloaded. For more details, see https://stackoverflow.com/questions/570452/what-is-the-clscompliant-attribute-in-net.

Rather than manually having to check these, Microsoft created CLS as a way of programmaticly enforcing it. More importantly, it also allows languages other than VB.NET and C# to interoperate through a common runtime, including hose that lack a special syntax for invalid identifiers.

Licensed under: CC-BY-SA with attribution
scroll top