PC Review


Reply
Thread Tools Rate Thread

C# - Static vs. Const

 
 
JPS
Guest
Posts: n/a
 
      22nd Oct 2008
Can someone please refresh my memory here, but what is the difference
between a Static variable (or method) and a Const?
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      22nd Oct 2008
JPS wrote:
> Can someone please refresh my memory here, but what is the difference
> between a Static variable (or method) and a Const?


With C#, a static member is a class member:
http://msdn.microsoft.com/en-us/library/98f28cdx(VS.80).aspx

const can be applied to members or to variables to indicate that the
value cannot be modified:
http://msdn.microsoft.com/en-us/library/e6w8fe1b(VS.80).aspx
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
Ben Voigt [C++ MVP]
Guest
Posts: n/a
 
      22nd Oct 2008
Martin Honnen wrote:
> JPS wrote:
>> Can someone please refresh my memory here, but what is the difference
>> between a Static variable (or method) and a Const?

>
> With C#, a static member is a class member:
> http://msdn.microsoft.com/en-us/library/98f28cdx(VS.80).aspx


Meaning that it's shared between all instances of the class.

>
> const can be applied to members or to variables to indicate that the
> value cannot be modified:
> http://msdn.microsoft.com/en-us/library/e6w8fe1b(VS.80).aspx


In C# it means both more and less that simply that the value won't be
modified.

More, because it actually has to be a compile-time literal, and the compiler
is allowed to substitute the literal value everywhere the const variable
appears. In essence, the const field exists only in the metadata.

Less, because you can't mark anything as "const" except static fields,
unlike C++ where variables and access through pointers can be marked "const"
(can't modify the value) anywhere they appear, including function arguments
and even the "this" pointer.

C#'s "readonly" comes a lot closer to a simple marker "cannot be modified
after creation".


 
Reply With Quote
 
puzzlecracker
Guest
Posts: n/a
 
      22nd Oct 2008

> Less, because you can't mark anything as "const" except static fields,
> unlike C++ where variables and access through pointers can be marked "const"
> (can't modify the value) anywhere they appear, including function arguments
> and even the "this" pointer.
>


I don't think you have to declare a field static if you specify const.
I believe compilers deduce that automatically.

correct me if I am wrong.

 
Reply With Quote
 
Ben Voigt [C++ MVP]
Guest
Posts: n/a
 
      22nd Oct 2008
puzzlecracker wrote:
>> Less, because you can't mark anything as "const" except static
>> fields, unlike C++ where variables and access through pointers can
>> be marked "const" (can't modify the value) anywhere they appear,
>> including function arguments and even the "this" pointer.
>>

>
> I don't think you have to declare a field static if you specify const.
> I believe compilers deduce that automatically.
>
> correct me if I am wrong.


No, in fact you can't use the "static" and "const" keywords together. But
whenever you use "const", you get a static member.


 
Reply With Quote
 
Ben Voigt [C++ MVP]
Guest
Posts: n/a
 
      29th Oct 2008
Peter Duniho wrote:
> On Wed, 22 Oct 2008 07:20:11 -0700, Ben Voigt [C++ MVP]
> <(E-Mail Removed)> wrote:
>
>> [...]
>> Less, because you can't mark anything as "const" except static
>> fields, unlike C++ where variables and access through pointers can
>> be marked "const"
>> (can't modify the value) anywhere they appear, including function
>> arguments
>> and even the "this" pointer.

>
> Small nit: you can mark local variables as "const" as well.


Even smaller nit: they aren't actually variables at that point (I think the
C# compiler will totally remove local constants during the compile process,
not even leaving metadata). The expression has to be a compile-time
literal, which is quite different from C and C++ where const means you can
have a run-time calculation but the variable can't be changed after it is
initialized.

>
> And yes, "const" applied to a field implies "static" as well.
>
> Pete



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const Vs static =?Utf-8?B?QW50?= Microsoft C# .NET 2 30th Jun 2005 07:33 AM
Re: Advantage of const against static readonly? Jeff Louie Microsoft C# .NET 0 23rd Jan 2005 11:59 PM
Advantage of const against static readonly? cody Microsoft C# .NET 20 23rd Jan 2005 10:37 AM
const and static readonly - Difference. =?Utf-8?B?S2F2aXRoYQ==?= Microsoft Dot NET 5 7th Dec 2004 12:19 PM
Static const. Vladimir Bezugliy Microsoft C# .NET 5 13th Feb 2004 10:10 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:31 PM.