SlideShare a Scribd company logo
C#
LECTURE
Abid Kohistani.
C# Operators
Operators are used to perform operations on variables and values.
In the example below, we use the + operator to add together two values:
Although the + operator is often used to add together two values, like in the example above, it can also be
used to add together a variable and a value, or a variable and another variable:
int x = 100 + 50;
int sum1 = 100 + 50; // 150 (100 + 50)
int sum2 = sum1 + 250; // 400 (150 + 250)
int sum3 = sum2 + sum2; // 800 (400 + 400)
Arithmetic Operators
Operator Name Description Example
+ Addition Adds together two values x + y
- Subtraction Subtracts one value from another x - y
* Multiplication Multiplies two values x * y
/ Division Divides one value by another x / y
% Modulus Returns the division remainder x % y
++ Increment Increases the value of a variable by 1 x++
-- Decrement Decreases the value of a variable by 1 x--
Arithmetic operators are used to perform common mathematical operations:
Assignment Operators
◦ Assignment operators are used to assign values to variables.
◦ In the example below, we use the assignment operator (=) to assign the value 10 to a variable called x:
◦ The addition assignment operator (+=) adds a value to a variable:
int x = 10;
int x = 10;
x += 5;
A list of all assignment operators:
Operator Example Same As
= x = 5 x = 5
+= x += 3 x = x + 3
-= x -= 3 x = x - 3
*= x *= 3 x = x * 3
/= x /= 3 x = x / 3
%= x %= 3 x = x % 3
&= x &= 3 x = x & 3
|= x |= 3 x = x | 3
^= x ^= 3 x = x ^ 3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
Comparison Operators
Operator Name Example
== Equal to x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
Comparison operators are used to compare two values:
Logical Operators
Operator Name Description
&& Logical and Returns true if both statements are true
|| Logical or Returns true if one of the statements is true
! Logical not Reverse the result, returns false if the result is true
Logical operators are used to determine the logic between variables or values:
Strings
Strings are used for storing text.
A string variable contains a collection of characters surrounded by double quotes:
String Length: A string in C# is actually an object, which contain properties and methods that can perform certain
operations on strings. For example, the length of a string can be found with the Length property:
string greeting = "Hello";
string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Console.WriteLine("The length of the txt string is: " + txt.Length);
Strings Cont..
There are many string methods available, for example ToUpper() and ToLower(), which returns a copy of
the string converted to uppercase or lowercase:A string variable contains a collection of characters
surrounded by double quotes:
String Concatenation: The + operator can be used between strings to combine them. This is called
concatenation:
string txt = "Hello World";
Console.WriteLine(txt.ToUpper()); // Outputs "HELLO WORLD"
Console.WriteLine(txt.ToLower()); // Outputs "hello world"
string firstName = "John ";
string lastName = "Doe";
string name = firstName + lastName;
Console.WriteLine(name) ;
Strings Cont..
String Concatenation: You can also use the string.Concat() method to concatenate two strings:
String Interpolation :Another option of string concatenation, is string interpolation, which substitutes values
of variables into placeholders in a string. Note that you do not have to worry about spaces, like with
concatenation
Also note that you have to use the dollar sign ($) when using the string interpolation method.
String interpolation was introduced in C# version 6.
string firstName = "John ";
string lastName = "Doe";
string name = string.Concat(firstName, lastName);
Console.WriteLine(name);
string firstName = "John";
string lastName = "Doe";
string name = $"My full name is: {firstName} {lastName}";
Console.WriteLine(name);
Strings Cont..
Access Strings: You can access the characters in a string by referring to its index number inside square
brackets [ ].
You can also find the index position of a specific character in a string, by using the IndexOf() method:
string myString = "Hello";
Console.WriteLine(myString[0]); // Outputs "H"
Note: String indexes start with 0: [0] is the first character. [1] is the second character,
etc.
string myString = "Hello"; Console.WriteLine(myString.IndexOf("e")); // Outputs "1"
Special Characters
The backslash () escape character turns special characters into string characters:
Escape character Result Description
' ' Single quote
" " Double quote
  Backslash
string txt = "We are the so-called "Vikings" from the north.";
Special Characters Cont..
Other useful escape characters in C# are:
Code Result
n New Line
t Tab
b Backspace
END

More Related Content

PPTX
Chapter 3.3
PDF
Python list
ODT
Introduction to biopython
PDF
Python List Comprehensions
PDF
Recursion Lecture in C++
PPTX
List in Python
PPTX
Basic Sorting algorithms csharp
PPTX
Python Lecture 8
Chapter 3.3
Python list
Introduction to biopython
Python List Comprehensions
Recursion Lecture in C++
List in Python
Basic Sorting algorithms csharp
Python Lecture 8

What's hot (20)

PPTX
Python Lecture 11
PPTX
Data Structures in Python
PDF
Python set
PPTX
Python Lecture 10
PPTX
7 searching injava-binary
PDF
List,tuple,dictionary
PDF
List , tuples, dictionaries and regular expressions in python
PDF
Data type list_methods_in_python
PDF
Arrays in python
PDF
Python tuple
PDF
Associativity of operators
PPTX
Basic python part 1
PPTX
Sequence Types in Python Programming
PDF
Python programming : Strings
PDF
Python Regular Expressions
PDF
The Ring programming language version 1.5.2 book - Part 18 of 181
PPTX
C# Arrays
Python Lecture 11
Data Structures in Python
Python set
Python Lecture 10
7 searching injava-binary
List,tuple,dictionary
List , tuples, dictionaries and regular expressions in python
Data type list_methods_in_python
Arrays in python
Python tuple
Associativity of operators
Basic python part 1
Sequence Types in Python Programming
Python programming : Strings
Python Regular Expressions
The Ring programming language version 1.5.2 book - Part 18 of 181
C# Arrays
Ad

Similar to C# Operators. (C-Sharp Operators) (20)

PPTX
3 operators-expressions-and-statements-120712073351-phpapp01
PPTX
3 operators-expressions-and-statements-120712073351-phpapp01
PPTX
03. operators and-expressions
PPTX
03. Operators Expressions and statements
PPTX
Lesson 3: Variables and Expressions
PPTX
Unit2_3.pptx Chapter 2 Introduction to C#
PPT
03 Operators and expressions
PPS
02 iec t1_s1_oo_ps_session_02
PPTX
Operators expressions-and-statements
PPT
13 Strings and text processing
PPTX
Operator C# - Lec3 (Workshop on C# Programming: Learn to Build)
PDF
C# Dot net unit-2.pdf
PDF
C# simplified
PPT
Operators and Expressions in C#
PPT
Lecture 2
PPTX
11operator in c#
PPTX
16 strings-and-text-processing-120712074956-phpapp02
PPTX
2.overview of c#
PPTX
C# overview part 1
PPTX
C sharp part 001
3 operators-expressions-and-statements-120712073351-phpapp01
3 operators-expressions-and-statements-120712073351-phpapp01
03. operators and-expressions
03. Operators Expressions and statements
Lesson 3: Variables and Expressions
Unit2_3.pptx Chapter 2 Introduction to C#
03 Operators and expressions
02 iec t1_s1_oo_ps_session_02
Operators expressions-and-statements
13 Strings and text processing
Operator C# - Lec3 (Workshop on C# Programming: Learn to Build)
C# Dot net unit-2.pdf
C# simplified
Operators and Expressions in C#
Lecture 2
11operator in c#
16 strings-and-text-processing-120712074956-phpapp02
2.overview of c#
C# overview part 1
C sharp part 001
Ad

More from Abid Kohistani (9)

PPT
Algorithm in Computer, Sorting and Notations
PPTX
Polymorphism in C# Function overloading in C#
PPTX
Exception Handling in C#
PPTX
Access Modifiers in C# ,Inheritance and Encapsulation
PPTX
OOP in C# Classes and Objects.
PPTX
Loops in C# for loops while and do while loop.
PPTX
Conditions In C# C-Sharp
PPTX
data types in C-Sharp (C#)
PPTX
Methods In C-Sharp (C#)
Algorithm in Computer, Sorting and Notations
Polymorphism in C# Function overloading in C#
Exception Handling in C#
Access Modifiers in C# ,Inheritance and Encapsulation
OOP in C# Classes and Objects.
Loops in C# for loops while and do while loop.
Conditions In C# C-Sharp
data types in C-Sharp (C#)
Methods In C-Sharp (C#)

Recently uploaded (20)

PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Electronic commerce courselecture one. Pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
KodekX | Application Modernization Development
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Machine learning based COVID-19 study performance prediction
PPT
Teaching material agriculture food technology
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Encapsulation theory and applications.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Electronic commerce courselecture one. Pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
KodekX | Application Modernization Development
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Machine learning based COVID-19 study performance prediction
Teaching material agriculture food technology
Reach Out and Touch Someone: Haptics and Empathic Computing
NewMind AI Monthly Chronicles - July 2025
Chapter 3 Spatial Domain Image Processing.pdf
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Per capita expenditure prediction using model stacking based on satellite ima...
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Understanding_Digital_Forensics_Presentation.pptx
Encapsulation theory and applications.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Review of recent advances in non-invasive hemoglobin estimation
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...

C# Operators. (C-Sharp Operators)

  • 2. C# Operators Operators are used to perform operations on variables and values. In the example below, we use the + operator to add together two values: Although the + operator is often used to add together two values, like in the example above, it can also be used to add together a variable and a value, or a variable and another variable: int x = 100 + 50; int sum1 = 100 + 50; // 150 (100 + 50) int sum2 = sum1 + 250; // 400 (150 + 250) int sum3 = sum2 + sum2; // 800 (400 + 400)
  • 3. Arithmetic Operators Operator Name Description Example + Addition Adds together two values x + y - Subtraction Subtracts one value from another x - y * Multiplication Multiplies two values x * y / Division Divides one value by another x / y % Modulus Returns the division remainder x % y ++ Increment Increases the value of a variable by 1 x++ -- Decrement Decreases the value of a variable by 1 x-- Arithmetic operators are used to perform common mathematical operations:
  • 4. Assignment Operators ◦ Assignment operators are used to assign values to variables. ◦ In the example below, we use the assignment operator (=) to assign the value 10 to a variable called x: ◦ The addition assignment operator (+=) adds a value to a variable: int x = 10; int x = 10; x += 5;
  • 5. A list of all assignment operators: Operator Example Same As = x = 5 x = 5 += x += 3 x = x + 3 -= x -= 3 x = x - 3 *= x *= 3 x = x * 3 /= x /= 3 x = x / 3 %= x %= 3 x = x % 3 &= x &= 3 x = x & 3 |= x |= 3 x = x | 3 ^= x ^= 3 x = x ^ 3 >>= x >>= 3 x = x >> 3 <<= x <<= 3 x = x << 3
  • 6. Comparison Operators Operator Name Example == Equal to x == y != Not equal x != y > Greater than x > y < Less than x < y >= Greater than or equal to x >= y <= Less than or equal to x <= y Comparison operators are used to compare two values:
  • 7. Logical Operators Operator Name Description && Logical and Returns true if both statements are true || Logical or Returns true if one of the statements is true ! Logical not Reverse the result, returns false if the result is true Logical operators are used to determine the logic between variables or values:
  • 8. Strings Strings are used for storing text. A string variable contains a collection of characters surrounded by double quotes: String Length: A string in C# is actually an object, which contain properties and methods that can perform certain operations on strings. For example, the length of a string can be found with the Length property: string greeting = "Hello"; string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; Console.WriteLine("The length of the txt string is: " + txt.Length);
  • 9. Strings Cont.. There are many string methods available, for example ToUpper() and ToLower(), which returns a copy of the string converted to uppercase or lowercase:A string variable contains a collection of characters surrounded by double quotes: String Concatenation: The + operator can be used between strings to combine them. This is called concatenation: string txt = "Hello World"; Console.WriteLine(txt.ToUpper()); // Outputs "HELLO WORLD" Console.WriteLine(txt.ToLower()); // Outputs "hello world" string firstName = "John "; string lastName = "Doe"; string name = firstName + lastName; Console.WriteLine(name) ;
  • 10. Strings Cont.. String Concatenation: You can also use the string.Concat() method to concatenate two strings: String Interpolation :Another option of string concatenation, is string interpolation, which substitutes values of variables into placeholders in a string. Note that you do not have to worry about spaces, like with concatenation Also note that you have to use the dollar sign ($) when using the string interpolation method. String interpolation was introduced in C# version 6. string firstName = "John "; string lastName = "Doe"; string name = string.Concat(firstName, lastName); Console.WriteLine(name); string firstName = "John"; string lastName = "Doe"; string name = $"My full name is: {firstName} {lastName}"; Console.WriteLine(name);
  • 11. Strings Cont.. Access Strings: You can access the characters in a string by referring to its index number inside square brackets [ ]. You can also find the index position of a specific character in a string, by using the IndexOf() method: string myString = "Hello"; Console.WriteLine(myString[0]); // Outputs "H" Note: String indexes start with 0: [0] is the first character. [1] is the second character, etc. string myString = "Hello"; Console.WriteLine(myString.IndexOf("e")); // Outputs "1"
  • 12. Special Characters The backslash () escape character turns special characters into string characters: Escape character Result Description ' ' Single quote " " Double quote Backslash string txt = "We are the so-called "Vikings" from the north.";
  • 13. Special Characters Cont.. Other useful escape characters in C# are: Code Result n New Line t Tab b Backspace
  • 14. END