By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,007 Members | 998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,007 IT Pros & Developers. It's quick & easy.

C# const declaration doc error

P: n/a
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?
Nov 15 '05 #1
Share this Question
Share on Google+
20 Replies


P: n/a
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?

Nov 15 '05 #2

P: n/a
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
Nov 15 '05 #3

P: n/a
100
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?

Nov 15 '05 #4

P: n/a
The documentation states that its not possible to declare them static, when
infact i was permited to, its a doc error. Or badly explained.

It clearly states...
"The static modifier is not allowed in a constant declaration."

Yet I can do what it says I cannot do. and also the compiler itself (if you
check object browser) states that they are what the documentation says they
are Not.
My point is, its confusing and inaccurate in the documentation.
"100" <10*@100.com> wrote in message
news:Op**************@tk2msftngp13.phx.gbl...
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?


Nov 15 '05 #5

P: n/a
What is the benifit of specifying a const in C# as static then? What is the
difference between a static const (which MSDN says clearly that cannot be
done but it can) and a non static const.

Anybody care to enlighten me as MSDN does a good job of confusing the issue.
"100" <10*@100.com> wrote in message
news:Op**************@tk2msftngp13.phx.gbl...
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?


Nov 15 '05 #6

P: n/a
100
Hi,
The documentation states that its not possible to declare them static, when infact i was permited to, its a doc error.


What do you mean? My C# compiler emits an error if I try to put *static*
modifier in constant declaration.

B\rgds
100
Nov 15 '05 #7

P: n/a
100 <10*@100.com> wrote:
Actually constants are only a meta data. There is no any fields behind them.


In what way? I can't see anything in any spec saying there aren't any
fields behind them, and reflection can certainly get them as fields:

using System;
using System.Reflection;

public class Test
{
public const int x = 5;

static void Main()
{
foreach (FieldInfo fi in typeof(Test).GetFields())
{
Console.WriteLine ("{0}={1}", fi.Name, fi.GetValue(null));
}
}
}

Yes, references to them are inlined, but I don't think that means
they're not fields.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #8

P: n/a
bwahahahaha <bw*******@bwahahaha.org.ie> wrote:
The documentation states that its not possible to declare them static, when
infact i was permited to, its a doc error. Or badly explained.

It clearly states...
"The static modifier is not allowed in a constant declaration."

Yet I can do what it says I cannot do.


Really? I get a compile-time error:

using System;

public class Test
{
static const int x = 5;

static void Main()
{
}
}

Test.cs(5,22): error CS0504: The constant 'Test.x' cannot be marked
static

Does the above really compile for you?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #9

P: n/a
Mine builds ok on 2003. Bloody odd.

public static const int WM_COMMAND = 0x112;

Works for me.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
bwahahahaha <bw*******@bwahahaha.org.ie> wrote:
The documentation states that its not possible to declare them static, when infact i was permited to, its a doc error. Or badly explained.

It clearly states...
"The static modifier is not allowed in a constant declaration."

Yet I can do what it says I cannot do.


Really? I get a compile-time error:

using System;

public class Test
{
static const int x = 5;

static void Main()
{
}
}

Test.cs(5,22): error CS0504: The constant 'Test.x' cannot be marked
static

Does the above really compile for you?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #10

P: n/a
try VS.NET 2003.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
bwahahahaha <bw*******@bwahahaha.org.ie> wrote:
The documentation states that its not possible to declare them static, when infact i was permited to, its a doc error. Or badly explained.

It clearly states...
"The static modifier is not allowed in a constant declaration."

Yet I can do what it says I cannot do.


Really? I get a compile-time error:

using System;

public class Test
{
static const int x = 5;

static void Main()
{
}
}

Test.cs(5,22): error CS0504: The constant 'Test.x' cannot be marked
static

Does the above really compile for you?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #11

P: n/a
bwahahahaha <bw*******@bwahahaha.org.ie> wrote:
Mine builds ok on 2003. Bloody odd.

public static const int WM_COMMAND = 0x112;

Works for me.


Doesn't work for me with VS.NET 2003. Is it definitely, definitely
building that file? Quick way to check: if you change it to

public xxx const int WM_COMMAND = 0x112;

does it *then* give a compiler error?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #12

P: n/a
IT worked for me today, strange.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
bwahahahaha <bw*******@bwahahaha.org.ie> wrote:
Mine builds ok on 2003. Bloody odd.

public static const int WM_COMMAND = 0x112;

Works for me.


Doesn't work for me with VS.NET 2003. Is it definitely, definitely
building that file? Quick way to check: if you change it to

public xxx const int WM_COMMAND = 0x112;

does it *then* give a compiler error?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #13

P: n/a
I cant check it now it wasnt on this machine but ill recheck again tomorrow.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
bwahahahaha <bw*******@bwahahaha.org.ie> wrote:
Mine builds ok on 2003. Bloody odd.

public static const int WM_COMMAND = 0x112;

Works for me.


Doesn't work for me with VS.NET 2003. Is it definitely, definitely
building that file? Quick way to check: if you change it to

public xxx const int WM_COMMAND = 0x112;

does it *then* give a compiler error?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #14

P: n/a
FWIW, I get a compiler error, as expected, on (yes, I'm using VS.NET 2003)

public class MyClass
{
public static const int WM_COMMAND = 0x112;
}
The constant 'xxx.WM_COMMAND' cannot be marked static
Brian W

"bwahahahaha" <bw*******@bwahahaha.org.ie> wrote in message
news:OG**************@TK2MSFTNGP11.phx.gbl...
Mine builds ok on 2003. Bloody odd.

public static const int WM_COMMAND = 0x112;

Works for me.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
bwahahahaha <bw*******@bwahahaha.org.ie> wrote:
The documentation states that its not possible to declare them static, when infact i was permited to, its a doc error. Or badly explained.

It clearly states...
"The static modifier is not allowed in a constant declaration."

Yet I can do what it says I cannot do.


Really? I get a compile-time error:

using System;

public class Test
{
static const int x = 5;

static void Main()
{
}
}

Test.cs(5,22): error CS0504: The constant 'Test.x' cannot be marked
static

Does the above really compile for you?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 15 '05 #15

P: n/a
100
Hi Jon,
I meant fields in terms of memory storage. They are literals. They are just
meta data. Internally.
This is the reason why the only reference type that can be used as constant
is String.
Constants are part of the type's meta data so they has to be accessible via
reflection. And because they are more like fields they are treated by
reflection as fields.
Unfortuantely at the moment I can't point you to any spec saying that, event
though I'm sure there is such spec. Anyway this can be found in the books.

Because there is no memory storage behind (they are part of the type's meta
data) declaring them as instance doesn't make sense.

The only evidence I came up with

struct TestStruct
{
public const int Const = 100;
public int Value;
}
....
unsafe static void Foo()
{

Console.WriteLine(sizeof(TestStruct));
}

Foo prints 4. Which means that there is no memory storage for the constant.

B\rgds
100

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
100 <10*@100.com> wrote:
Actually constants are only a meta data. There is no any fields behind
them.
In what way? I can't see anything in any spec saying there aren't any
fields behind them, and reflection can certainly get them as fields:

using System;
using System.Reflection;

public class Test
{
public const int x = 5;

static void Main()
{
foreach (FieldInfo fi in typeof(Test).GetFields())
{
Console.WriteLine ("{0}={1}", fi.Name, fi.GetValue(null));
}
}
}

Yes, references to them are inlined, but I don't think that means
they're not fields.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #16

P: n/a
100 <10*@100.com> wrote:
I meant fields in terms of memory storage.
They're certainly not *instance* fields, but I don't see why an
implementation shouldn't also decide to keep a copy of the value along
with the rest of its static fields.

<snip>
Because there is no memory storage behind (they are part of the type's meta
data) declaring them as instance doesn't make sense.

The only evidence I came up with

struct TestStruct
{
public const int Const = 100;
public int Value;
}
...
unsafe static void Foo()
{

Console.WriteLine(sizeof(TestStruct));
}

Foo prints 4. Which means that there is no memory storage for the constant.


Not per instance, no - but then that's true of static fields in
general, and they're still fields.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #17

P: n/a
100
> They're certainly not *instance* fields, but I don't see why an
implementation shouldn't also decide to keep a copy of the value along
with the rest of its static fields.

Why should it. The value of the constants is used only by compilers. One
can't get the adress of constant neither change it event with unmanaged
code. Constant's value is avaluated at compile time as the oposite to the
readonly (initonly) fields, which have to have memory storage to hold the
value.
Actually I think that the *implementation* keeps the value in the type's
internal structure for reflection's performance's sake. But it doesn't have
to do so. The value can be extracted from the meta data.
Which is not true for any other fields - static or instance.

B\rgds
100
Nov 15 '05 #18

P: n/a

"100" <10*@100.com> escribió en el mensaje
news:ep**************@TK2MSFTNGP09.phx.gbl...
Why should it. The value of the constants is used only by compilers. One
can't get the adress of constant neither change it event with unmanaged
code. Constant's value is avaluated at compile time as the oposite to the
readonly (initonly) fields, which have to have memory storage to hold the
value.
Although that's true as of reality, it doesn't really have to be like
that... that's an optimization compilers make, because it's the obvious use,
but there is a difference between constants and preprocessor values, that's
why #define was not equal to const in C++ unless you used optimizations.

In the real world, though, they are the same, and, i'm not sure about C#,
but I wouldn't be surprised they would use const values at compile time
(even with no optimizations), coz that's the most obvious way to use them,
and it's the way they should be used, and the way optimizations can be
actually performed (that's where you gain a variable being const anwyay).
Actually I think that the *implementation* keeps the value in the type's
internal structure for reflection's performance's sake. But it doesn't have to do so. The value can be extracted from the meta data.
Which is not true for any other fields - static or instance.


I don't know how the C# implementation is, so I can't say anything about
it... that sounds right anyway, but I can't confirm.

Javier Campos
Virtual Media Systems, S.L.
Nov 15 '05 #19

P: n/a
100 <10*@100.com> wrote:
They're certainly not *instance* fields, but I don't see why an
implementation shouldn't also decide to keep a copy of the value along
with the rest of its static fields.
Why should it.
Convenience?
The value of the constants is used only by compilers. One
can't get the adress of constant neither change it event with unmanaged
code. Constant's value is avaluated at compile time as the oposite to the
readonly (initonly) fields, which have to have memory storage to hold the
value.
It's also available for reflection though.
Actually I think that the *implementation* keeps the value in the type's
internal structure for reflection's performance's sake. But it doesn't have
to do so. The value can be extracted from the meta data.
Which is not true for any other fields - static or instance.


I never claimed that it *had* to - I was just arguing against the idea
that it definitely *didn't*.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #20

P: n/a
100
> I never claimed that it *had* to - I was just arguing against the idea
that it definitely *didn't*.


I haven't said it definitely didn't. I said that this is the idea behind the
literals. How it is implemented is different story, isn't it.

B\rgds
100
Nov 15 '05 #21

This discussion thread is closed

Replies have been disabled for this discussion.