C# Language Document
C# Language Document
E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4
Nullable types
E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4
E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4
E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4
E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4
Introduction to LINQ
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
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
E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4
E-mail : [email protected]
Twitter : ahmadmohey85
LINQ - method syntax exercise
Salary about higher than 4000 and last appraisal less than 8
E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4
E-mail : [email protected]
Twitter : ahmadmohey85
LINQ - query syntax exercise
Salary about higher than 4000 and last appraisal less than 8
E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4
E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4
TimeSpan
E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4
Introduction to multithreading
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
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.
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
E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4
Managing threads
E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4
Thread exercise 1
E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4
Thread exercise 2
E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4
Multithreaded applications
E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4
ParameterizedThreadStart
E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4
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
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
E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4
Mutex
E-mail : [email protected]
Twitter : ahmadmohey85
Mutex
A mutex's scope is system-wide, while the monitor's scope and lock’s scope
is application-wide.
Advanced C# Part 4
Semaphore
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
E-mail : [email protected]
Twitter : ahmadmohey85
Introduction to asynchronous programming
Thread 1 Task 1
Thread 2 Task 2
Thread 3 Task 3
Introduction to asynchronous programming
Asynchronous Programming
Single Threaded : Thread starts executing a task it can pause it in middle and start executing another task.
Multi-Threaded : Multiple threads performing different tasks and have the ability to pause in the middle and start
executing another 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
E-mail : [email protected]
Twitter : ahmadmohey85
Tasks
E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4
E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4
E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4
Cancelling tasks
E-mail : [email protected]
Twitter : ahmadmohey85
Cancelling tasks
E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4
Task run
E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4
E-mail : [email protected]
Twitter : ahmadmohey85
Advanced C# Part 4
Concurrent collections
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
E-mail : [email protected]
Twitter : ahmadmohey85
Assignments
• 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