C#const声明doc错误 [英] C# const declaration doc error

查看:101
本文介绍了C#const声明doc错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MSDN文档中说明..


备注

常量声明可以声明多个常量,例如:


public const double x = 1.0,y = 2.0,z = 3.0;

常量

声明中不允许使用static修饰符。

常量可以参与常量表达式,例如:

public const int c1 = 5.0;

public const int c2 = c1 + 100;

然而在VS.NET 2003中,编译器生成一个静态的

修饰符,但在MSDN中它表示不允许这样做。如果我没有b $ b声明它是静态的,那么对象查看器会说它是静态的,并且我也允许将它指定为静态const。那么,

MSDN错了吗?或者只是典型的开发人员严重解释它?

解决方案

你是什么意思编译器生成一个静态修饰符?如果你是在谈论IL产生的是什么,那么你就不能将你在C#

语言规范中的内容应用到你在IL中看到的内容,因为那样遵循不同的

规范。最终,常量表示为静态变量。它只是一个

C#构造,并且无关如何在IL中表示。


希望这会有所帮助。

" Blah" <一个******* @ discussions.microsoft.com>在消息中写道

新闻:0f **************************** @ phx.gbl ... < blockquote class =post_quotes>在MSDN文档中说明..

备注
常量声明可以声明多个常量,例如:
public const double x = 1.0,y = 2.0,z = 3.0;
静态修饰符不允许在常量
声明中。
常量可以参与常量表达式,例如:
public const int c1 = 5.0;
public const int c2 = c1 + 100;

然而在VS.NET 2003中,编译器会生成一个静态的
修饰符MSDN它说不允许。如果我没有声明它是静态的,那么对象查看器会说它是静态的,并且我也允许将它指定为静态const。那么,MSDN错了吗?或者只是典型的开发人员严重解释它?



嗨Blah,


" Blah" <一个******* @ discussions.microsoft.com>在消息中写道

新闻:0f **************************** @ phx.gbl ... < blockquote class =post_quotes>在MSDN文档中说明..

备注
常量声明可以声明多个常量,例如:
public const double x = 1.0,y = 2.0,z = 3.0;
静态修饰符不允许在常量
声明中。
常量可以参与常量表达式,例如:
public const int c1 = 5.0;
public const int c2 = c1 + 100;

然而在VS.NET 2003中,编译器会生成一个静态的
修饰符MSDN它说不允许。如果我没有声明它是静态的,那么对象查看器会说它是静态的,并且我也允许将它指定为静态const。那么,MSDN错了吗?或者只是典型的开发人员严重解释它?




常量是隐式静态的。由于它们只能是一个值

,所以没有理由让它们成为实例成员并且静态使它们更加灵活(可以在没有类实例的情况下引用它们) )。你确定

你可以指定静态const吗?对我来说,编译器(VS 2003)会按预期检测到

错误(常量''MyClass.MY_CONSTANT''无法标记为

static")。


问候,

Dan


嗨Blah,

其实常数是只有元数据。它们背后没有任何字段。

你可以看到所谓的字段有属性* literal *应用。

尼古拉斯是对的。你在c#中声明和使用常量的方式是根据c#规范的b $ b,与IL和CLR无关。


B \ rgds

100

" Blah" <一个******* @ discussions.microsoft.com>在消息中写道

新闻:0f **************************** @ phx.gbl ... < blockquote class =post_quotes>在MSDN文档中说明..

备注
常量声明可以声明多个常量,例如:
public const double x = 1.0,y = 2.0,z = 3.0;
静态修饰符不允许在常量
声明中。
常量可以参与常量表达式,例如:
public const int c1 = 5.0;
public const int c2 = c1 + 100;

然而在VS.NET 2003中,编译器会生成一个静态的
修饰符MSDN它说不允许。如果我没有声明它是静态的,那么对象查看器会说它是静态的,并且我也允许将它指定为静态const。那么,MSDN错了吗?或者只是典型的开发人员严重解释它?



In MSDN documentation it states..

Remarks
The constant declaration can declare multiple constants,
for example:
public const double x = 1.0, y = 2.0, z = 3.0;
The static modifier is not allowed in a constant
declaration.
A constant can participate in a constant expression, for
example:
public const int c1 = 5.0;
public const int c2 = c1 + 100;
Yet in VS.NET 2003 the compiler generates a static
modifier yet in MSDN it says its not allowed. If i dont
declare it static the object viewer says its static, and i
am also permitted to specify it as a static const. So,
MSDN wrong? or just typical developer badly explaining it?

解决方案

What do you mean the compiler generates a static modifier? If you are
talking about what IL is produced, then you can''t apply what is in the C#
language spec to what you see in the IL, because that follows a different
spec. Ultimately, a constant is represented as a static variable. It is a
C# construct only, and has nothing to do how it is represented in IL.

Hope this helps.

"Blah" <an*******@discussions.microsoft.com> wrote in message
news:0f****************************@phx.gbl...

In MSDN documentation it states..

Remarks
The constant declaration can declare multiple constants,
for example:
public const double x = 1.0, y = 2.0, z = 3.0;
The static modifier is not allowed in a constant
declaration.
A constant can participate in a constant expression, for
example:
public const int c1 = 5.0;
public const int c2 = c1 + 100;
Yet in VS.NET 2003 the compiler generates a static
modifier yet in MSDN it says its not allowed. If i dont
declare it static the object viewer says its static, and i
am also permitted to specify it as a static const. So,
MSDN wrong? or just typical developer badly explaining it?



Hi Blah,

"Blah" <an*******@discussions.microsoft.com> wrote in message
news:0f****************************@phx.gbl...

In MSDN documentation it states..

Remarks
The constant declaration can declare multiple constants,
for example:
public const double x = 1.0, y = 2.0, z = 3.0;
The static modifier is not allowed in a constant
declaration.
A constant can participate in a constant expression, for
example:
public const int c1 = 5.0;
public const int c2 = c1 + 100;
Yet in VS.NET 2003 the compiler generates a static
modifier yet in MSDN it says its not allowed. If i dont
declare it static the object viewer says its static, and i
am also permitted to specify it as a static const. So,
MSDN wrong? or just typical developer badly explaining it?



Constants are implicitly static. Since they can only be one value
there''s no reason to make them instance members and being static makes them
more flexible (can reference without an instance of the class). Are you sure
you can specify "static const"? For me the compiler (VS 2003) detects an
error as expected ("The constant ''MyClass.MY_CONSTANT'' cannot be marked
static").

Regards,
Dan


Hi Blah,
Actually constants are only a meta data. There is no any fields behind them.
You can see that so called field has attribute *literal* applied.
Nicholas is right. The way you declare and use constants in c# is according
the c# spec and has nothing to do with IL and CLR.

B\rgds
100

"Blah" <an*******@discussions.microsoft.com> wrote in message
news:0f****************************@phx.gbl...

In MSDN documentation it states..

Remarks
The constant declaration can declare multiple constants,
for example:
public const double x = 1.0, y = 2.0, z = 3.0;
The static modifier is not allowed in a constant
declaration.
A constant can participate in a constant expression, for
example:
public const int c1 = 5.0;
public const int c2 = c1 + 100;
Yet in VS.NET 2003 the compiler generates a static
modifier yet in MSDN it says its not allowed. If i dont
declare it static the object viewer says its static, and i
am also permitted to specify it as a static const. So,
MSDN wrong? or just typical developer badly explaining it?



这篇关于C#const声明doc错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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