Professional Documents
Culture Documents
Basics
By Lusine Hovsepyan
UNDERSTANDING DATA TYPES AND
VARIABLES
What is Variable?
How to define variable?
How to set value to the variable?
Select appropriate data type
int, string varables
Assignment operator (=)
String concatanation operator (+)
Console.Write() vs Console.WriteLine()
Console.ReadLine()
Nameing conventions
VARIABLES
Variables are containers for storing data values.
Different types of variables:
int - stores integers (whole numbers), without decimals, such as 123 or -123
double - stores floating point numbers, with decimals, such as 19.99 or -19.99
char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
string - stores text, such as "Hello World". String values are surrounded by double quotes
bool - stores values with two states: true or false
To create a variable, you must specify the type and assign it a value:
Variables in C# must be declared before they can be used. This means, the name
and type of variable must be known before they can be assigned a value.
Once declared, the datatype of a variable can not be changed within a scope.
You can also declare a variable without assigning the value, and assign the value
later.
int myNum;
myNum = 15;
Console.WriteLine(myNum);
Assign a new value to an existing variable, it will overwrite the previous value.
Change the value of myNum to 20. myNum = 20;
Must contain letters (uppercase and lowercase), underscore( _ ) and digits only.
The variable name must start with either letter, underscore or @ symbol.
VAR KEYWORD
var age = 17;
DATA TYPES
EXAMPLES - VARIABLES
Examples
Console.WriteLine("Enter username:");
string userName = Console.ReadLine();
Console.WriteLine("Username is: " + userName);
Use Console.ReadLine and assign the result to the variable called "name".
Write a program that reads from the console three numbers of type int and prints their
sum.
A company has name, address, phone number, web site and manager. The manager has
name, surname and phone number. Write a program that reads information about the
company and its manager and then prints it on the console.
(use string interpolation)
Equality operator ==
Console.WriteLine(7.0 == 5.1); // output: False
Inequality operator !=
Console.WriteLine(7.0 != 5.1); // output: True
BOOLEAN OPERATORS
! operator
bool pas s ed = f al s e;
Cons ol e. Wr i t eLi ne( ! pas s ed) ; / / out put : Tr ue
CONDITIONAL OPERATORS
AND operator &&
bool a = f al s e && t r ue;
Cons ol e. Wr i t eLi ne( a) ; / / Out put : F al s e
OR operator ||
bool a = t r ue || t r ue;
Cons ol e. Wr i t eLi ne( a) ; / / Out put : Tr ue
ARITHMETIC OPERATORS
Multiplication operator *
i nt i = 3;
Cons ol e. Wr i t eLi ne( 5 * 2) ; / / out put : 10
Integer division /
Cons ol e. Wr i t eLi ne( 13 / 5) ; / / out put : 2
Cons ol e. Wr i t eLi ne( - 13 / 5) ; / / out put : - 2
Remainder operator %
Cons ol e. Wr i t eLi ne( 5 % 4) ; / / out put : 1
Addition operator +
Cons ol e. Wr i t eLi ne( 5 + 4) ; / / out put : 9
Subtraction operator -
Cons ol e. Wr i t eLi ne( 47 - 3) ; / / out put : 44
EXAMPLES - ARITHMETIC OPERATORS