Professional Documents
Culture Documents
C# Lecture 03 Type Casting, Explicit Vs Implicit Types and Operators (06-10-2021)
C# Lecture 03 Type Casting, Explicit Vs Implicit Types and Operators (06-10-2021)
Lecture 03
namespace CSharpBeginnerCourse
{
class Program
{
static void Main(string[] args)
{
var userName = "CSharp";
string name = "CSharp";
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:
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