0% found this document useful (0 votes)
119 views59 pages

C# Language Document

The document provides an overview of advanced C# topics including unique operators, LINQ, synchronous and asynchronous programming, multithreading, and concurrent collections. It covers conditional and safe navigation operators, introduces LINQ including LINQ to Objects and method/query syntax with examples. It also discusses synchronous vs asynchronous programming, creating and managing threads, multithreaded applications, and synchronization techniques like locks, monitors, mutexes and semaphores.

Uploaded by

Avinash Ganesan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views59 pages

C# Language Document

The document provides an overview of advanced C# topics including unique operators, LINQ, synchronous and asynchronous programming, multithreading, and concurrent collections. It covers conditional and safe navigation operators, introduces LINQ including LINQ to Objects and method/query syntax with examples. It also discusses synchronous vs asynchronous programming, creating and managing threads, multithreaded applications, and synchronization techniques like locks, monitors, mutexes and semaphores.

Uploaded by

Avinash Ganesan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

Advanced C# Part 4

✓ Unique Operators (Conditional, safe navigation)


✓ LINQ
✓ Synchronous programming
✓ Asynchronous programming
✓ Multithreading
✓ Concurrent collections

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4

Nullable types

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4

Conditional operator (Ternary operator)

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4

Conditional operator exercise (Even or Odd)

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4

Safe navigation operator

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4

Introduction to LINQ

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
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
Method Syntax
LINQ to XML
Query Syntax
LINQ to Entities
LINQ to SQL
Introduction to LINQ

Jack Steven, 5000, 35


Debora Watson, 10000, 45
Claire Adam, 7500, 32
Michael Rob, 3500, 22
Matthew Forest, 4500, 28 foreach (var employee in employees)
Charles Kris, 8000, 27 {
Robert Wilson, 8000, 28 if(employee.Salary>=6000 && employee.Age>=40)
Emma Brooks, 6000, 23         Console.WriteLine(employee.Name);
Jennifer Blake, 3000, 38 }
Terry Loyd, 9000, 49
Adam Spencer, 7000, 33
Stacy Shelton, 4000, 25
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 : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4

LINQ - method syntax exercise

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
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 : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4

LINQ - query syntax exercise

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
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 : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4

LINQ queries with methods

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4

TimeSpan

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4

Introduction to multithreading

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
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 Thread 2 Thread 3 Thread 4

Responding to Offering Solutions for


Checking Syntax Displaying Intellisense
keystrokes 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 : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4

Managing threads

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4

Thread exercise 1

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4

Thread exercise 2

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4

Multithreaded applications

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4

ParameterizedThreadStart

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4

Deadlocks and lock keyword

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
Twitter : ahmadmohey85
Deadlocks and lock keyword

Deadlock occurs when a thread enters a waiting state because a requested system resource is held by another waiting thread,
which in turn is waiting for another resource held by another waiting threat.

X A

Y B

Resource A and resource B are used by process X and process Y


X starts to use A
X and Y try to start using B
Y gets B first
Y needs to use A
A is locked by X, which is waiting for Y
Deadlocks and lock keyword

Lock keyword will ensure that one thread is executing a piece of code at one time. Which means
that one thread does not enter a critical section of code while another thread is in that critical
section.
Advanced C# Part 4

Monitor

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4

Mutex

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
Twitter : ahmadmohey85
Mutex

Mutex (mutual exclusion) object is used to synchronize access to a protected resource.

A mutex's scope is system-wide, while the monitor's scope and lock’s scope
is application-wide.
Advanced C# Part 4

Semaphore

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
Twitter : ahmadmohey85
Semaphore

Limits the number of threads that can access a resource or pool of resources concurrently.
Advanced C# Part 4

Introduction to asynchronous programming

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
Twitter : ahmadmohey85
Introduction to asynchronous programming

Synchronous Programming  Asynchronous Programming 

Single Threaded Multi-Threaded Single Threaded Multi-Threaded


Introduction to asynchronous programming
Synchronous Programming 

Single Threaded Multi-Threaded

Single Threaded : Thread is working on a single task at a time.

Thread 1 Task 1 Task 2 Task 3 Task 4

Multi-Threaded : Multiple threads which are performing different tasks simultaneously .

Thread 1 Task 1

Thread 2 Task 2

Thread 3 Task 3
Introduction to asynchronous programming
Asynchronous Programming 

Single Threaded Multi-Threaded

Single Threaded : Thread starts executing a task it can pause it in middle and start executing another task.

Thread 1 Task 1 Task 2 Task 3 Task 1 Task 2 Task 4

Multi-Threaded : Multiple threads performing different tasks and have the ability to pause in the middle and start
executing another tasks.

Thread 1 Task 1 Task 2 Task 3 Task 1 Task 2 Task 4

Thread 2 Task 5 Task 3 Task 7 Task 5 Task 8 Task 9

Thread 3 Task 7 Task 6 Task 9 Task 4 Task 8 Task 6


Tasks

Synchronous model means two or more tasks are running at the same time and it is possible
that one may block another.

Asynchronous model means two or more operations are running in different contexts (thread)
so that they can run concurrently and do not block each other.
Advanced C# Part 4

Tasks

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
Twitter : ahmadmohey85
Tasks

Task represents an asynchronous operation.


Advanced C# Part 4

Passing parameters to tasks

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4

Tasks with return value

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4

Checking tasks status

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4

Cancelling tasks

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
Twitter : ahmadmohey85
Cancelling tasks

CancellationTokenSource object, which provides a cancellation token through its token property


and sends a cancellation message by calling the Cancel method.

CancellationToken which indicates whether cancellation is requested.


Advanced C# Part 4

Waiting for tasks

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4

Task run

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4

Async and Await

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4

Concurrent collections

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
Twitter : ahmadmohey85
Concurrent collections

Provides several thread-safe collection classes that should be used in place of the corresponding types in the
System.Collections and System.Collections.Generic namespaces whenever multiple threads are accessing the collection
concurrently.

BlockingCollection<T>

ConcurrentBag<T> 

ConcurrentDictionary<TKey,T>

ConcurrentStack<T> 

ConcurrentQueue<T> 
Advanced C# Part 4

Assignments(19, 20, 21, 22)

Ahmad Mohey | Full Stack Developer

E-mail : [email protected]
Twitter : ahmadmohey85
Assignments

Assignment No.19: (Search in a list using LINQ)

• Display count of search result


• Add numbers before each line
• Add a parameter in the display method to display the title or not
Assignments

Assignment No.20: (Say hi in 7 different languages)

• Create a list of threads


• Create a class called Hi contains 7 or so different SayHi methods in different languages
• Each SayHi method has a name and a count to display how many times

• English
• French
• Korean
• Russian
• Greek
• Hindi
• Swedish
Assignments
Assignment No.21: (Fastest task)

• Creating two tasks whatever task finishes first cancels the other one and display the
maximum value the cancelled one reaches.
Assignments
Assignment No.22: (ConcurrentStack)

• Create concurrent stack and fill it with integers using two different threads
• Access it using two different threads
• Count how many items each thread accessed

You might also like