C# Variables

Summary: in this tutorial, you’ll learn about C# variables including declaring variables, assigning values to variables, and displaying variables in the console.

Introduction to the C# variables

Programs process data. Typically, They work as follows:

  • First, get data from user inputs, files, or third-party API.
  • Second, process the data.
  • Third, output the result to the screen or save it to data storage such as a file or database.

To store data during the execution of the program, you use variables.

By definition, variables are identifiers whose values can change during the program’s execution. When the program ends, the values stored in the variables are also gone.

Declare variables

Before using a variable, you need to declare it using the following syntax:

type variableName;Code language: C# (cs)

In this syntax:

  • The type can be any C# built-in type or custom type. For example, the int built-in type represents the integers, and the string built-in type represents the text strings. Note that you’ll learn about the custom types later.
  • The variableName is a valid identifier that starts with a character or underscore (_) and is followed by other characters.

The following table illustrates the most commonly used built-in types:

TypeMeaningExamples
intIntegers1, 2, 3
floatSingle-precision floating-point numbers1.1F
doubleDouble-precision floating-point numbers2.5
stringText strings“Hi”
charCharacters‘a’, ‘b’,’c’
boolboolean valuestrue, false

By convention, variable names are in the camel case and start with a letter. For example color, textColor, and backgroundColor.

The following example declares a variable age with the type int:

int age;Code language: C# (cs)

When you declare a variable, it is initially unassigned. If you attempt to read from an unassigned variable, the compiler will issue an error.

After declaring a variable, you can assign a value to it using the assignment operator (=), like this:

int age;
age = 18;Code language: C# (cs)

And you can do both steps using one statement:

int age = 18;Code language: C# (cs)

The following increases the value of the age variable by one:

int age = 18;
age = age + 1;Code language: C# (cs)

After the second statement, the value of age is 19.

C# is a type-safe language. It means that the compiler will ensure that the variable will always store a value of the declared type.

In the above example, the age variable will always store an integer. If you assign it a string, the compiler will issue an error.

The following code will result in an error when compiling:

int age;
age = "one";Code language: C# (cs)

Error:

error CS0029: Cannot implicitly convert type 'string' to 'int'Code language: C# (cs)

Display variables

To output the age variable to the console, you use the Console.WriteLine method as follows:

int age = 18;
Console.WriteLine(age);Code language: C# (cs)

Output:

The age is 18Code language: C# (cs)

To embed the age variable in a string and display it, you use the following statement:

Console.WriteLine($"The age is {age}");Code language: C# (cs)

In this statement:

  • First, prefix the string with the $ symbol.
  • Second, place the variable (age) inside the curly braces {}.

When the compiler sees a string with the $ prefix, it’ll replace all the variables in the curly braces with their corresponding values.

Declare multiple variables

To declare multiple variables, you use multiple statements:

double weight = 60.5;
double height = 1.72;Code language: C# (cs)

Note that the double type represents the double-precision floating-point numbers.

If variables have the same type, you can declare them in one statement and use a comma (,) to separate two variables like this:

double weight = 60.5,
       height = 1.72;

Console.WriteLine($"The weight is {weight}kg and height is {height}m");Code language: C# (cs)

Output:

The weight is 60.5kg and height is 1.72mCode language: plaintext (plaintext)

C# variable example

The following program illustrates how to use variables to calculate the body mass index:

double weight = 60.5,
       height = 1.72,
       bmi;

// calculate BMI
bmi = weight / (height * height);

// output
Console.WriteLine("Calculate Body Mass Index (BMI)");
Console.WriteLine($"Weight: {weight}kg");
Console.WriteLine($"Height: {height}m");
Console.WriteLine($"BMI: {bmi:0.#}");Code language: C# (cs)

Output:

Calculate Body Mass Index (BMI)
Weight: 60.5kg
Height: 1.72m
BMI: 20.5Code language: plaintext (plaintext)

How it works.

First, declare three variables weight, height, and bmi:

double weight = 60.5,
       height = 1.72,
       bmi;Code language: C# (cs)

Second, calculate the body mass index (BMI) and store the result in the bmi variable:

// calculate BMI
bmi = weight / (height * height);Code language: C# (cs)

Third, show the output:

// output
Console.WriteLine("Calculate Body Mass Index (BMI)");
Console.WriteLine($"Weight: {weight}kg");
Console.WriteLine($"Height: {height}m");
Console.WriteLine($"BMI: {bmi:0.#}");Code language: C# (cs)

Note that the following syntax formats the BMI value that will show one digit after the decimal point:

{bmi:0.#}Code language: C# (cs)

If you want to format a number with more digits after the decimal point, you can add more of the # symbol. Each # symbol represents a number.

Summary

  • Variables are identifiers whose values change during the execution of a program.
  • By convention, variable names are in the camel case.
  • Use variables to store data in the program.
Was this tutorial helpful ?