7.Advanced Programming Concepts
7.Advanced Programming Concepts
Concepts
Delegates and Events
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.
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.
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
Syntax
var query = from product in products
where product.Price > 50
select product;
Method Syntax.
Syntax
var query = products.Where(product => product.Price > 50);
Asynchronous programming
with async and await
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.
To work with XML in C#, you can use the System.Xml namespace.
Example
XmlDocument doc = new XmlDocument();
JSON
Example
JsonObject obj = new JsonObject();
END