Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Readonly vs static readonly clarification

Tags:

c#

I've run into an interesting situation I am trying to understand. I have a readonly struct field in my class. This means that when I reference it, it references a copy and not the actual one, so when I call a change method, it will be working with a copy, and the original will remain unchanged.

That is not what I am observing. I only see the expected behavior with a static field. I expected the behavior for both types.

private struct junk
{
    public int i;

    public void change()
    {
        i += 1;
    }
}

private readonly junk jk;
private static readonly junk jk2;

public Form1()
{
    InitializeComponent();
    jk.change();
    //jk.i is now 1, why? Shouldn't it be changing a copy and not the original jk?
    jk2.change();
    //jk2.i is 0
}
like image 384
Endel_ Avatar asked Mar 10 '19 17:03

Endel_


People also ask

What is the difference between static and readonly?

Constant and ReadOnly keyword is used to make a field constant which value cannot be modified. The static keyword is used to make members static that can be shared by all the class objects.

What is static readonly?

Static ReadOnly: A Static Readonly type variable's value can be assigned at runtime or assigned at compile time and changed at runtime. But this variable's value can only be changed in the static constructor. And cannot be changed further.

What is difference between static constant and readonly variables?

The first, const, is initialized during compile-time and the latter, readonly, initialized is by the latest run-time. The second difference is that readonly can only be initialized at the class-level. Another important difference is that const variables can be referenced through "ClassName.

Should I use const or readonly?

Use the const keyword when the value contained in a variable will never change during the lifetime of the application. Use the readonly keyword when you are not sure whether the value of a variable of an object needs to change but you want to prevent other classes from changing the value.


1 Answers

I have a readonly struct field in my class. This means that when I reference it, it references a copy and not the actual one, so when I call a change method, it will be working with a copy, and the original will remain unchanged.

That's not at all what the readonly modifier does. The readonly modifier prevents you from assigning a new value to jk anywhere but in a constructor. Then, the static modifier allows you to reuse that value independently of the instance of Form1 you are working with.
That said, neither readonly nor static is making the weird behavior you are describing because that exact behavior cannot be reproduced with the code you've posted.

Look at a simpler example in a Console application (which you can try here):

public class Program
{
    private readonly junk jk;
    private static readonly junk jk2;

    public static void Main()
    {
        var program = new Program();
        program.jk.change();
        Console.WriteLine(program.jk.i); // prints 0

        jk2.change();
        Console.WriteLine(jk2.i); // prints 0
    }
}

public struct junk
{
    public int i;
    public void change()
    {
        i += 1;
    }
}

Then, as @Damien_The_Unbeliever commented, try to avoid mutable structs as much as you can.

like image 87
Camilo Terevinto Avatar answered Oct 05 '22 00:10

Camilo Terevinto