Friday, May 5, 2006

C# and VB.NET accessors

C# and VB.NET property accessor methods (get/set) have three uses, as far as I can tell:
  1. It’s possible to find out which methods are marked as property accessors, so it’s possible to show a list of properties, e.g. in Visual Studio’s class property lists, such as that used by the Windows.Forms designer.
  2. The user of a class can see that calling certain ‘property access’ methods on an instance will return or alter (part of) the state of the object.
  3. The caller of a ‘get’ method knows that it is ‘const’ and therefore idempotent.
Is the last point really true, though? I recently had a conversation with a programmer who laughed at my statement. I told him that it seemed obvious that the method should be ‘const’ (at least externally visible state should be maintained) but he said this was nonsense.

Is it only me who believes the last point to be axiomatic?