C# Coding Bootcamp VariablesApril 11th, 2024
C# Coding Bootcamp Variables
Watch our C# Variable Lecture Video from our C# Coding Bootcamp:
Why:
At the end of the 16th century, François Viète introduced the idea of representing known and unknown numbers by letters, nowadays called variables, and of computing with them as if they were numbers, in order to obtain the result by a simple replacement. In reference to programming, values being used in the program are able to be remembered more easily.
Essentially, variables play an important role as they allow us as programmers to write flexible programs. Instead of entering data directly into a program, a programmer can use variables to represent the data.
A variable is a container that stores some value.
Note: The opposite of a variable is a constant. Constants are values that never change.
What:
Variables keep track of the data throughout the program. It is like a container to store the information. It is used to store, retrieve, and modify changeable data.
Variables consist of a data-type, a variable name and a value (initialized by providing a value):
SYNTAX:
datatype variableName; // Declaration
variableName = value; // Initialization
datatype variableName = value; // Declaration & Initialization Syntax
Variables are always assigned with a data-type, meaning it holds the value of a specific type, such as string, bool, int and so on. C# is strongly-typed meaning once a variable has a type, that type cannot change. C# is also statically-typed meaning every variable must have a type.
C# Variable Naming Rules/Conventions:
You can’t just choose any sequence of characters as a variable name. Instead, C# has some rules regarding variable names that are to be followed:
- camelCase for local variables, such as cost, firstName, dateOfBirth, and petName.
- A meaningful or descriptive name that is neither too long nor too short to identify the information stored in a variable just by looking at it
- Can contain the letters a–z and A–Z, numbers 0–9, and the underscore (_) character..other symbols are not allowed.
- Cannot have spaces
- and cannot start with a number.
- Cannot use a word reserved by C# language—keywords such as namespace, class, using, and so on...
Invalid Names:
- ~Valid$PascalName! (symbols)
- 1 (starts with a number)
- class, while, if, protected (keyword)
- 1order, 1name (starts with a number)
- -validName (starts with a dash)
How:
Declaring a variable
Create a variable by declaring its type and then giving it a name using the syntax below.
When you declare a variable, the computer knows that it has to reserve a place in its memory for this variable.
// dataType variableName;
int y;
Assigning a value to a variable
To initialize a variable, you need to assign it a value. This is done by naming the variable followed by an equal sign (=) and then the value.
// variableName = value;
y = -20;
Note: The term initialize means to assign an initial value.
Variable Declaration and Initialization Syntax Combined on a Single Line
// dataType variableName = variableValue;
int x = 10;
Here, the datatype must be one of the valid data types, and variableName is the identifier used for the variable. This is an explicitly typed local variable where the type is explicitly defined
- string name;
- int age;
- int weight;
- bool isMarried;