The Top 20 C# Features Every .NET Developer Should Know! Part 1/2

C# is a modern, object-oriented programming language developed by Microsoft. It was first introduced in 2002 as part of the .NET Framework. C# is a simple, powerful, and type-safe programming language used to build desktop, web, game, and mobile applications. It supports both static and dynamic typing and is extremely versatile and continuously evolving. Therefore, a .NET/C# developer should always stay up-to-date with the latest changes.

Here is a list of the first 10 best C# features you should know:

Introduced in C# 2.0. Enables the creation of type-safe data structures in C#. Provides generic collection classes in the System.Collections.Generic namespace.

For example:

"'

List numbers = new List ();

numbers.Add(1); // Strictly for integers

"'

Introduced in C# 2.0. Allows splitting a class definition across multiple files using the keyword `partial`.

For example: 

"'

// In File1.cs

public partial class MyClass { void Method1() {} }

// In File2.cs

public partial class MyClass { void Method2() {} }

"'

Introduced in C# 3.0. Enables queries from various data sources such as C# collections, SQL, and XML using a common query syntax.

For example:

"'

// Query C# Collection

var numbers = new List { 1, 2, 3, 4, 5 };

var evenNumbers = numbers.Where(n => n% 2 == 0);

// Query SQL DB

var data = _db.Products.Select(p=>p.IsActive=true);

"'

Introduced in C# 3.0. Provides a concise way to write inline expressions or anonymous methods.

For example: 

"'

List numbers = new List { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

// Lambda expression to define the criteria for filtering

IEnumerable evenNumbers = numbers.Where(n => n& 2 == 0);

"'

Introduced in C# 3.0. Helps add methods to an existing class without modifying it.

For example: 

"'

public static class StringExtension

{

public static string ToPascalCase(this string str)

{

// Implementation

}

}

string title = “hello world”;

Console.WriteLine(title.ToPascalCase());

"'

Introduced in C# 4.0. Enables writing dynamic code in C#. Delays type checking from compile time to runtime.

Introduced in C# 5.0. Helps with writing asynchronous code, which is essential for non-blocking UI and server applications.

For example: 

"'

public async Task GetDataAsync(string url)

{

using (var client = new HttpClient())

{

return await client.GetStringAsync(url);

}

}

"'

Introduced in C# 6.0. Allows the embedding of expressions and variables directly into string literals.

For example:

"'

string name = “Mohan”;

int age = 20;

//String interpolation using $

string message = $”Hello, my name is {name} and I am {age} years old.”;

"'

Introduced in C# 6.0. Allows the definition of concise, single-line methods, properties, and other elements using a lambda-like syntax.

For example: 

"'

// Expression-bodied method

public int Add(int x, int y) => x + y;

"'

Introduced in C# 6.0. Allows the initialization of the value of an automatically implemented property directly in the property declaration.

For example:

"'

// Auto-property initializer

public int Website { get; set; } = “www.scholarhat.com”;

"'

Introduced in C# 2.0. Enables the creation of type-safe data structures in C#. Provides generic collection classes in the System.Collections.Generic namespace.

For example:

"'

List numbers = new List ();

numbers.Add(1); // Strictly for integers

"'

Introduced in C# 2.0. Allows splitting a class definition across multiple files using the keyword `partial`.

For example: 

"'

// In File1.cs

public partial class MyClass { void Method1() {} }

// In File2.cs

public partial class MyClass { void Method2() {} }

"'

Introduced in C# 3.0. Enables queries from various data sources such as C# collections, SQL, and XML using a common query syntax.

For example:

"'

// Query C# Collection

var numbers = new List { 1, 2, 3, 4, 5 };

var evenNumbers = numbers.Where(n => n% 2 == 0);

// Query SQL DB

var data = _db.Products.Select(p=>p.IsActive=true);

"'

Introduced in C# 3.0. Provides a concise way to write inline expressions or anonymous methods.

For example: 

"'

List numbers = new List { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

// Lambda expression to define the criteria for filtering

IEnumerable evenNumbers = numbers.Where(n => n& 2 == 0);

"'

Introduced in C# 3.0. Helps add methods to an existing class without modifying it.

For example: 

"'

public static class StringExtension

{

public static string ToPascalCase(this string str)

{

// Implementation

}

}

string title = “hello world”;

Console.WriteLine(title.ToPascalCase());

"'

Introduced in C# 4.0. Enables writing dynamic code in C#. Delays type checking from compile time to runtime.

Introduced in C# 5.0. Helps with writing asynchronous code, which is essential for non-blocking UI and server applications.

For example: 

"'

public async Task GetDataAsync(string url)

{

using (var client = new HttpClient())

{

return await client.GetStringAsync(url);

}

}

"'

Introduced in C# 6.0. Allows the embedding of expressions and variables directly into string literals.

For example:

"'

string name = “Mohan”;

int age = 20;

//String interpolation using $

string message = $”Hello, my name is {name} and I am {age} years old.”;

"'

Introduced in C# 6.0. Allows the definition of concise, single-line methods, properties, and other elements using a lambda-like syntax.

For example: 

"'

// Expression-bodied method

public int Add(int x, int y) => x + y;

"'

Introduced in C# 6.0. Allows the initialization of the value of an automatically implemented property directly in the property declaration.

For example:

"'

// Auto-property initializer

public int Website { get; set; } = “www.scholarhat.com”;

"'

…and 10 more features. This list provides an overview of the most important functions that are relevant for both novice and experienced .NET/C# developers. Understanding these features and using them in your own programming is essential for developing more efficient and robust applications.