Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Introduction to C#

Lecture 03

Prepared by: Ahmed Ali Fahim


Contents
➢ Implicit vs Explicit Variable declaration.
➢ Type Casting/Conversion
➢ How to accept user input and use it?
➢ What is an Operator?
➢ Arithmetic Operators
➢ Relational Operators
➢ Logical Operators
➢ Assignment Operators
➢ Unary Operators
Implicit vs Explicit Variable
using System;

namespace CSharpBeginnerCourse
{
class Program
{
static void Main(string[] args)
{
var userName = "CSharp";
string name = "CSharp";

var userAge = 20;


int age = 20;

Console.ReadLine();
}
}
}
Type Casting/Conversion
➢ Type conversion happens when we assign the value of one data type to another.
➢ If the data types are compatible, then C# does Automatic Type Conversion, also known as Implicit
Type Casting.
➢ If not comparable, then they need to be converted explicitly which is known as Explicit Type
Casting.
Automatic Type Conversion/Implicit Type Casting
Automatic Type Conversion happens in the following
cases:

➢ The two data types are compatible.


➢ When we assign value of a smaller data type to a
bigger data type.
Explicit Type Casting
➢ Explicit Type Casting is required when we want to assign a value of larger
data type to a smaller data type.
➢ It may result into the lossy conversion.
Explicit Type Casting
Accepting an users input and using it
using System;

namespace CSharpBeginnerCourse
{
class Program
{
static void Main(string[] args)
{
string input = Console.ReadLine ();
Console.WriteLine (input);
Console.ReadLine ();
}
}
}
Operators
➢ Operators are symbols that are used to perform operations on operands.
➢ Operands may be variables and/or constants.
➢ For example, in 6 + 4, + is an operator that is used to carry out addition operation, while 6 and 4 are
operands.
Types of Operator
➢ Arithmetic Operators
➢ Relational Operators
➢ Logical Operators
➢ Assignment Operators
➢ Unary Operators
➢ Ternary Operator (Will be discussed on next lecture)
➢ Bitwise and Bit Shift Operators (Won’t be covered)
Arithmetic Operators
➢ Addition Operator ( + )
➢ Subtraction Operator ( - )
➢ Multiplication Operator ( * )
➢ Division Operator ( / )
➢ Modulo Operator [Remainder] ( % )
Relational Operators
➢ Equal to ( == )
➢ Not equal to ( != )
➢ Greater than ( > )
➢ Less than ( < )
➢ Greater than or equal to ( >= )
➢ Less than or equal to ( <= )
Logical Operators
➢ Logical AND Operator ( && )
➢ Logical OR Operator ( || )
➢ Logical Negation Operator ( ! )
Assignment Operators
Basic Assignment

➢ =

Compound Assignment

➢ +=
➢ -=
➢ *=
➢ /=
➢ %=
Unary Operators
The unary operators operates on a single operand.

➢ Unary plus ( + )
➢ Unary minus ( - )
➢ Increment( ++ )
○ Postfix increment
○ Prefix increment
➢ Decrement ( -- )
○ Postfix decrement
○ Prefix decrement
References
➢ https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var
➢ https://www.geeksforgeeks.org/var-keyword-in-c-sharp/
➢ https://www.geeksforgeeks.org/c-sharp-type-casting/
➢ https://www.w3schools.com/cs/cs_operators.php
➢ https://www.javatpoint.com/csharp-operators
➢ https://www.tutorialspoint.com/csharp/csharp_operators.htm
➢ https://www.geeksforgeeks.org/c-sharp-operators/
➢ https://youtu.be/SvD3m4apJZ4
➢ https://www.programiz.com/csharp-programming/operators

You might also like