1

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?
gnat
  • 21,442
  • 29
  • 112
  • 288
Khadim Ali
  • 153
  • 1
  • 7
  • 2
    Why dont you try it out? there are actual practical problems that you might encounter, but this doesnt look like one of them. For example: http://stackoverflow.com/questions/12085131/c-sharp-net-assembly-causes-ambiguous-error-when-used-in-vb-net – Dave Hillier Nov 02 '13 at 18:28
  • well, put here for someone already learned to answer. discussion with the learned ones always let me learn something that I do not learn in practicing without theory. – Khadim Ali Nov 03 '13 at 09:33

2 Answers2

2

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'.

Heinzi
  • 9,646
  • 3
  • 46
  • 59
1

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.

akton
  • 6,912
  • 31
  • 34
  • Well, I was surprised to know that C# case-sensitivity could be a problem in interoperability b/w the two. I always had in my conscious that somehow compiler would be emitting the managed code to handle this. Anyways.. In your second response, would you want to say that any keyword that is not shared by both languages would make the code non-interoperable? like if we write `obj = null` in C# it would not be usable in VB.NET because the later understands `obj = Nothing`? – Khadim Ali Nov 03 '13 at 12:32
  • @Ali.NET Keywords are only problematic in identifiers only, such as class and method names. For example, a class called `MustInherit` might be fine in C# might may cause problems in VB.NET. – akton Nov 03 '13 at 20:57
  • 2
    The point about keywords is not true, both C# and VB have a special syntax for using keywords as identifiers (`@keyword` in C#, `[keyword]` in VB). – svick Nov 04 '13 at 11:40
  • @svick Good point. Forgot that. Updated the answer. – akton Nov 04 '13 at 23:12