C# 11 Variables and Constants

It is impossible to gain proficiency in any programming language without learning some basics. Perhaps the most basic aspect of programming involves using variables and constants. Even the most advanced and impressive programs, from high-end video games to enterprise commerce applications, use variables in one form or another.

This chapter will cover everything a C# programmer needs to know about variables and constants.

What is a C# Variable?

Variables are essentially locations in computer memory that are reserved for storing the data used by an application. Each variable is given a name by the programmer and assigned a value. The name assigned to the variable may then be used in the C# code to access the value assigned to the variable. This access can involve either reading the variable’s value or changing the value. It is, of course, the ability to change the value of variables that gives them the name variable.

Type annotations and implicit typing

C# is categorized as a type-safe programming language. This essentially means that once the data type of a variable has been identified, that variable cannot subsequently be used to store data of any other type without inducing a compilation error. This contrasts with loosely typed programming languages where a variable, once declared, can subsequently be used to store other data types.

There are two ways in which the type variable will be identified. One approach is to use a type annotation when the variable or constant is declared in the code. This is achieved by preceding the variable name with the type declaration. The following line of code, for example, declares a variable named userCount as being of type int (integer):

int userCount = 10;

When using type annotation, a value may be assigned to the variable after it has been declared:

int userCount;
userCount = 10;

As an alternative to type annotation in a declaration, the C# compiler can use a technique called implicit typing to identify the variable type. When relying on implicit typing, the compiler looks to see what type of value is assigned to the variable when it is initialized and uses that as the type. To instruct the compiler to use implicit typing, you must precede the variable name with the var keyword.

Consider, for example, the following variable declarations:

var signalStrength = 22;
var companyName = "My Company";

During the compilation of the above lines of code, C# will infer that the signalStrength variable is of type int and that companyName is of type string.

When a variable is declared using implicit typing, it must be assigned a value at the point of declaration. The following, for example, is not valid C# code:

var bookTitle;
bookTitle = "C# Programming Essentials";

A new value may be assigned to a variable at any point after it has been declared:

int interestRate = 5; //Declare the variable and initialize it to 5
interestRate = 10; // variable now equals 10

What is a C# Constant?

A constant is similar to a variable, providing a named location in memory to store a data value. Constants differ in one significant way: once a value has been assigned to a constant, it cannot subsequently be changed.

Constants are particularly useful if a value is used repeatedly throughout the application code. Rather than using the value each time, it makes the code easier to read if the value is first assigned to a constant which is then referenced in the code. For example, it might not be clear to someone reading your C# code why you used the value 5 in an expression. If instead of the value 5, you use a constant named interestRate, the purpose of the value becomes much clearer. Constants also have the advantage that if the programmer needs to change a widely used value, it only needs to be changed once in the constant declaration and not each time it is referenced.

As with variables, constants have a type, a name, and a value. Unlike implicitly typed variables, constants must always be initialized at the same time that they are declared and must be prefixed with the const keyword:

const int interestRate = 10;

Note that a constant must be initialized at the point that it is declared. For example, the following code will fail to compile because it declares a constant without assigning a value:

const int interestRate; // Invalid - a const must be initialized at creation time

Categories