readonly struct
Should not contain reference type
#75249
-
Reference types in This code works pretty good as the static void Main(string[] args)
{
var b = new Bar { Value = "bar before foo" };
var f = new Foo(b);
Console.WriteLine(f.Value);
b.Value = "bar after foo";
f.Bar.Value = "bar from f.Bar";
Console.WriteLine(f.Value);
}
readonly struct Foo
{
public string Value => Bar.Value;
public Bar Bar { get; }
public Foo(Bar b) => Bar = b;
}
struct Bar
{
public string Value { get; set; }
} but once you change
Having that, the compiler should prevent reference types from being used in |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Read-only structs are not immutable, they are read-only. This has been the design of read-only in the language since 1.0. Changing this would break the world. If you want to have this restriction in your own code, you can write an analyzer for that purpose. |
Beta Was this translation helpful? Give feedback.
Read-only structs are not immutable, they are read-only. This has been the design of read-only in the language since 1.0. Changing this would break the world.
If you want to have this restriction in your own code, you can write an analyzer for that purpose.