You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Sep 13, 2022. It is now read-only.
private class TestReadOnly
{
public static readonly int Field;
}
[TestMethod]
public void TestReadOnlyField()
{
FieldInfo field = typeof(TestReadOnly).GetField(nameof(TestReadOnly.Field));
Console.WriteLine(field.GetValue(null));
field.SetValue(null, 0xFF);
Console.WriteLine(field.GetValue(null));
Console.WriteLine(TestReadOnly.Field);
}
Output:
0
255
255
0
The text was updated successfully, but these errors were encountered:
Static readonly fields get baked into the generated asm where they are used when Jitted; this also means that asm has branch elimination and devirtualisation happen on it due to the value of the field so its not possible to undo that by changing its value.
In .NET Core 3.0 it now throws this exception if you try to change the static readonly:
System.FieldAccessException: Cannot set initonly static field 'Field' after type 'TestReadOnly' is initialized.
Output:
0
255
255
0
The text was updated successfully, but these errors were encountered: