Avatar ·

What is the difference between const and readonly in C#?

Hi everyone! I'm new to C# and I keep getting confused between the `const` and `readonly` keywords. They both seem to make a value immutable, but I feel like that's not quite the case. I have this code: ```csharp public class MyClass { public const int MaxAttempts = 5; public readonly string ConnectionString = "localhost"; public void Test() { // MaxAttempts = 10; // Error - const cannot be changed // ConnectionString = "new"; // Error - readonly cannot be changed } } ``` Both fields cannot be changed after initialization. But what's the difference then? I've heard that `const` works at compile time, while `readonly` works at runtime. What does that mean in practice? When should I use `const` and when should I use `readonly`? And why can I initialize