-1

Is there a way to tell the compiler that the result of an expression shall be considered immutable from this place onward?

...
"const" MyClass str = b[0].x+b[1].x;
... // from here on str is an immutable object

And have const or readonly variable a performance benefit?


Added: @Onur: (a) the assignment of a new value shall be allowed; (b) the current value of str shall not be allowed, that includes everything that can be accessed via str, eg any field of str as well as everything that is reference in any way by str; (c) Example might be difficult. But I try to rephrase it. str shall be any kind of datastructure (object, collection and any mixture of that). The object that will be assigned to str is mutable and can be modified by another reference. But when assigned to str, I want to make sure that via str no modification of the object is possible. The idea behind that is, that the object, once created, will be passed to differenct threads to work with the data of the object and I want to make sure that I do not accidently modify the object that is passed to the different threads by reference. So I want some kind of reference that prevents my from modifying any data. Let's say I want some kind of reference that makes the complete object readonly.

bebo
  • 819
  • 1
  • 9
  • 26
  • 10
    It looks like you're confusing constness and immutability. Strings are already immutable in C#. – Frédéric Hamidi Mar 11 '15 at 10:48
  • There is no serious performance benefit from const or readonly, they are mostly there to help maintainability. – Rik Mar 11 '15 at 10:54
  • I think that const variables should not be changed (initialized) during the runtime. – Peter Mar 11 '15 at 10:55
  • I changed String to MyClass. Wasn't thinking about String is readonly by default. – bebo Mar 11 '15 at 11:00
  • 1
    Could you clarify if (a) str should not be assigned a new value; (b) the current value of str shall not be modifyable or (c) something different; Maybe an example could help showing what should (not) be allowed – Onur Mar 11 '15 at 12:41

3 Answers3

1

You can't have readonly local variable (see for example Why does C# disallow readonly local variables?). You can have a readonly field in a class/struct. I don't think they are faster to access than a normal local variable/a normal field variable.

Note that per Jon Skeet readonly fields are slower :-)

You can have a const local variable, but it can only have values that can be calculated at compile time. A const variable/field is faster than a variable, because it's removed by the compiler :-) (at least it's faster to JIT, because it isn't even present in the IL code)

This:

const int xxx = 1;
const int yyy = xxx + 1;
Console.WriteLine(yyy);

is compiled to

Console.WriteLine(2);

(checked with ILSpy, even in Debug mode)

Community
  • 1
  • 1
xanatos
  • 109,618
  • 12
  • 197
  • 280
0
  1. You can't assign a const any value other then another constant. You can, however, define a private variable that can be initialized inside it's class with whatever value you want, and simply expose a read only property with it's value.

  2. Constant is not the same as immutable. the value of a constant can not be changed, while the value of a reference pointing to an immutable object such as string can be changed. When you have a string variable and you change it's value, what happens underneath the .Net layer is that the string is destroyed and the new string value replaces it. this is the meaning of immutable.

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
0

You probably confuse three different concepts:

  • Immutable refers to objects: an immutable object is an object that cannot change state, you can only create for instance an new object based on earlier objects.

  • const refers to constant. A constant is a variable where the value is/can be known at compile time. For instance (with small error):

    public const double Pi = 3.14159265358979;
    public const double PiHalf = Pi/2;
    

    The compiler can simply reason about constants, and - optionally - inline time. Objects are in general no constants, since they require allocation on the heap, can have multiple references to them, etc.

  • Readonly: readonly means you write once (at constructor level) to the fields, and then never change their value again.

C# allows to mark a (local) variable as constant, but as said before, that implies the value must be known at compile time. So you can't - let's say - read in a value from stdin and then say the variable will remain constant. Since it is however likely that b is an array of objects, the arguments are not constants and thus neither will the result be. Your statement will thus fail.

Liveness analysis is however a processes most compilers perform can in many cases detect that a variable will not be modified.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555