Static readonly vs const in C#: when to use what?
Hi everyone! I'm a beginner C# developer, and I'm struggling with a question: what's the difference between `static readonly` and `const`? I understand that both are used to declare immutable values, but I have no idea when to use which.\n\nHere's an example: I'm writing a class for working with mathematical formulas. I need to declare the number Pi and the number of decimal places for outputting results. I did it like this:\n\n```csharp\npublic class MathHelper\n{\n public const double Pi = 3.14159;\n public static readonly int Precision = 3;\n}\n```\n\nBut then another developer said that `const` for `Pi` is fine, but `static readonly` for `Precision` is strange. Why? After all, both values don't change. Also, I noticed that if I use `const` in another project, after changing the value in this assembly, the other
No answers yet.
Log in to leave an answer