Re: C# const declaration doc error
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.WriteLi ne(sizeof(TestS truct));
}
Foo prints 4. Which means that there is no memory storage for the constant.
B\rgds
100
"Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
news:MPG.19f8d5 2b7a97be809898a 6@msnews.micros oft.com...[color=blue]
> 100 <100@100.com> wrote:[color=green]
> > Actually constants are only a meta data. There is no any fields behind[/color][/color]
them.[color=blue]
>
> 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.Reflecti on;
>
> public class Test
> {
> public const int x = 5;
>
> static void Main()
> {
> foreach (FieldInfo fi in typeof(Test).Ge tFields())
> {
> Console.WriteLi ne ("{0}={1}", fi.Name, fi.GetValue(nul l));
> }
> }
> }
>
> Yes, references to them are inlined, but I don't think that means
> they're not fields.
>
> --
> Jon Skeet - <skeet@pobox.co m>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too[/color]
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.WriteLi ne(sizeof(TestS truct));
}
Foo prints 4. Which means that there is no memory storage for the constant.
B\rgds
100
"Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
news:MPG.19f8d5 2b7a97be809898a 6@msnews.micros oft.com...[color=blue]
> 100 <100@100.com> wrote:[color=green]
> > Actually constants are only a meta data. There is no any fields behind[/color][/color]
them.[color=blue]
>
> 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.Reflecti on;
>
> public class Test
> {
> public const int x = 5;
>
> static void Main()
> {
> foreach (FieldInfo fi in typeof(Test).Ge tFields())
> {
> Console.WriteLi ne ("{0}={1}", fi.Name, fi.GetValue(nul l));
> }
> }
> }
>
> Yes, references to them are inlined, but I don't think that means
> they're not fields.
>
> --
> Jon Skeet - <skeet@pobox.co m>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too[/color]
Comment