Tracks
/
C#
C#
/
Syllabus
/
Const Readonly
Co

Const Readonly in C#

0 exercises

About Const Readonly

By default, values in C# are mutable, that is they can change over time. To make a value immutable, there are two options:

  1. Add the const modifier.
  2. Add the readonly modifier.

The const modifier has some restrictions:

  1. It can only be applied to "constant" types: strings, booleans and numbers.
  2. The const value must be initialized immediately.

See defining constants for more information.

If your value is a non-constant type or you need to initialize the value in a constructor, readonly can be used to enforce immutability.

Edit via GitHub The link opens in a new window or tab