What is difference between const and readonly?

πŸ’‘ Concept: const vs readonly in C#

const and readonly are keywords used to define immutable values, but differ in when and how their values are assigned and used.

πŸ“˜ Quick Intro

const is a compile-time constant; readonly is a runtime constant assigned once, typically in constructor.

🧠 Analogy

Think of const as a tattooβ€”permanent and unchangeable; readonly as a signed contractβ€”set once and fixed afterward.

πŸ”§ Technical Explanation

  • const values are embedded at compile time; readonly values assigned at runtime.
  • const must be assigned where declared; readonly can be assigned in constructor.
  • const is static by default; readonly can be instance-level or static.
  • const only supports primitive types and strings.
  • readonly can hold reference types and complex objects.

🎯 Use Cases

  • βœ… Use const for fixed values known at compile time.
  • βœ… Use readonly for values assigned once during object construction.
  • βœ… Use readonly for complex types needing runtime initialization.
  • βœ… Use const for performance benefits in embedded values.

πŸ’» Code Example


public class Example {
    public const double Pi = 3.1415;
    public readonly int MaxValue;

    public Example(int max) {
        MaxValue = max;
    }
}

❓ Interview Q&A

Q1: What is the difference between const and readonly?
A: const is compile-time, readonly is runtime.

Q2: Can readonly be assigned multiple times?
A: No, only once in constructor or declaration.

Q3: Are const values static?
A: Yes, implicitly static.

Q4: Can readonly hold complex objects?
A: Yes.

Q5: Can const be changed at runtime?
A: No.

Q6: Is readonly slower than const?
A: Slightly, due to runtime assignment.

Q7: Can const be instance level?
A: No, always static.

Q8: Can readonly be static?
A: Yes.

Q9: When to prefer readonly?
A: When value known only at runtime.

Q10: Are const and readonly thread-safe?
A: Yes, inherently immutable.

πŸ“ MCQs

Q1. What is const in C#?

  • Runtime constant
  • Compile-time constant
  • Variable
  • Property

Q2. What is readonly in C#?

  • Runtime constant
  • Compile-time constant
  • Variable
  • Method

Q3. Can readonly be assigned multiple times?

  • Yes
  • No
  • Sometimes
  • Always

Q4. Are const values static?

  • No
  • Yes
  • Maybe
  • Sometimes

Q5. Can readonly hold complex objects?

  • No
  • Yes
  • Maybe
  • Sometimes

Q6. Can const be changed at runtime?

  • Yes
  • No
  • Sometimes
  • Rarely

Q7. Is readonly slower than const?

  • Faster
  • Slightly
  • Same
  • No

Q8. Can const be instance level?

  • Yes
  • No
  • Maybe
  • Sometimes

Q9. Can readonly be static?

  • No
  • Yes
  • Sometimes
  • Never

Q10. Are const and readonly thread-safe?

  • No
  • Yes
  • Sometimes
  • Never

πŸ’‘ Bonus Insight

Understanding const vs readonly helps write safer and more predictable C# code.

πŸ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

πŸ” Navigation

Learn More About C# πŸ“š

1. What is C#? πŸ‘‰ Explained
2. Main Features of C# πŸ‘‰ Explained
3. Difference Between C# and Java πŸ‘‰ Explained
4. Common Language Runtime (CLR) in C# πŸ‘‰ Explained
5. Common Type System (CTS) in C# πŸ‘‰ Explained
6. Common Language Specification (CLS) in C# πŸ‘‰ Explained
7. Value Types vs Reference Types in C# πŸ‘‰ Explained
8. What is a Namespace in C#? πŸ‘‰ Explained
9. Purpose of the 'using' Keyword in C# πŸ‘‰ Explained
10. Different Data Types in C# πŸ‘‰ Explained
11. Difference Between int and Int32 in C# πŸ‘‰ Explained
12. Difference Between float, double, and decimal in C# πŸ‘‰ Explained
13. What is the Default Value of a Boolean in C#? πŸ‘‰ Explained
14. What is Boxing and Unboxing in C# πŸ‘‰ Explained
15. What are the Different Types of Operators in C# πŸ‘‰ Explained
16. Difference Between Equals and == in C# πŸ‘‰ Explained
17. What is the Null-Coalescing Operator ?? in C# πŸ‘‰ Explained
18. What is the Ternary Operator in C# πŸ‘‰ Explained
19. How Does the Switch Statement Work in C# πŸ‘‰ Explained
20. What is Object-Oriented Programming in C# πŸ‘‰ Explained
21. What are the Four Pillars of OOP in C# πŸ‘‰ Explained
22. What is Encapsulation in C# πŸ‘‰ Explained
23. What is Inheritance in C# πŸ‘‰ Explained
24. What is Polymorphism in C# πŸ‘‰ Explained
25. What is Abstraction in C# πŸ‘‰ Explained
26. What is an Abstract Class in C# πŸ‘‰ Explained
27. What is an Interface in C# πŸ‘‰ Explained
28. Can a Class Implement Multiple Interfaces in C#? πŸ‘‰ Explained
29. Difference Between Abstract Class and Interface in C# πŸ‘‰ Explained
30. How Do You Create a Class in C#? πŸ‘‰ Explained
31. What is a Constructor in C# πŸ‘‰ Explained
32. What Are the Types of Constructors in C# πŸ‘‰ Explained
33. What is a Static Constructor in C# πŸ‘‰ Explained
34. Difference Between Static and Non-Static Members in C# πŸ‘‰ Explained
35. What is the Use of 'this' Keyword in C# πŸ‘‰ Explained
36. What is a Destructor in C# πŸ‘‰ Explained
37. What is Object Initializer Syntax in C# πŸ‘‰ Explained
38. What is the Difference Between Field and Property in C# πŸ‘‰ Explained
Share:

Tags:


Feedback Modal Popup