本文介绍了为什么C#在类/方法级别不支持const?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想知道为什么C#在类或方法级别上不支持const.我知道Jon Skeet长期以来一直希望支持不变性,我认为使用const函数的C ++语法可以帮助实现这一点.通过在类级别添加const关键字,我们将获得全面的支持.

I've been wondering for a while why C# doesn't support const on a class or a method level. I know that Jon Skeet have wanted support for immutability for a long time, and I recon that using the C++ syntax of function const could aid in that. By adding a const keyword on a class level we would have total support.

现在,我的问题是,C#团队没有开发这种支持的原因是什么?

Now, my question is, what the reason is for the C# team to not have developed this kind of support?

我想所有事情都可以通过编译时检查或通过属性来创建,而无需更改CLR.我不介意代码能够通过反射覆盖const行为.

I'd imagine everything could be created with a compile-time check or through attributes, without needing to change the CLR. I don't mind code being able to override the const behavior through reflection.

想象一下:

const class NumberContainer
{
    public int Number { get; }
}

..这样的类只能在构造时填充,因此我们需要一个构造函数来接受一个int.

.. Such a class could only be populated at construction time, so we'd need a constructor to take in an int.

另一个示例是方法级别的const:

Another example is const on a method-level:

public int AddNumbers(NumberContainer n1, NumberContainer n2) const
{
   return n1.Number + n2.Number;
}

常量级方法不应能够更改其自身类中的状态或传递给它们的引用类型实例.另外,const级函数只能在其作用域内调用其他const级函数.

Const-level methods should not be able to alter state in their own class or instances of reference types passed to them. Also, const-level functions could only invoke other const-level functions while in their scope.

我不太确定lambda和委托是否会使所有事情变得太难(或不可能),但是我敢肯定,有一定语言和编译器设计经验的人会告诉我.

I'm not really sure if lambdas and delegates would make everything too hard (or impossible) to achieve, but I'm sure someone with more experience in language and compiler design could tell me.

正如史蒂夫·B(Steve B)在评论中指出的那样,readonly的存在使事情变得更加复杂,因为在runtime期间const和readonly接近相同,但是readonly的值不能为在编译时确定.我想我们可以具有constreadonly级别,但这可能太令人困惑了?

As Steve B pointed out in the comments, the existence of readonly makes things a bit more complex, as const and readonly are close to the same during runtime, but readonly values can't be determined during compile-time. I guess we could have const and readonly level but that might be too confusing?

那么,不执行此操作的原因是什么?可用性方面的问题(对于新用户来说,很难理解C ++中的常量性),语言设计方面的问题(无法完成)或仅是优先级方面的问题(不变性嗡嗡声的时代已经过去了).

So, what's the reason for not implementing this? Usability concerns (understanding constness in C++ usually quite hard for new users), language design concerns (can't be done) or simply priority concerns (the days of the immutability-buzz are over)..?

推荐答案

出于某种循环的解释,C#不支持const,因为CLR不支持const. CLR不支持它,因为它完全不符合CLS.

Risking a somewhat circular explanation, C# doesn't support const because the CLR has no support for it whatsoever. The CLR doesn't support it because it is drastically non-CLS compliant.

很少有具有此概念的语言. C语言支持const,而C#中的 readonly 关键字对此也提供了很好的支持.但是,大狗当然是C ++,它对于const的适用性要广得多,毫无疑问,您正在寻找的是const.我将避免限定const的含义,这本身就是一个虫洞,而只是谈论"const-ness",即应用const的特性.

There are very few languages that have the concept. The C language has support for const, that's well supported in C# by readonly keyword. But the big dog is of course C++ that has a much wider applicability for const, no doubt the one you are looking for. I'll avoid pinning down what const should mean, that's a wormhole in itself and just talk of "const-ness", the property of having const applied.

保持一致性的麻烦是必须对其进行强制执行.当任意 other 语言可以使用C#类并完全忽略const-ness只是因为该语言不支持const-ness时,这就是C#中的问题.仅仅因为C#支持,就将其绑定到其他所有CLS语言上都是不切实际的.

The trouble with const-ness is that it needs to be enforced. That's a problem in C# when an arbitrary other language can use a C# class and completely ignore const-ness just because the language doesn't support it. Bolting it onto every other CLS language just because C# supports it is of course very unpractical.

在C ++中,可执行性也是一个问题.因为该语言还支持const_cast<>.任何客户端代码都可以快速且无法诊断地消除常量性.您不应该这样做,但是有时您必须这样做.因为有两种常态,严格性和可观察性.大致类似于私有常态和公共常态.后来将 mutable 关键字添加到该语言中,以尝试处理可观察到的常量性,因此至少可以避免const_cast<>的不可避免的用法.有人说C ++是一门难懂的语言.不太了解C#.

Enforceability is a problem in C++ as well. Because the language also supports const_cast<>. Any client code can cast the const-ness away swiftly and undiagnosably. You are not supposed to, but then sometimes you have to. Because there are two kinds of const-ness, strict and observable. Roughly analogous to private const-ness and public const-ness. The mutable keyword was added to the language later to try to deal with the need for observable const-ness so at least the inevitable usage of const_cast<> could be avoided. Some people say that C++ is a difficult language. Don't hear that of C# much.

这篇关于为什么C#在类/方法级别不支持const?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 07:39
查看更多