Why is C# Case Sensitive? [closed]

怎甘沉沦 提交于 2019-11-26 17:01:58

问题


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:


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.




回答2:


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




回答3:


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.




回答4:


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.




回答5:


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.




回答6:


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.




回答7:


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




回答8:


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!).




回答9:


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?




回答10:


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?




回答11:


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.




回答12:


My best guess as to the why it is case sensitive would be because Unix is case sensitive. Dennis Ritchie, the father of C also co-wrote Unix, so it makes sense that the language he wrote would coincide with the environment available at that time. C# just inherited this from its ancestor. I think this was a good decision on Microsoft's part, being that windows paths are not case sensitive.




回答13:


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++?




回答14:


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.



来源:https://stackoverflow.com/questions/494561/why-is-c-sharp-case-sensitive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!