-5

I am writing down some notes on a C# course I'm currently taking, and I was wondering if there's a specifically referenced name to the fields in a class we create that should not be able to be changed. Do we simply call them, "Readonly Fields"? Take the following field (width) in the Map class as an example:

public class Map
    {
      public readonly int Width;
        public int Height;

        public Map(int width, int height)
        {
            Width = width;
            Height = height;
        }
    }
Alek
  • 9
  • 1
  • 2
    Does this answer your question? [What is the difference between const and readonly in C#?](https://stackoverflow.com/questions/55984/) and [const vs. readonly](https://stackoverflow.com/questions/11323617/) and ['Static readonly' vs. 'const'](https://stackoverflow.com/questions/755685/) and [What are the benefits to marking a field as `readonly` in C#?](https://stackoverflow.com/questions/277010/) and [What is difference between Init-Only and ReadOnly in C# 9?](https://stackoverflow.com/questions/62372422/) and [The true definition of immutability?](https://stackoverflow.com/questions/912858/) –  Jul 30 '21 at 17:04

2 Answers2

1

The word for an object, field or property whose value doesn't change for its entire lifetime is immutable.

System.String is a classic example -- any time you need a new string value, you actually get an entirely new object.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
-2

you should try const. but in this case you should set initialized value.