Advanced C# Part 4
✓ Unique Operators (Conditional, safe navigation)
✓ LINQ
✓ Synchronous programming
✓ Asynchronous programming
✓ Multithreading
✓ Concurrent collections
Ahmad Mohey | Full Stack Developer
E-mail :
Twitter : ahmadmohey85
Advanced C# Part 4
Nullable types
Ahmad Mohey | Full Stack Developer
E-mail :
Twitter : ahmadmohey85
Advanced C# Part 4
Conditional operator (Ternary operator)
Ahmad Mohey | Full Stack Developer
E-mail :
Twitter : ahmadmohey85
Advanced C# Part 4
Conditional operator exercise (Even or Odd)
Ahmad Mohey | Full Stack Developer
E-mail :
Twitter : ahmadmohey85
Advanced C# Part 4
Safe navigation operator
Ahmad Mohey | Full Stack Developer
E-mail :
Twitter : ahmadmohey85
Advanced C# Part 4
Introduction to LINQ
Ahmad Mohey | Full Stack Developer
E-mail :
Twitter : ahmadmohey85
Introduction to LINQ
LINQ stands for Language-Integrated Query, it is a query syntax used to bridges the
gap between the world of objects and the world of data.
LINQ to Objects
LINQ to DataSet
LINQ to XML
LINQ to Entities
LINQ to SQL
Method Syntax
Query Syntax
Introduction to LINQ
Jack Steven, 5000, 35
Debora Watson, 10000, 45
Claire Adam, 7500, 32
Michael Rob, 3500, 22
Matthew Forest, 4500, 28
Charles Kris, 8000, 27
Robert Wilson, 8000, 28
Emma Brooks, 6000, 23
Jennifer Blake, 3000, 38
Terry Loyd, 9000, 49
Adam Spencer, 7000, 33
Stacy Shelton, 4000, 25
foreach (var employee in employees)
{
if(employee.Salary>=6000 && employee.Age>=40)
Console.WriteLine(employee.Name);
}
Introduction to LINQ
Familiar language: you don’t have to learn a new query language for each type of data source or data format.
Less coding: It reduces the amount of code comparing to the normal approach.
Readable code: LINQ makes the code more readable.
Standardization: The same LINQ syntax can be used to query multiple data sources.
Advanced C# Part 4
LINQ - method syntax
Ahmad Mohey | Full Stack Developer
E-mail :
Twitter : ahmadmohey85
Advanced C# Part 4
LINQ - method syntax exercise
Ahmad Mohey | Full Stack Developer
E-mail :
Twitter : ahmadmohey85
LINQ - method syntax exercise
Salary about higher than 4000 and last appraisal less than 8
Use the DisplayWithAppraisal method
Advanced C# Part 4
LINQ - query syntax
Ahmad Mohey | Full Stack Developer
E-mail :
Twitter : ahmadmohey85
Advanced C# Part 4
LINQ - query syntax exercise
Ahmad Mohey | Full Stack Developer
E-mail :
Twitter : ahmadmohey85
LINQ - query syntax exercise
Salary about higher than 4000 and last appraisal less than 8
Use the DisplayWithAppraisal method
Advanced C# Part 4
Sorting data using LINQ
Ahmad Mohey | Full Stack Developer
E-mail :
Twitter : ahmadmohey85
Advanced C# Part 4
LINQ queries with methods
Ahmad Mohey | Full Stack Developer
E-mail :
Twitter : ahmadmohey85
Advanced C# Part 4
TimeSpan
Ahmad Mohey | Full Stack Developer
E-mail :
Twitter : ahmadmohey85
Advanced C# Part 4
Introduction to multithreading
Ahmad Mohey | Full Stack Developer
E-mail :
Twitter : ahmadmohey85
Introduction to multithreading
Sequential programming
All the programs that we were developing are sequential programs each has a
• Beginning
• Execution sequence
• End
Single thread
A thread is a single sequential flow of control within a program.
Introduction to multithreading
Multithreading
is a type of execution model that allows multiple threads to exist within the process and they
execute independently but share their process resources
Visual Studio
Thread 1
Responding to
keystrokes
Thread 2
Checking Syntax
Thread 3
Displaying Intellisense
Thread 4
Offering Solutions for
errors
Introduction to multithreading
Advantages of multithreading
Responsiveness: multithreading allow an application to remain responsive.
Faster execution: multithreaded applications operate faster on computers that have multiple CPUs.
Lower resource consumption: multithreaded applications can handle multiple requests simultaneously using fewer resources.
Better system utilization : multithreaded applications can be doing different tasks at the same time not in a sequential order.
Disadvantages of multithreading
Complexity: Increases the complexity of your application.
Difficulty to write code: because you are place each task on a separate independent thread.
Difficulty to debug code: because the application will not work in a sequential way anymore.
Difficulty to test code: for the same previous two reasons.
Potential deadlocks : when two or more threads are blocking each other.
Introduction to multithreading
Critical Section
is a section of code that needs to be executed without being interpreted.
For example
• A user trying to reserve the last ticket available on a plane.
• One thread is opening a file and another thread is writing in the file
Race Condition
Occurs when two or more threads try to manipulate a shared resource concurrently and outcome of the execution depends on
the particular order in which the access takes place.
To avoid race conditions, the execution of critical sections must be mutually exclusive
Advanced C# Part 4
Creating threads
Ahmad Mohey | Full Stack Developer
E-mail :
Twitter : ahmadmohey85
Advanced C# Part 4
Managing threads
Ahmad Mohey | Full Stack Developer
E-mail :
Twitter : ahmadmohey85