Constants

The values of constants are known at compile time and do not change
They are immutable values
Constants are accessed as if they were static fields although they do require the static keyword
Constants must be value types

public class MyClass 
{
   const string name = "Monday";
   private const int months = 12;
}

Multiple Constants

You can declare multiple constants on the same line if they have the same data type.

const int months = 12, weeks = 52. days = 365; 

Expressions

You can use an expression to initialize a constant by referring to another constant

const int months = 12; 
const int daysinmonth = 30;
const int daysinyear = months * daysinmonth


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext