0% found this document useful (0 votes)
5 views

7.Advanced Programming Concepts

The document discusses advanced programming concepts in C#, focusing on delegates, events, lambda expressions, and LINQ for data manipulation. It explains how delegates are type-safe function pointers that enable callbacks and event handling, while LINQ simplifies data querying with both query and method syntax. Additionally, it covers asynchronous programming with async/await, file handling, and working with XML and JSON data formats.

Uploaded by

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

7.Advanced Programming Concepts

The document discusses advanced programming concepts in C#, focusing on delegates, events, lambda expressions, and LINQ for data manipulation. It explains how delegates are type-safe function pointers that enable callbacks and event handling, while LINQ simplifies data querying with both query and method syntax. Additionally, it covers asynchronous programming with async/await, file handling, and working with XML and JSON data formats.

Uploaded by

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

Advanced Programming

Concepts
Delegates and Events

 Delegates and events play a pivotal role in building


flexible, maintainable, and reusable code.

 They are fundamental in implementing callbacks,


handling asynchronous tasks, and creating event-driven
programming models.
Delegates

 What is a Delegate?
 A delegate is a variable that holds the a reference to a method
or pointer to a method.
A delegate is a type-safe function pointer in C#. It allows you
to encapsulate a method with a specific signature and return
type, enabling you to pass methods as parameters or store
them in variables. Delegates provide the foundation for events
and callback methods, making your code more modular and
Syntax of a Delegate
flexible.

public delegate int Operation(int a, int b);


Events

 What is an Event?
 An event in C# is a way for a class to provide notifications to
clients of that class when something of interest occurs. Events
are built upon delegates and provide a more structured way to
handle notification patterns.

 Events are commonly used in GUI applications to handle user


actions like button clicks.
Lambda Expressions

 Lambda expressions in C# are used like anonymous functions,


with the difference that in Lambda expressions you don’t need
to specify the type of the value that you input thus making it
more flexible to use.
The ‘=>’ is the lambda operator which is used in all lambda
expressions.
 The Lambda expression is divided into two parts, the left side
is the input and the right is the expression.
The Lambda Expressions can be of two types:
•Expression Lambda: Consists of the input and the
expression.
Syntax:
input => expression;
LINQ (Language Integrated
Query) for data manipulation

 LINQ is a powerful technology that seamlessly integrates


querying capabilities into the .NET framework, providing
developers a way to interact with data from various sources.

 It allows you to approach a querying collections, databases,


XML, and more. By eliminating tedious loops, LINQ allows you
to write clean and readable code for data manipulation tasks.
Why LINQ is important

 Why is LINQ so important? Imagine diving into a sea of data,


trying to find specific information or transform datasets to suit
your needs. LINQ acts as your trusty navigation tool, making the
process simpler, more efficient, and enjoyable.

 With its query syntax and a wide range of built-in operators, LINQ
empowers you to focus on what you want to achieve, rather than
getting lost in implementation details.
 When it comes to querying data using LINQ, you have two
options at your disposal: Query Syntax and Method Syntax.
Both syntaxes provide a way to express your data queries, but
they differ in their approach and syntax style.
Query Syntax

The Query Syntax, also known as the SQL-like


syntax, allows you to write queries that resemble
traditional SQL statements. It employs a set of
keywords, such as from, where, select, group by, and order by,
to construct queries in a familiar manner.

Syntax
var query = from product in products
where product.Price > 50
select product;
Method Syntax.

 On the other hand, the Method Syntax leverages a fluent and


chainable method-based approach. It involves using extension
methods provided by LINQ, such
as Where(), Select(), OrderBy(), and GroupBy(), to construct
queries.
 To better understand these syntaxes, let’s consider an
example. Suppose you have a collection of Product objects and
you want to retrieve all products with a price higher than $50.

Syntax
 var query = products.Where(product => product.Price > 50);
Asynchronous programming
with async and await

 The Task asynchronous programming (TAP)


model provides a layer of abstraction over typical
asynchronous coding. In this model, you write code as a
sequence of statements, the same as usual. The difference is
you can read your task-based code as the compiler processes
each statement and before it starts processing the next
statement.
the async and await keywords make it easier to reason
about code that includes a series of asynchronous
instructions.
The instructions for making a breakfast
might be provided as a list:

1.Pour a cup of coffee.


2.Heat a pan, then fry two eggs.
3.Fry three slices of bacon.
4.Toast two pieces of bread.
5.Spread butter and jam on the
toast.
6.Pour a glass of orange juice.
Reading and writing files

The File class, from the System.IO namespace comes with pretty much
everything we could possibly want, making it very easy to do simple
reading and writing of a file. C# makes it very easy.

You will notice that we use the File class in three places: We use it
to check if our file exists, we use the ReadAllText() method to read
the content of the file, and we use the WriteAllText() method to
write new content to the file. You will notice that I'm not using
absolute paths, but just a simple filename.
Working with XML and JSON data

you may need to work with both XML and JSON data structures at
some point in your projects.

While they serve similar purposes as data interchange formats,


there are some key differences between the two that you should be
aware of when deciding which one to use.

Let’s explore how to work with XML and JSON in C#.


XML

XML (eXtensible Markup Language) is a markup language used for


representing structured data.

It’s a standardized way of defining the structure and organization


of data, making it easy to share and parse data between different
systems. XML is commonly used for configuring applications,
storing data, and transmitting data over networks.

To work with XML in C#, you can use the System.Xml namespace.

Example
XmlDocument doc = new XmlDocument();
JSON

JSON (JavaScript Object Notation) is a lightweight data interchange


format that is easy to read and write.
It’s based on a subset of the JavaScript programming language, but it
can be easily parsed and generated by any programming language
that supports JSON parsing. JSON is commonly used for transmitting
data between web servers and web applications, as well as for storing
data in databases.

To work with JSON in C#, you can use


the System.Web.Script.Serialization namespace.

Example
JsonObject obj = new JsonObject();
END

You might also like