8

Possible Duplicate:
“const correctness” in C#

I suspect const was simplified for the C# spec for general language simplicity. Was there a specific reason we can't declare variable references or methods as const like we can with C++? e.g.:

const MyObject o = new MyObject();  // Want const cast referenece of MyObject
o.SomeMethod();    // Theoretically legal because SomeMethod is const
o.ChangeStuff();   // Theoretically illegal because ChangeStuff is not const

class MyObject 
{
   public int val = 0;

   public void SomeMethod() const 
   {
      // Do stuff, but can't mutate due to const declaration.
   }

   public void ChangeStuff() 
   {
      // Code mutates this instance.  Can't call with const reference.
      val++;
   }
}
Community
  • 1
  • 1
spoulson
  • 21,335
  • 15
  • 77
  • 102
  • 1
    You mean, besides "Because C++-style `const`ness has far-reaching implications which are really hard to do right (from both a design and implementation POV) and for which there is no runtime support"? It's a useful, but really expensive feature. FWIW, F# takes the opposite approach: *Nothing* is mutable unless declared as such. Interactions with the framework can be non-obvious, though. – Craig Stuntz Jun 03 '10 at 17:21
  • I disagree that this is an exact duplicate. The old question asks how to implement this in code. @spoulson asks why it's not in the compiler. – Craig Stuntz Jun 03 '10 at 17:27
  • 1
    Const correctness needs to be enforced to be meaningful. That becomes difficult when any .NET enabled language can consume the types created by another language. There are probably close to a hundred of them. The syntax of all of these languages would have to be modified to accommodate declaring const attributes and their compilers tweaked to verify them. In other words, const correctness is drastically non-CLS compliant. – Hans Passant Jun 03 '10 at 17:32
  • @HansPassant: Attributes to indicate whether struct methods and properties affect the underlying struct would be useful even if they were not enforced, since they could tell compilers which methods and properties should be usable on read-only structures. Further, even if a language didn't have support for const correctness, the JITter could enforce it by making a defensive copy of structures if supplier or recipient can't guarantee that it isn't needed, but omitting the copy operation if both supplier and recipient can be verified as not needing it. – supercat Oct 29 '12 at 21:38

3 Answers3

1

A const performs a compile time substitution of the value wherever it is used and therefore doesn't have any runtime meaning. In general what you propose for const objects would be very difficult for the compiler to determine (if a method will modify the object or not). Your proposal to use a const keyword as an access modifier also then puts burden on the writer and you are still left with a problem of verifying of something does or does not modify the object. Also you are imposing something on the object that does not have a meaning in all contexts. What does it mean if the method is const but you aren't using it as a const object? The functionality you want is usually accomplished by implementing an interface and only exposing the "read-only" parts of the class.

Craig Suchanec
  • 10,474
  • 3
  • 31
  • 39
0

I suspect the first sentence of your question answers it.

zneak
  • 134,922
  • 42
  • 253
  • 328
0

I believe you can declare variables as const in C#. static as well, if you feel the need.

http://msdn.microsoft.com/en-us/library/e6w8fe1b(VS.71).aspx

Caleb Hearth
  • 3,315
  • 5
  • 30
  • 44