You are on page 1of 15

C#

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

DECLARING (CREATING) VARIABLES

To create a variable, you must specify the type and assign it a value:

type variableName = value;


VARIABLES

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;

myNum = myNum + 10; is the same as myNum += 10


RULES FOR NAMING VARIABLES IN C#

Must contain letters (uppercase and lowercase), underscore( _ ) and digits only.

The variable name must start with either letter, underscore or @ symbol.

C# is case sensitive. It means age and Age refers to 2 different variables.

Variable cannot be C# keyword, For example, if, for, using can not be a variable


name.

VAR KEYWORD

var age = 17;
DATA TYPES
EXAMPLES - VARIABLES

Create a variable called "name" of type string and assign it the value "John".


Create a variable called "myNum" of type int and assign it the value 15.
Write a program to print Hello and your name (using variable) using string
concatenation (+).
Write above problem using string interpolation ($"{}").
Print the sum of 5 + 10, using two variables: x and y.
Create an int variable called c, assign a + b to it, and print the result.
Write a program to swap two numbers.*
USER INPUT / OUTPUT

Console.WriteLine() is used to output (print) values


Console.ReadLine() is used to get user input

Examples
Console.WriteLine("Enter username:");
string userName = Console.ReadLine();
Console.WriteLine("Username is: " + userName);

Console.WriteLine("Enter your age:");


int age = Console.ReadLine();
Console.WriteLine("Your age is: " + age);

Console.WriteLine("Enter your age:");


int age = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Your age is: " + age);
EXAMPLES - USER INPUT / OUTPUT

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)

Write a program that reads from the console the radius "r" of a circle and prints


its perimeter and area.
COMPARISON OPERATORS (==, <, >, <=, AND >=)

Less than operator <


Console.WriteLine(7.0 < 5.1); // output: False

Greater than operator <


Console.WriteLine(7.0 > 5.1); // output: True

Less than or equal operator <=


Console.WriteLine(7.0 <= 5.1); // output: False

Greater than or equal operator >=


Console.WriteLine(7.0 >= 5.1); // output: True

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

Postfix increment operator Postfix decrement operator


i nt i = 3; i nt i = 3;
Cons ol e. Wr i t eLi ne( i ) ; / / out put : 3 Cons ol e. Wr i t eLi ne( i ) ; / / out put : 3
Cons ol e. Wr i t eLi ne( i ++) ; / / out put : 3 Cons ol e. Wr i t eLi ne( i - - ) ; / / out put : 3
Cons ol e. Wr i t eLi ne( i ) ; / / out put : 4 Cons ol e. Wr i t eLi ne( i ) ; / / out put : 2

Prefix increment operator Prefix decrement operator


doubl e a = 1. 5; doubl e a = 1. 5;
Cons ol e. Wr i t eLi ne( a) ; / / out put : 1. 5 Cons ol e. Wr i t eLi ne( a) ; / / out put : 1. 5
Cons ol e. Wr i t eLi ne( ++a) ; / / out put : 2. 5 Cons ol e. Wr i t eLi ne( - - a) ; / / out put : 0. 5
Cons ol e. Wr i t eLi ne( a) ; / / out put : 2. 5 Cons ol e. Wr i t eLi ne( a) ; / / out put : 0. 5
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

Multiply 10 with 5, and print the result.


Divide 10 by 5, and print the result.
Write a program to print the sum of two numbers (numbers must
take as input).
Use the correct operator to increase the value of the
variable x by 1.
Use the addition assignment operator to add the value 5 to the
variable x.
Write a program to print the result of dividing two numbers.
EXAMPLES - ARITHMETIC OPERATORS

Write a C# Sharp program to print the result of the specified


operations.
-1 + 4 * 6
( 35+ 5 ) % 7
14 + -4 * 6 / 11
2 + 15 / 6 * 1 - 7 % 2

Write a program to print the output of multiplication of three


numbers which will be entered by the user.

You might also like