使用consts在静态类C# [英] C# using consts in static classes

查看:165
本文介绍了使用consts在静态类C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个开源项目过去的这个周末,当我跑进< A HREF =HTTP://$c$c.google.com/p/noda-time/source/browse/src/NodaTime/Format/FormatUtils.cs>有点code那我弄糊涂了查找使用在 C#规范

I was plugging away on an open source project this past weekend when I ran into a bit of code that confused me to look up the usage in the C# specification.

在问题的code是如下:

The code in questions is as follows:

internal static class SomeStaticClass
{
    private const int CommonlyUsedValue = 42;

    internal static string UseCommonlyUsedValue(...)
    {
        // some code
        value = CommonlyUsedValue + ...;

        return value.ToString();
    }
}

我被打了个措手不及,因为这似乎是正在使用的静态函数的非静态字段其中一些如何编译就好一个静态类!

I was caught off guard because this appears to be a non static field being used by a static function which some how compiled just fine in a static class!

该规范规定(第10.4节):

The specification states (§10.4):

一个常数声明可能包括
  属性集(第17章),一个新的
  改性剂(§10.3.4),和一个有效
  四个组合的访问
  修饰符(§10.3.5)。属性
  和修饰符适用于所有的
  成员的声明
  常数声明。 虽然
  常量被认为是静态
  件,一个常数声明
  既不要求也允许静态
  修改
这是同样的错误
  改性剂出现在多次
  常量声明。

A constant-declaration may include a set of attributes (§17), a new modifier (§10.3.4), and a valid combination of the four access modifiers (§10.3.5). The attributes and modifiers apply to all of the members declared by the constant-declaration. Even though constants are considered static members, a constant-declaration neither requires nor allows a static modifier. It is an error for the same modifier to appear multiple times in a constant declaration.

所以,现在它使更多一点意义,因为的常数被认为是静态成员的,但是句子的其余部分是一个有点出人意料我。为什么是它的一个常数声明既不需要也不允许static修饰符的?诚然,我不知道该规范不够好,这立即意义摆在首位,但为什么决定做不强制常量使用static修饰符,如果他们被认为是静态的?

So now it makes a little more sense because constants are considered static members, but the rest of the sentence is a bit surprising to me. Why is it that a constant-declaration neither requires nor allows a static modifier? Admittedly I did not know the spec well enough for this to immediately make sense in the first place, but why was the decision made to not force constants to use the static modifier if they are considered static?

纵观该段最后一句,我不能,如果它是直接有关previous声明弄清楚,而且对常量一些隐性static修饰符,首先,如果它代表本身作为另一个规则为常数。谁能帮我清理一下吗?

Looking at the last sentence in that paragraph, I cannot figure out if it is regarding the previous statement directly and there is some implicit static modifier on constants to begin with, or if it stands on its own as another rule for constants. Can anyone help me clear this up?

推荐答案

基本上,常量暗示的静态不已,因为该值不能在运行时改变。没有理由让你永远声明的静态常量的,因为它已经暗示,语言设计者决定使语言的语法反映这一点。

Basically, const implies static already, since the value cannot be changed at runtime. There's no reason for you to ever declare static const, since it's already implied, and the language designers decided to make the language syntax reflect that.

该规范的语言基本上是说常量总是静态的,所以你可以没有明确说静态和const,因为它是多余的。

The specification language is basically saying "Const is always static, so you can't explicitly say static and const since it's redundant."

这篇关于使用consts在静态类C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆