Const constants:

There are two advantages to creating const constants. The first is that they are easier to read and modify than numbers because of the meaningful name. The second advantage is that, since the editor guarantees that its value remains fixed throughout the program, const constants are more robust than variables.

The syntax for declaring a const is as follows:

Access modifier Constant keyword Data type Constant name = Initialize;

Public const double PI = 3.1415926;

A const constant can only be initialized when it is declared, and cannot be assigned elsewhere. Its value remains the same during runtime. It is important to note that a const member of a class is implicitly static. In fact, the static nature of const is code level, because when the compiler generates IL, the reference to const is replaced by the corresponding value. This breaks the dependency relationship between const and its type. It is not reasonable to declare with the static keyword.)

Readonly constants

The const constant is implicitly static and is shared by all objects of the same class. All objects have the same value. If O1 and O2 are objects of Clircle, then their const PI has the same size, 3.1415926

Sometimes we need a variable that is fixed in the object of the class, but can have different values in different object aggregates. This constant can be implemented using the readOnly constant

  • Const: must be initialized when declared; Readonly: The value may not be initialized and may be deferred to the constructor.
  • Cons: It is parsed at compile time and replaces the value of the constant with the initialized value; Readonly: delay until run time.
  • Const: Focus on efficiency; Readonly: Pay attention to flexibility.
  • Const: no memory consumption; Readonly: Memory consumption because constants need to be stored

For example, if a hotel has a fixed number of rooms, but different hotels may have different numbers of rooms, we need to use readonly as a constant

namespace ReadonlyDemo
{
    /// <summary>
    ///Create a hotel class
    /// </summary>
    class Hotel
    {
        public readonly int roomNum;  // total number of rooms
        public int guestNun;          // Indicates the number of checked in rooms
       
        public Hotel(int roomNumValue) {  // The constructor
            roomNum = roomNumValue;
        }  
        public bool isFull()
        {
            if(guestNun >= roomNum)
            {
                return true;
            }
            else
            {
                return false; }}// One guest checking in
        public void LodgeIn()
        {
            if (isFull())
            {
                Console.WriteLine("The room is full");
            }
            else
            {
                guestNun++;
                Console.WriteLine("Check-in successful"); }}}class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("-- -- -- -- -- -- -- -- -- HOT1 hotel -- -- -- -- -- -- -- -- -- --");
            Hotel hot1 = new Hotel(6);  // The Hot1 hotel has six rooms
            for (int i = 0; i < 7; i++)
            {
                hot1.LodgeIn();  // Check-in guests
            }
           
            Console.WriteLine("HOT1 Hotel room total :{0}", hot1.roomNum);
            Console.WriteLine("HOT1 hotel has been checked in: {0} people", hot1.guestNun);

            Console.WriteLine("-- -- -- -- -- -- -- -- -- Tel1 hotel -- -- -- -- -- -- -- -- -- --");
            Hotel tel1 = new Hotel(5);  // The Tel1 hotel has ten rooms
            for (int i = 0; i < 3; i++)
            {
                tel1.LodgeIn();  // Check-in guests
            }
            Console.WriteLine("Total number of hotel rooms in Tel1 :{0}", tel1.roomNum);
            Console.WriteLine("Tel1 hotel has been checked in: {0} person", tel1.guestNun); Console.ReadLine(); }}}Copy the code