Analyzing Constants in C and How to Define Constants in C Programming

  • 2021-09-05 00:40:54
  • OfStack

Constants are immutable values that are known at compile time and do not change during the lifetime of a program. Constants are declared using the const modifier. Only C # built-in types (except System. Object) can be declared as const.
User-defined types (including classes, structures, and arrays) cannot be const. Use the readonly modifier to create a class, structure, or array that is initialized once at run time and cannot be changed again.
C # does not support const methods, properties, or events.
You can use enumeration types to define named constants for integer built-in types such as int, uint, long, and so on.
Constants must be initialized when declared. For example:

class Calendar1
  public const int months = 12;

In this example, the constant months is always 12 and cannot be changed, even if the class itself cannot change it. In fact, when the compiler encounters a constant modifier in C # source code (for example, months), it will directly replace the text value with the intermediate language (IL) code it generates. Because there is no variable address associated with a constant at run time, the const field cannot be passed by reference and cannot appear as a left value in an expression.
System_CAPS_note Note
Be careful when referring to constant values defined in other code, such as DLL. If a new version of DLL defines a new value for a constant, the program retains the old text value until the program is recompiled for the new version.
You can declare multiple constants of the same type at the same time, for example:

class Calendar2
  const int months = 12, weeks = 52, days = 365;

An expression that initializes one constant can refer to another constant if it does not cause a circular reference. For example:

class Calendar3
  const int months = 12;
  const int weeks = 52;
  const int days = 365;

  const double daysPerWeek = (double) days / (double) weeks;
  const double daysPerMonth = (double) days / (double) months;

Constants can be labeled public, private, protected, internal, or protectedinternal. These access modifiers define how users of the class access the constant. For more information, see Access Modifiers (C # Programming Guide).
Because the constant value is the same for all instances of the type, the constant is accessed as static Field 1. Constants are not declared using the static keyword. Expressions that are not included in the class that defines the constant must use the class name, 1 period, and constant name to access the constant. For example:

int birthstones = Calendar.months;

How to Define Constants in C #
A constant is a field whose value is set at compile time and whose value can never be changed. Use constants to provide meaningful names for special values instead of numeric text ("magic numbers").
To define constant values of integer types (int, byte, and so on), use enumerated types. For more information, see enum (C # reference).
One way to define non-integer constants is to group them into a single static class named Constants. This requires that all references to constants be prefixed with the class name, as shown in the following example.

static class Constants
  public const double Pi = 3.14159;
  public const int SpeedOfLight = 300000; // km per sec.

class Program
  static void Main()
    double radius = 5.3;
    double area = Constants.Pi * (radius * radius);
    int secsFromSun = 149476000 / Constants.SpeedOfLight; // in km

Using the class name qualifier helps ensure that you and others who use a constant understand that it is a constant and cannot be modified.

Related articles: