本文介绍了为什么会出现在C#和const参数没有const成员方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

和C ++不同,没有在C#中没有const成员方法和常量参数。想知道原因。

Unlike c++, there is no const member method and const parameters in c#. Want to know the reason.

推荐答案

首先,没有要求我们提供一个理由的没有的实施功能。特点是非常昂贵的;必须有正当理由的的实施一项功能,而不是理由的没有的实施功能。

First off, there is no requirement that we provide a reason for not implementing a feature. Features are extremely expensive; there has to be a justification for implementing a feature, not a justification for not implementing a feature.

二,C#不是C ++或C的克隆仅仅因为一个功能是在其他一些语言不是一个理由把它在C#。

Second, C# is not a clone of C++ or C. Just because a feature is in some other language is not a reason to put it in C#.

三,常量进行了深入,惨遭破的C和C ++。 常量让你不能保证你可以真正依靠。如果你是一个方法,需要一个常量引用,然后调用者必须的没有保证的,该方法荣誉的常量性;该方法具有突变const引用的许多方面。如果你是的消费者的的常引用,那么你有没有保证,底层的对象居然会发生变异随意。由于合同上没有任何主叫或被叫方一侧执行,它远比我们想使类型系统的任何其他担保弱。我们不希望复制这样一个破碎系统。

Third, "const" is deeply, tragically broken in C and C++. "const" gives you no guarantee that you can actually rely upon. If you are the caller of a method that takes a const reference then you have no guarantee whatsoever that the method honours the constness; the method has many ways of mutating a const reference. If you are the consumer of a const reference then you have no guarantee that the underlying object actually will not mutate arbitrarily. Since the contract is not enforced on either the caller or the callee side, it is far weaker than any other guarantee that we would like to make in the type system. We would not want to replicate such a broken system.

四,把常量性的CLR类型系统是指的每次的语言必须使用的一样的实施常量性的;由于不同的语言有不同的含义的常量性,这将是使它的困难的带来更多的语言到CLR,没有的更容易的。

Fourth, putting constness in the CLR type system means that every language would have to use the same implementation of constness; since different languages have different meanings for constness, that would be making it harder to bring more languages to the CLR, not easier.

有很多原因不这样做极其昂贵的功能,只有极少数的理由这样做。昂贵的,没有道理的功能没有得到落实。

There are many reasons for not doing this extremely expensive feature, and very few reasons to do it. Expensive, unjustified features don't get implemented.

这篇关于为什么会出现在C#和const参数没有const成员方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 16:05