为什么 C# 区分大小写?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/494561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is C# Case Sensitive?
提问by Andrew Harry
What reasoning exists behind making C# case sensitive?
使 C# 区分大小写背后存在什么推理?
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.
我正在考虑从 VB.NET 切换到利用某些语言功能(CCR 和 yield),并且理解这种差异背后的原因可能会使转换更容易。
[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.
[更新] 好吧,我三天前就冒险了。学习 C# 并不是特别难,但我几乎不记得我在 90 年代后期的 C++ 日子。
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.
区分大小写是否让我烦恼?不像我想象的那么多......而且我发现它实际上是有利的。实际上,我对 CCR 作为异步协调编程模型非常满意。如果我在当前项目上有更多时间,我会将代码库移植到 C# 中以充分利用。不过对我的客户不公平。
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!)
好吧,我已经用 C# 编程近一年了。我真的很喜欢这门语言,而且我真的很讨厌过渡到 VB(尤其是在不可避免的情况下!)
And the case sensitivity thing? not even an issue
区分大小写的事情?甚至不是问题
采纳答案by JoshBerke
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 linkwhich is not working for me right now I can't verify.
C# 区分大小写,因为它采用了所有区分大小写的 C 风格语言。这是来自记忆中的MSDN 链接,它现在对我不起作用,我无法验证。
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.
是的,您可以使用成员变量的前缀来解决这个问题,但有些人不喜欢这样做。
回答by Bhushan Bhangale
Probably copied from C, C++, Java, etc. or may be kept the same on purpose so that its similar to what other langauges have.
可能是从 C、C++、Java 等复制而来的,或者可能有意保持相同,以便与其他语言具有的相似。
回答by Spencer Ruport
They were probably thinking "we don't want people using SoMeVaRiAbLe in one place and sOmEvArIaBlE in another.
他们可能在想“我们不希望人们在一个地方使用某些变量而在另一个地方使用某些变量。
回答by CMS
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.
我认为区分大小写的标识符可以使代码更具可读性,通过使用命名约定,好吧,即使没有命名约定,区分大小写的一致性确保您始终以相同的方式编写相同的实体。
回答by Eduard - Gabriel Munteanu
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?
对于区分大小写的语言,解析也稍微容易一些。只要没有充分的理由选择不区分大小写的方式,何必呢?
回答by Tim
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.
这只是 C# 语言设计团队的品味问题。我敢打赌这是为了与其他 C 家族语言的共性。然而,它确实会导致一些糟糕的编程实践,例如私有字段及其社交属性仅在第一个字母的情况下有所不同。
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!).
用 _ 或 m 前缀私有字段可能会更容易发现。这不是一个大问题,我个人还是会做我刚刚说的不好的事情(所以起诉我!)。
回答by Fortyrunner
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?
作为使用 BASIC 多年的人,我对开发人员对同一个变量使用不同的情况感到非常厌倦。这种事情看起来很烦人,并且鼓励草率的编程。如果您不费心去把案子弄好——还有什么是您不费心去做的?
回答by Jonas Pegerfalk
C# inherits the case sensitivity from C and Java, which it tries to mimic to make it easier for developers to move to C#
C# 继承了 C 和 Java 的大小写敏感性,它试图模仿它们以使开发人员更容易迁移到 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.
在三年前创建 C 时,可能有一些很好的理由使 C 区分大小写,但似乎没有任何关于原因的记录。Jeff Atwood 写了一篇好文章,提倡区分大小写可能不再有意义。
回答by Charles Duffy
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.
区分大小写允许用例分隔类名和实例的约定;此类公约在发展世界中并不少见。
回答by Ray Hidayat
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.
有时将实例称为与类名相同的东西是有道理的,区分它们的唯一方法是大小写。在 C++ 中,区分大小写变得更加有用,但那是另一回事了。如果你有兴趣,我可以详细说明。