为什么 C# 区分大小写?

发布于 2024-07-13 03:08:22 字数 1432 浏览 9 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

把梦留给海 2024-07-20 03:08:22

C# 区分大小写,因为它继承了全部区分大小写的 C 风格语言。 这是从记忆中得出的 MSDN 链接,它对我不起作用现在我无法验证。

我还想指出,这是一个非常有效的用例:

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

是的,您可以在成员变量上使用前缀来解决这个问题,但有些人不喜欢这样做。

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.

残月升风 2024-07-20 03:08:22

他们可能在想“我们不希望人们在一个地方使用 SomeVaRiAbLe,而在另一个地方使用 SoMeVARIABLE。

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

风吹雨成花 2024-07-20 03:08:22

考虑以下伪代码中的变量名:

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

区分大小写允许使用用例来分隔类名和实例的约定; 此类公约在发展世界中并不罕见。

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.

舟遥客 2024-07-20 03:08:22

我认为案例能够传达信息是一个很好的理由。 例如,按照惯例,类名、公共方法和属性按照惯例以大写字母开头。 相反,字段和局部变量以小写字母开头。

使用该语言多年后,我真的很喜欢这一点,当您可以简单地从单词的大小写中读取信息时,代码更容易阅读。

这就是为什么人们有时会这样做,这是有道理的:

Foo foo = new Foo();

我一直这样做,它非常有用。 如果您在像这样的更有用的情况下考虑它:

Image image = Image.LoadFrom(path);

有时将实例称为与类名相同的东西是有意义的,区分它们的唯一方法是大小写。 在 C++ 中,区分大小写变得更加有用,但那是另一回事了。 如果你有兴趣我可以详细说明。

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.

雨夜星沙 2024-07-20 03:08:22

我认为,通过使用命名约定,区分大小写的标识符可以使代码更具可读性,即使没有命名约定,区分大小写所强制执行的一致性也可以确保相同的实体始终以相同的方式编写。

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.

魂牵梦绕锁你心扉 2024-07-20 03:08:22

C# 继承了 C 和 Java 的区分大小写,它试图模仿这一点,以便开发人员更容易转向 C#。

三十年前创建 C 时,可能有一些充分的理由使 C 区分大小写,但事实并非如此。似乎有任何关于原因的记录。 Jeff Atwood 写了一篇很好的文章,提倡区分大小写可能不再有意义

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.

半世蒼涼 2024-07-20 03:08:22

可能是从 C、C++、Java 等复制而来,或者可能故意保持相同,以便与其他语言相似。

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

终陌 2024-07-20 03:08:22

这只是 C# 语言设计团队的品味问题。 我敢打赌这是为了与其他 C 家族语言的共性。 然而,它确实会导致一些糟糕的编程实践,例如私有字段及其关联属性仅在第一个字母的情况下有所不同。

编辑:

为什么这可能被认为是不好的。

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

在私有字段前面加上 _ 或 m 可能会更容易发现。 这不是什么大事,就我个人而言,我仍然会做我刚才所说的不好的事情(所以起诉我!)。

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

撑一把青伞 2024-07-20 03:08:22

对于区分大小写的语言来说,解析也稍微容易一些。 只要没有充分的理由选择不区分大小写的方式,为什么还要麻烦呢?

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?

久而酒知 2024-07-20 03:08:22

从你的角度来看,你以非常有限的方式看待这个问题。 语言设计者必须考虑一系列其他因素:文化原因、与其他语言的兼容性、常见的编码实践等。

所有现代语言都使用区分大小写的功能:哪些不区分大小写?

作为使用 BASIC 多年的人,我已经厌倦了开发人员对同一变量使用不同的情况。 这种事情看起来非常烦人,并且会鼓励草率的编程。 如果你懒得把案件弄对——还有什么是你懒得做的呢?

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?

老街孤人 2024-07-20 03:08:22

我假设您希望看到如下代码:

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

编译时就好像任何大小写变体中的 SomeField 都是相同的变量。

就我个人而言,我认为这是一个坏主意。 它鼓励懒惰和/或粗心。 当事情无论如何都不重要时,为什么要费心去正确地包装它呢? 好吧,当我们这样做时,也许编译器应该允许拼写错误,比如 SoemField++

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

西瑶 2024-07-20 03:08:22


.NET Framework 开发人员指南
大写约定,区分大小写:

存在大小写准则
只是为了使标识符更容易
阅读并认识。 外壳不能
用作避免名字的手段
库元素之间的冲突。

不要假设所有编程
语言区分大小写。 他们是
不是。 名称不能因大小写不同而不同
独自一人。

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.

晨光如昨 2024-07-20 03:08:22

我对为什么区分大小写的最佳猜测是因为 Unix 区分大小写。 C 之父丹尼斯·里奇 (Dennis Ritchie) 也是 Unix 的共同编写者,因此他编写的语言与当时的环境相吻合也是有道理的。 C# 只是从它的祖先那里继承了这一点。 我认为这对微软来说是一个很好的决定,因为 Windows 路径不区分大小写。

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.

去了角落 2024-07-20 03:08:22

命名。 名称很难获得,您不想在谈论类似的事物时必须创建一个新名称,或者必须使用某种符号将它们分开。

此类场景是您要分配给字段的构造函数中的字段或参数的属性。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文