c-sharp
  1. c-sharp-advanced-c

Advanced C#

C# is a versatile programming language that supports many advanced features. In this tutorial, we'll introduce some of these advanced features, including generics, delegates, lambda expressions, and more.

Generics

Generics in C# allows you to write classes, methods, and interfaces that work with any type of data. You can use generics to create reusable data structures, algorithms, and utility classes.

Here's an example of using generics to create a List class:

public class List<T> {
   private T[] items;

   public void Add(T item) {
      // add item to the list
   }
   // other methods
}

Now, you can create a list of any type using the List class:

List<int> intList = new List<int>();
List<string> stringList = new List<string>();

Delegates

Delegates in C# allow you to create references to methods, which you can then execute at a later time. You can use delegates to create event handlers, Callback functions, and more.

Here's an example of using delegates to create an event handler:

public delegate void ButtonClickEventHandler(object sender, EventArgs e);
public class Button {
   public event ButtonClickEventHandler Click;

   public void OnClick() {
      if (Click != null) {
         Click(this, new EventArgs());
      }
   }
}

Now, you can create an instance of the Button class and attach a method to its Click event:

Button button = new Button();
button.Click += new ButtonClickEventHandler(Button_Click);

private void Button_Click(object sender, EventArgs e) {
   // handle button click event
}

Lambda Expressions

Lambda expressions in C# allow you to create anonymous functions, which you can use to simplify your code and make it more readable. You can use lambda expressions to create delegates, LINQ queries, and more.

Here's an example of using lambda expressions to create a delegate:

public delegate int Calculate(int x, int y);

Calculate calc = (x, y) => x + y;
int result = calc(5, 10); // result = 15

LINQ

LINQ (Language Integrated Query) in C# allows you to query objects, databases, and XML using a common syntax. LINQ provides a standard way of querying different data sources, making it possible to use the same language constructs for all of them.

Here's an example of using LINQ to query a list of numbers:

List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
var query = from num in numbers
            where num % 2 == 0
            select num;

foreach (var num in query) {
   // handle even numbers
}

Asynchronous Programming

Asynchronous programming in C# allows you to write code that runs asynchronously, without blocking the main thread of the application. This improves the responsiveness of the application and allows you to handle multiple requests concurrently.

Here's an example of using async and await to perform a long-running operation on a separate thread:

private async void Button_Click(object sender, EventArgs e) {
   var result = await LongRunningOperationAsync();
   // handle the result
}

private async Task<int> LongRunningOperationAsync() {
   await Task.Delay(5000); // simulate a long-running operation
   return 42;
}

Summary

In this tutorial, we introduced some of the advanced features of C#, including generics, delegates, lambda expressions, LINQ, and asynchronous programming. With these features, you can write more robust and efficient code that leverages the full power of the C# language.

Published on: