Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does C# include programming constructs that are not CLS-compliant?

It seems strange that the flagship language of .NET would include programming constructs that are not CLS-compliant. Why is that?

Example (from here): Two or more public / protected / protected internal members defined with only case difference

public int intA = 0;
public int INTA = 2; 

or

public int x = 0;

public void X()
{
} 
like image 589
richard Avatar asked Feb 16 '11 21:02

richard


People also ask

Why is %d used in C?

%d takes integer value as signed decimal integer i.e. it takes negative values along with positive values but values should be in decimal otherwise it will print garbage value. ( Note: if input is in octal format like:012 then %d will ignore 0 and take input as 12) Consider a following example.

Why do we write in C?

It was mainly developed as a system programming language to write operating system. The main features of C language include low-level access to memory, simple set of keywords, and clean style, these features make C language suitable for system programming like operating system or compiler development.

Why is C not A or B?

Because C comes after B The reason why the language was named “C” by its creator was that it came after B language. Back then, Bell Labs already had a programming language called “B” at their disposal.

Why semicolon is used in C?

Semicolons are end statements in C. The Semicolon tells that the current statement has been terminated and other statements following are new statements. Usage of Semicolon in C will remove ambiguity and confusion while looking at the code.


2 Answers

Even unsigned integers aren't compliant (at least, on the public API), yet they are very important to people who do bit-shifting, in particular when right-shifting (signed and unsigned have different right-shift behaviours).

They are ultimately there to give you the freedom to use them when appropriate. The case-sensitivity one I could get less religious about - despite the convenience of having a field and property differ only by case, I think I could live happily with being forced to use a different name! Especially now we have automatically implemented properties...

This is similar to how it lets you use unsafe - the difference here being that a few unsigned integers aren't going to destabilise the entire runtime, so they don't need quite as strong molly-guards.

You can add:

[assembly: CLSCompliant(true)]

if you like, and the compiler will tell you when you get it wrong.

And finally: most code (by volume) is not consumed as a component. It is written to do a job, and maybe consumed by other code in-house. It is mainly library writers / vendors that need to worry about things like CLS compliance. That is (by the numbers) the minority.

like image 147
Marc Gravell Avatar answered Oct 01 '22 02:10

Marc Gravell


That's not how CLS compliance works. It is something that's your burden. C# doesn't restrain itself to strict compliancy itself, that would make it a language with poor expressivity. Dragging all .NET languages down to the lowest common denominator would have quickly killed the platform as a viable programming environment.

It is up to you to ensure that the publicly visible types in your assembly meet CLS compliancy. Making sure that class members don't differ only by case is very simple to do. Let the compiler help you out by using the [assembly:CLSCompliant(true)] attribute and the compiler will warn you when you slipped.

like image 34
Hans Passant Avatar answered Oct 01 '22 02:10

Hans Passant