Leo Qiao

Leo Qiao

  • 1.7k
  • 78
  • 68

Is it possible to implement a immutable struct in c#

Nov 24 2024 5:04 AM

class string is immutable. It is impossible implementing an immutable struct type as you can't disable or override assignment operation (=). Even you define a readonly struct, you can always change the value of the struct type by using assignment operator.

For example, x,y are values of a readonly struct. x = y; will change the content of x. So, the readonly struct section in C# language reference is not correct. Is this right?