C# Variables
Data values are kept in containers called variables.
distinct sorts of variables (specified with distinct keywords) exist in C#. For instance:
int
– holds whole numbers, or integers, such as 123 or -123, without decimals.- double – stores floating point numbers, with decimals, such as 19.99 or -19.99
- char – keeps track of single characters like “a” or “B.” Single quotations around the values of char.
- string – Text like “Hello World” is stored. Double quotations surround string values.
- bool – holds values in the true or false state.
Declaring (Creating) Variables
You need to provide the type and give a variable a value in order to create it:
type variableName = value;
where variableName is the name of the variable (like x or name) and type is a C# type (like int or string). Values are assigned to the variable using the equal sign.
Take a look at the following example to learn how to construct a variable that will hold text:
Example
Take a look at the following example to learn how to build a variable that should hold a number:
Example
It is also possible to declare a variable without first assigning a value, then assign the value afterwards:
Example
int myNum;
myNum = 15;
Console.WriteLine(myNum);
Note that if you assign a new value to an existing variable, it will overwrite the prior value:
Example
Other Types
An example of declaring variables of different types:
int myNum = 5;
double myDoubleNum = 5.99D;
char myLetter = ‘D’;
bool myBool = true;
string myText = “Hello”;
Later on in this chapter, you will discover more about data types.
Constants
You can put the const keyword in front of the variable type if you don’t want other people (or yourself) to overwrite existing values.
Declaring the variable as “constant” will make it read-only and unchangeable:
Example
const int myNum = 15;
myNum = 20;// error
Display Variables
When displaying variable values in the console window, the WriteLine() function is frequently utilized.
Use the + symbol to merge text with a variable:
Example
string name = “gkits;
console.writeLine(“Hello” + name);
Additionally, you can add a variable to another variable by using the + symbol:
string firstName = “gkits”;
string Last = “student”;
string fullName = firstName + lastName;
Console.WriteLine(fullName);
The + symbol functions as a mathematical operator for numeric values (note that we are using int (integer) variables here):
Example
int Y = 6;
- x stores the value 5
- y stores the value 6
- Next, we show the value of x + y, which is 11, using the WriteLine() method.
Declare Many Variables
Example
int x, y, z ;
x = 5, y = 6, z =50;
C# Identifiers
Identifiers can be more descriptive names like age, sum, or totalVolume, or they can be simple names like x and y.
Note: To write comprehensible and maintainable code, it is advised to use descriptive names:
Example
- Letters, numbers, and the underscore character (_) can all be found in names.
- Names have to start with an underscore or letter.
- Names cannot begin with a space and must begin with a lowercase letter.
- Case matters when naming variables (“myVar” and “myvar” are distinct variables).
- Reserved terms cannot be used as names (such as C# keywords like int or double).
Data Types
Example
Data Type | Size | Description |
---|---|---|
int | 4 bytes | Stores whole numbers from -2,147,483,648 to 2,147,483,647 |
long | 8 bytes | Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
float | 4 bytes | Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits |
double | 8 bytes | Stores fractional numbers. Sufficient for storing 15 decimal digits |
bool | 1 bit | Stores true or false values |
char | 2 bytes | Stores a single character/letter, surrounded by single quotes |
string | 2 bytes per character | Stores a sequence of characters, surrounded by double quotes |
Numbers
Two categories exist for different number types:
Whole numbers, whether positive or negative (like 123 or -456), are stored as integer types without digits. Int and long are acceptable types. The numerical value determines the type to utilize.
Integer Types
Int
Example
Long
Example
Floating Point Types
Fractional numbers can be stored in the float and double data types. Keep in mind that a “F” should be used to terminate the value for floats and a “D” for doubles.
Float Example
Double Example
Should you use double or float?
The number of digits that can follow the decimal point in a floating point value is indicated by its precision. Whereas double variables have a precision of roughly 15 decimal places, float variables only have six or seven. For the majority of computations, it is therefore safer to utilize double.
Scientific Numbers
Example
Booleans
Example
char myGraade = “B”;
console.WriteLine(myGrade);
Strings
A text string is stored as a sequence of characters using the string data type. Double quotations must enclose string values: