Delegates in C# are type-safe function pointers
that are used to encapsulate a method with a specific
signature and return type. They are the foundation of event handling and callbacks in C#
Delegates are often used in conjunction with events
. An event in C# is a wrapper around a delegate that
provides additional features for managing event subscribers.
using System;public delegate void ProcessCompletedEventHandler();public class Publisher{public event ProcessCompletedEventHandler ProcessCompleted;public void StartProcess(){Console.WriteLine("Process Started...");// Simulate some work with a delaySystem.Threading.Thread.Sleep(2000);// Raise the eventOnProcessCompleted();}protected virtual void OnProcessCompleted(){if (ProcessCompleted != null){ProcessCompleted();}}}public class Subscriber{public void OnProcessCompleted(){Console.WriteLine("Process Completed! Subscriber notified.");}}class Program{static void Main(string[] args){Publisher publisher = new Publisher();Subscriber subscriber = new Subscriber();// Subscribe to the eventpublisher.ProcessCompleted += subscriber.OnProcessCompleted;// Start the processpublisher.StartProcess();// Unsubscribe from the event (optional)publisher.ProcessCompleted -= subscriber.OnProcessCompleted;Console.ReadLine();}}
Events in C# provide a way for a class to notify other classes or objects
when something of interest occurs.
They are an essential part of the event-driven programming model and are built on top of delegates.
public class Publisher{// Declare the event using the delegatepublic event ProcessCompletedEventHandler ProcessCompleted;public void StartProcess(){Console.WriteLine("Process Started...");// Simulate some work with a delaySystem.Threading.Thread.Sleep(2000);// Raise the eventOnProcessCompleted();}protected virtual void OnProcessCompleted(){// Check if there are any subscribersProcessCompleted?.Invoke(this, EventArgs.Empty);}}public class Subscriber{// Event handler methodpublic void OnProcessCompleted(object sender, EventArgs e){Console.WriteLine("Process Completed! Subscriber notified.");}}using System;class Program{static void Main(string[] args){Publisher publisher = new Publisher();Subscriber subscriber = new Subscriber();// Subscribe to the eventpublisher.ProcessCompleted += subscriber.OnProcessCompleted;// Start the processpublisher.StartProcess();// Unsubscribe from the event (optional)publisher.ProcessCompleted -= subscriber.OnProcessCompleted;Console.ReadLine();}}
LINQ (Language Integrated Query) is a set of features in C# that allows you to query and manipulate data in a more readable
and concise way. LINQ enables you to write queries directly in C# (or other .NET languages) using a
syntax similar to SQL
. It can be used to query various data sources, such as collections of objects
(in-memory data), XML, databases
, and more.
using System;using System.Collections.Generic;using System.Linq;public class Student{public int Id { get; set; }public string Name { get; set; }public int Age { get; set; }}class Program{static void Main(){List<Student> students = new List<Student>{new Student { Id = 1, Name = "Alice", Age = 23 },new Student { Id = 2, Name = "Bob", Age = 21 },new Student { Id = 3, Name = "Charlie", Age = 22 },new Student { Id = 4, Name = "David", Age = 21 }};// Query syntaxvar youngStudentsQuery = from student in studentswhere student.Age < 23select student;Console.WriteLine("Young students (query syntax):");foreach (var student in youngStudentsQuery){Console.WriteLine(student.Name);}// Method syntaxvar youngStudentsMethod = students.Where(student => student.Age < 23);Console.WriteLine("Young students (method syntax):");foreach (var student in youngStudentsMethod){Console.WriteLine(student.Name);}}}
Generics in C# allow you to define classes, methods, interfaces, and delegates with a placeholder
for the type of data
they store or use. This provides type safety without the need to specify the actual data type when defining the class,
method, interface, or delegate. Instead, you specify the data type when you create an instance of the generic type or
call a generic method. Generics improve code reusability and performance by enabling you to write a method or class
that can work with any data type.
public class GenericList<T>{private T[] _items;private int _count;public GenericList(int capacity){_items = new T[capacity];_count = 0;}public void Add(T item){if (_count < _items.Length){_items[_count++] = item;}}public T Get(int index){if (index >= 0 && index < _count){return _items[index];}throw new IndexOutOfRangeException();}}GenericList<int> intList = new GenericList<int>(10);intList.Add(1);intList.Add(2);Console.WriteLine(intList.Get(0)); // Outputs: 1GenericList<string> stringList = new GenericList<string>(10);stringList.Add("Hello");stringList.Add("World");Console.WriteLine(stringList.Get(0)); // Outputs: Hello
In C#, IEnumerable and IQueryable are two interfaces commonly used for data querying
and manipulation,
particularly in the context of LINQ (Language Integrated Query). Understanding the differences between
them is crucial for effective data handling and performance optimization in various scenarios
.
using System;using System.Collections.Generic;using System.Linq;class Program{static void Main(){List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };// IEnumerable queryIEnumerable<int> query = numbers.Where(n => n > 2);// Execution happens hereforeach (var num in query){Console.WriteLine(num); // Outputs: 3, 4, 5}}}
using System;using System.Linq;using System.Data.Entity;public class Product{public int ProductId { get; set; }public string Name { get; set; }public decimal Price { get; set; }}public class MyDbContext : DbContext{public DbSet<Product> Products { get; set; }}class Program{static void Main(){using (var context = new MyDbContext()){// IQueryable queryIQueryable<Product> query = context.Products.Where(p => p.Price > 100);// Execution happens here (on the database)foreach (var product in query){Console.WriteLine(product.Name); // Outputs products with price > 100}}}}
Quick Links
Legal Stuff
Social Media