CodeBlanks Logo Icon CodeBlanks

Summary - Class constants

C# course · Intermediate · Lesson 41/116

Combine const, readonly and enum to write a MathConstants class.

Summary - Class constants

In this group you learned C#'s immutable-value tools: const is a compile-time constant, readonly a runtime immutable, static readonly a shared runtime immutable and enum a set of named constants.

const causes versioning issues in public APIs; static readonly is preferred there. readonly suits per-instance immutable values. enum groups related constants type-safely and prevents numeric mix-ups.

Exercise Constants class (summary)

Define a static class Constants. Add a public const int MaxSize = 10; field. In Main, print Constants.MaxSize.

Show example solution
Add `static class Constants { public const int MaxSize = 10; }`. In Main write `Console.WriteLine(Constants.MaxSize);`.
Example solution:
using System;

static class Constants
{
    public const int MaxSize = 10;
}

class Program
{
    static void Main()
    {
        Console.WriteLine(Constants.MaxSize);
    }
}

Exercise Configuration class (summary)

Add a public readonly string Environment; field to Configuration. The constructor should assign this.Environment = env;. In Main, create new Configuration("Test") and print Environment.

Show example solution
Add `public readonly string Environment;` to Configuration. In the ctor write `this.Environment = env;`. In Main write `var c = new Configuration("Test"); Console.WriteLine(c.Environment);`.
Example solution:
using System;

class Configuration
{
    public readonly string Environment;

    public Configuration(string env)
    {
        this.Environment = env;
    }
}

class Program
{
    static void Main()
    {
        var c = new Configuration("Test");
        Console.WriteLine(c.Environment);
    }
}

Next step

Class constants keep immutable values tidy and type-safe. const is fast and embedded; readonly is flexible; enum groups related options. The next group will combine these in a real Quiz app project with constant score values, readonly quiz settings and enum states.

Remember: static readonly for public APIs, const for internal constants, readonly for per-instance immutability.

Test yourself

  1. Which statement about class constants is correct?

    • const is assigned in a constructor.
    • const is implicitly static, readonly is an instance member.
    • readonly embeds the value at compile time.
    • enum only holds string values.
    Show answer

    const is implicitly static, readonly is an instance member. — Correct. const is implicitly static; readonly is an instance member assigned in the ctor.

  2. Fill in the blank: readonly in constructor

    class Configuration
    {
        public readonly string Environment;
    
        public Configuration(string env)
        {
            this.____ = env;
        }
    }
    Show answer
    class Configuration
    {
        public readonly string Environment;
    
        public Configuration(string env)
        {
            this.Environment = env;
        }
    }

All lessons in Intermediate