C# Prodigy: 3 Must-Know Features For Seasoned Developers

TechTrendTracker
4 min readMar 24, 2023

--

C# resembles dating by being both frustrating and rewarding. Throughout my time as a C# developer, I have been surrounded by highly experienced programmers that pushed me to recognize the level of dedication required to succeed in this industry.

We will explore 3 advanced, must-know C# features that will convert you into a programming prodigy and elevate your OOP and C# skills to new heights.

1. LINQ (Language-Integrated Query): In the dumbest of terms, this is a set of tools that facilitate data manipulation of enumerable types (List, Dictionaries, etc.). This tool includes methods that modify variables based on pre-determined set of predicates/requirements:

using System;
using System.Collections.Generic;
using Systems.Linq;

public class LinqMethodExample
{
public static void Main(string[] args)
{
// Create a list of integers.
var listOfNumbers = new List<int> { 10, 5, 20, 15, 30, 25, 33, 66 };

// Perform a variety of LINQ methods.
var result = listOfNumbers
.Where(num => num % 5== 0) // Get numbers divisible by 5.
.Select(num => (int)Math.Pow(num, 2)) // Square each number.
.Skip(1) // Skip the first element.
.Last(num => num % 2 == 0) // Get last even number.

Console.WriteLine($"{result}");

// Sample Output:
// 900
}
}

There are other methods such as:

  • OrderBy() -> sorts sequence in ascending order based on a predicate.
  • GroupBy() -> groups elements based on a predicate.
  • SelectMany() -> flattens an embedded list(i.e., extracts List<int> from List<List<int>>)
  • Join() -> joins two sequences based on a predicate.
  • Zip() -> combines two sequences to a single sequence of tuples.
  • Distinct() -> returns distinct elements from a sequence.

This is a simple example as to the power LINQ methods hold. The scalability and application of these methods is up to you to determine as an engineer. There exists MANY more methods in the LINQ library.

2. Asynchronous Programming: This is simply a technique that allows programs to run multiple tasks of different procedures without blocking the main thread.

  • Instead of waiting for a task to finish, asynchronous tasks allows for other tasks to continue executing simultaneously.
  • This helps improve responsiveness and performance of an application and typically is achieved using async or await keywords.

The following code executes 3 tasks asynchronously. Meaning, if you compile this code, you will see the 3 tasks will run at the same time and be completed at different times!:

using System;
using System.Threading.Tasks;

public class AsyncProgramming
{
public static async Task RandomDelayAsync(int workId)
{
// Set a random delay value for this given task (1s-5s).
var rand = new Random();
var delay = rand.Next(1000, 5000);
await Task.Delay(delay);

// Print message so user knows when this task finished.
Console.WriteLine($"Work {workId} completed in {delay}ms.");
}

public static async Task Main()
{
// Create array of 3 tasks to execute asynchronously.
var tasks = new Task[3];

for (var idx=0; idx < tasks.Length; idx++)
{
tasks[idx] = RandomDelayAsync(idx);
}

// Wait for all tasks to be complete.
await Task.WhenAll(tasks);
Console.WriteLine("All tasks completed.");

// Sample Output:
// Work 2 completed in 1053ms.
// Work 0 completed in 1903ms.
// Work 1 completed in 4661ms.
// All tasks completed.
}
}

3. Delegates and Events: Simply put, these are tools that help provide a way to define a signature of a callback method and provide a way to attach/detach event handlers to an event.

In other words, a delegate is like a phone number you give to someone. An event is like the voicemail that comes after the call. When this event happens, its caused by the phone number call, and the code answers and does something in response, such as triggering a voicemail:

using System;

public class DelegateAndEventExample
{
// Declare a delegate type that represents a method
// that takes no parameters and returns void.
public delegate void PhoneNumber();

public class Class
{
// Declare an event of type PhoneNumber.
public event PhoneNumber VoiceMail;

// Define a method that raises the event.
public void CallPhoneNumber()
{
Console.WriteLine("Calling the phone number...");
MyEvent?.Invoke();
}
}

public static void Main()
{
var instance = new MyClass();

// Attach a method to the voicemail event using a lambda expression.
instance.VoiceMail += () => Console.WriteLine("Event handler called!");

// Call the CallPhoneNumber method, which raises the voicemail event.
instance.CallPhoneNumber();

// Sample Output:
// Calling the phone number...
// Event handler called!
}
}

In conclusion, mastering LINQ, asynchronous programming, and delegates/events are essential for seasoned C# developers who aim to elevate their coding skills. By leveraging these powerful features, developers can write more efficient, concise, and flexible code that can handle complex operations with ease. So, if you’re a C# prodigy or aspiring to become one, make sure to add these features to your toolkit and take your coding game to the next level.

--

--

TechTrendTracker

Big time tech enthusiast. When I'm not coding a storm, I'm here to inform! ;)