1

In the below code I am not understanding the role played by ReadOnly. I elaborate my question below:

Structure Mutable
    Private x As Integer
    Public Function Mutate() As Integer
        x = x + 1
        Return x
    End Function
End Structure

Module Module1
    Public ReadOnly m As Mutable 'Statement 1
    Sub Main()
        Console.WriteLine(m.Mutate())  ' 1  Statement 2
        Console.WriteLine(m.Mutate())  ' 1
        Console.WriteLine(m.Mutate())  ' 1
        Console.ReadLine()
    End Sub
End Module

How the control behaves if the ReadOnly keyword was not there:

  1. Statement 1 would read as: Public m As Mutable
  2. Upon execution of statement 1, an instance “m” gets created in the stack memory which is large enough to fit an integer type value “x” in it. The default value of x is 0
  3. m.Mutate() gets executed the first time. The control calls the function Mutate() where x = x+1 gets executed which increases the value of x by 1
  4. The value of x = 1 is returned to the function Mutate() which is then displayed.
  5. Steps 3 and 4 get repeated for the next two times when Mutate() function is called. The key point is that the value of x increases from 0 to 1, then from 1 to 2 and then finally to 3.

What is the difference when we add the ReadOnly keyword?

  1. m.mutate() gets executed the first time which increases the value of x from 0 to 1. This is returned and “1” gets printed
  2. But when the control shifts to the next line i.e. statement 2,and after the Mutate() function is called again I see from a break-point analysis that it is then the value of x is reset to 0 from 1. The function Mutate gets executed which increases the value again from 0 to 1 which gets printed. This is the main difference.

My Question: Why does the presence of ReadOnly keyword reset the value of x to 0 everytime the function Mutate is called? In this case, “m” is a local variable storing an instance of the structure. When an instance of a structure is declared as ReadOnly in VB.Net what is the implication? In such cases, should I understand the role of ReadOnly as “write once and reset if called again”? Can someone please explain the point.

My confusion stems from the fact that ReadOnly in VB.Net can be only used on Properties and Member Variables (Fields). When that is the case, their values can be set in the constructor of that structure/class or at declaration. But here “m” stores an instance. So what changes are being introduced when ReadOnly is being used on an instance?

In short how should I understand why using the ReadOnly keyword gives the output as “1 1 1”?

Sougata
  • 319
  • 1
  • 10
  • 1
    For C# the answer is in https://stackoverflow.com/q/3859534/11683, VB must be doing the same thing. Which you may already know because that code is the direct translation of the same from the mentioned [Eric Lippert's article](https://learn.microsoft.com/en-us/archive/blogs/ericlippert/mutating-readonly-structs). – GSerg May 01 '21 at 19:38
  • 1
    `m` is not a Local Variable, it's Field. When it's read-only, you always work on the original copy of the value assigned to the Field. In practice, you Load `m`, Push the value of the Field, Pop `m` (the original copy created when the Field is initialized), call `Mutate()`, Pop the value. Repeat for each `m.Mutate()` call -> You're always working on the original value or `m`, since `m` cannot be mutated. – Jimi May 01 '21 at 20:51
  • 1
    When `ReadOnly` is not specified, you instead work on `m` directly, changing its current value: you Load `m`, Push `m`, call `Mutate()`, Pop the value: now `m` reflects the value assigned each time by `Mutate()` -- You should read the documentation linked above. – Jimi May 01 '21 at 20:51
  • @GSerg yes you are right. I am aware of Eric Lippert's article. But his explanation was not clear to me. – Sougata May 02 '21 at 05:09

0 Answers0