views:

1966

answers:

13

What reasoning exists behind making C# case sensitive?

I'm considering switching from VB.NET to take advantage of some language features (CCR and yield), and understanding the reasoning behind this difference may make the transition easier.

[UPDATE] Well I took the plunge three days ago. Learning C# hasn't been particularly hard, I could barely remember my C++ days in the late 90's though.

Is the Case Sensitivity annoying me? not as much as i'd thought... plus I am finding that it actually is advantageous. I'm actually really happy with the CCR as a asynchronous coordination programming model. If only I had more time on the current project i'd port the code base into C# to take full advantage. Wouldn't be fair to my client though.

Assessing my current project now and I'm seeing blocking threads EVERYWHERE! AHhhh!!!

[UPDATE]

Well i've been programming in C# for nearly a year now. I'm really enjoying the language, and I really REALLY hate crossing over to VB (especially when it is unavoidable!)

And the case sensitivity thing? not even an issue

+1  A: 

Probably copied from C, C++, Java, etc. or may be kept the same on purpose so that its similar to what other langauges have.

Bhushan
+1  A: 

They were probably thinking "we don't want people using SoMeVaRiAbLe in one place and sOmEvArIaBlE in another.

Spencer Ruport
VB.net automatically corrects your case usage to reflect the original which provides a much better consistancy
Harry
Um, no. VB.net doesn't do anything of the sort -- your IDE does.
Cody Brocious
ok fair enough :) hard to split IDE and Language sometimes. The Language might not be doing it, but my 'experience' (IDE) of the language is this
Harry
Well then you're going to have the same experience with C# or any other language in Visual Studio.
Cody Brocious
The fact that the compiler allows it means that it will turn up somewhere despite decent IDEs.Or, you can just take a look at ASP scripts.
Spencer Ruport
+10  A: 

C# is case sensistive because it takes after the C style languages which are all case sensitive. This is from memory here's an MSDN link which is not working for me right now I can't verify.

I would also like to point out that this is a very valid use case:

public class Child
{
   private Person parent;
   public Person Parent
   {
      get { return parent;}
   }
}

Yes you can get around this using prefixes on your member variables but some people don't like to do that.

JoshBerke
The Reason is Inheritance? Seriously?
Harry
Who said anything about Inheritance? You have a property called Parent on a class with a private field called parent. This is not inheritance. I used Child Parent as in a kid and their Parent. Guess sleeping next to my kids gave me that as my sample.
JoshBerke
"C# is case sensistive because it takes after the C style languages"language inheritance
Harry
JoshBerke
You don't have variable-length bloat. aVar is different from AVar is different from AVAR.
Calyth
It is bad practice to use names that vary only by casing. See my answer to this question.
Tom A
@Tom I agree, I prefer to prefix my member variables and not have things differ by case, although I will have a parameter to a method be all lower case for example when a constructor is settings properties on the object.
JoshBerke
@Josh: Yes, that makes sense. The Visual Studio refactor function called Encapsulate makes this very easy to use. Define a member field named "_stuff" and Encapsulate creates a property named "Stuff" for it.
Tom A
@Tom: AFAIK it's only for public members. It's ok to have local variables that differ by casing, not that it's a good thing.
Joan Venge
+2  A: 

I think that having case sensitive identifiers can make code more readable, through the use of naming conventions, well, and even without naming conventions, the consistency enforced by case sensitivity ensures you that the same entity is always written the same way.

CMS
And are you happy with this reason?
Harry
+1  A: 

Parsing is also a tiny bit easier for case-sensitive languages. As long as there's no good reason to choose the non-case-sensitive way, why bother?

Eduard - Gabriel Munteanu
No it's not. A name is a name is a name is a name. The only time this would come into play is when determining references inside the compiler. The parsing stage is 100% identical.
Cody Brocious
Couldn't the compiler just go source.tolower? (well I obviously don't know the syntacs or whatever)
Harry
Well, you've answered your doubts yourselves. When the language is case-sensitive, no more provisions have to be taken. (I remember I said "a tiny bit".)
Eduard - Gabriel Munteanu
Yes they could, but then you lose case in error messages. This reason simply isn't valid, despite case sensitivity being a Good Thing (TM).
Cody Brocious
I have clarified my standpoint in the answer. Relevant reason or not, it was meant as a completion for other answers, as in "doing it case-sensitive does make things a bit easier, so why bother the other way around?"
Eduard - Gabriel Munteanu
+1  A: 

It was just a matter of taste on the C# langauge designer team. I am willing to bet it was for comonality with other C family languages. It does however lead to some bad programming practices such as a private field and its associalted property differing only in the case of the first letter.

EDIT:

Why might this be cosidered bad.

class SomeClass 
{
    private int someField;

    public int SomeField
    {
        get { return SomeField; } 
        // now we have recursion where its not wanted and its 
        // difficult for the eye to pick out and results in a
        // StackOverflowException.
    }
}

Prefixing private fields with an _ or an m might make it easier to spot. Its not a huge biggie and personally I sill do exactly what I have just said is bad (so sue me!).

Tim
You say that's a bad programming practice, but I find it makes the code pretty clear without needing extra prefixes. Why do you think it's bad?
Jon Skeet
Updated answer to clarify myself.
Tim
A: 

You are looking at this in a very limited way - from your point of view. Language designers must take into account a whole other set of considerations: cultural reasons, compatibility with other languages, common coding practices etc

All modern languages use case-sensitivity: which ones don't?

As someone who used BASIC for a number of years I got very tired of developers using different cases for the same variable. This sort of thing is very tiresome to look at and encourages sloppy programming. If you can't be bothered to get the case right - what else can't you be bothered to do?

Fortyrunner
Easily fixed by the IDE
Harry
The IDE isn't the language. For instance: I program Java in a couple of IDEs, sometimes I use Notepad or Vi (neither of which support auto-correction). I'd like to think thatt one day we'll get a competing IDE for .NET languages as well!
Fortyrunner
Well is the language case sensitive or the compiler?
Harry
The language is a specification and mandates what the compiler should accept. So the compiler must process the language specification - which dictates case-sensitivity. Not sure why the compiler/language question is important
Fortyrunner
+1  A: 

C# inherits the case sensitivity from C and Java, which it tries to mimic to make it easier for developers to move to C#

There might have been some good reasons for making C case sensitive when it was created three decades ago, but there don't seem to be any records on why. Jeff Atwood wrote a good article advocating for that case sensitivity might no longer make sense.

Jonas Pegerfalk
Does anyone else concur?
Harry
+2  A: 

Consider the variable names in the following pseudocode:

class Foo extends Object { ... }
...
foo = new Foo();

Having case sensitivity allows conventions which use case to separate class names and instances; such conventions are not at all uncommon in the development world.

Charles Duffy
Yeah I do this one all the time!
Ray Hidayat
+1  A: 

I think the fact that case can convey information is a very good reason. For example, by convention, class names, public methods and properties start with an uppercase letter by convention. And conversely, fields and local variables start with a lowercase letter.

After using the language for years, I've come to really enjoy this, code is much easier to read when you can read information simply from the casing of the words.

And that's why people do this sometimes, and it can make sense:

Foo foo = new Foo();

I do that all the time, it's very useful. If you think about it in a more useful situation like this:

Image image = Image.LoadFrom(path);

It just makes sense sometimes to call the instance the same thing as the class name, and the only way to tell them apart is the casing. In C++ the case-sensitivity becomes even more useful, but that's another story. I can elaborate if you're interested.

Ray Hidayat
A: 

I assume you'd like to see code like:

SomeField=28;
someField++;
somefield++;
SOMEFIELD++;

compiled as if SomeField in any casing variation is the same variable.

Personally, I think it's a bad idea. It encourages laziness and/or carelessness. Why bother properly casing the thing when it doesn't matter anyway? Well, while we're at it, maybe the compiler should allow misspellings, like SoemField++?

Marc Bernier
no, the bad idea is to write that code, but if you write it, it is a god idea for someField and SomeField to be the same. That is my opinion, don't kill me.
csmba
Marc, you've obviously never programmed in VB .NET. The IDE corrects case as you type. So if you have `Dim myCustomer As New Customer` and then you type `mycustomer.age = 5` in the next line, the IDE corrects it to `myCustomer.Age = 5` automatically. This is what C# often can't do, though in VS 2008 it's gotten better about guessing correctly.
Kyralessa
A: 

Naming. Names are hard to come by and you don't want to have to create a new name when talking about something similar or have to use some notation to set them apart.

Such scenarios are properties for fields or arguments in the constructor that you are about to assign to fields.

Quibblesome
A: 

From .NET Framework Developer's Guide Capitalization Conventions, Case-Sensitivity:

The capitalization guidelines exist solely to make identifiers easier to read and recognize. Casing cannot be used as a means of avoiding name collisions between library elements.

Do not assume that all programming languages are case-sensitive. They are not. Names cannot differ by case alone.

Tom A
Yes for all public items but internal items to the class can differ by case.
JoshBerke