Cool Features in C#
C# has continually evolved to enhance developer productivity and code readability, introducing modern features that streamline object creation, improve string handling, and simplify collection manipulations. These advancements make coding in C# cleaner, faster, and more intuitive, allowing developers to write elegant and efficient code. Whether you’re an experienced programmer or new to C#, exploring these innovative features with practical examples and outputs will elevate your coding experience.
1. Verbatim String Literal (@””)
A verbatim string literal starts with @ and allows you to include escape sequences like \ or line breaks without escaping them. Useful for file paths and multi-line strings.
Example:
string path = @"C:\Users\Documents\file.txt";
string multiLine = @"This is a
multi-line string.";
Console.WriteLine(path);
Console.WriteLine(multiLine);
Output:
C:\Users\Documents\file.txt
This is a
multi-line string.
2. Raw String Literal (“””)
Introduced in C# 11, raw string literals allow for multi-line strings without escape characters or special formatting requirements. These are enclosed in triple double-quotes (“””).
Example:
string rawString = """
This is a raw string literal.
It allows you to include "quotes", \slashes\, and
spans multiple lines without escaping anything.
""";
Console.WriteLine(rawString);
Output:
This is a raw string literal.
It allows you to include “quotes”, \slashes\, and
spans multiple lines without escaping anything.
3. Target-typed new Expressions
Introduced in C# 9, this allows you to create objects without specifying the type on the right-hand side if it can be inferred from context.
Example:
Person person = new("John Doe", 30); // Type inferred from the variable
Console.WriteLine(person.Name);
Console.WriteLine(person.Age);
class Person
{
public string Name { get; }
public int Age { get; }
public Person(string name, int age)
{
Name = name;
Age = age;
}
}
Output:
John Doe
30
4. Collection Expressions
A collection expression is a concise syntax for creating collection values, represented by a sequence of elements enclosed in [ ] brackets. It can be evaluated and assigned to various collection types, simplifying the creation of common collections.
Example:
// Create an array:
int[] a = [1, 2, 3, 4, 5, 6, 7, 8];
// Create a list:
List<string> b = ["one", "two", "three"];
// Create a span
Span<char> c = ['a', 'b', 'c', 'd', 'e', 'f', 'h', 'i'];
// Create a jagged 2D array:
int[][] twoD = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
// Create a jagged 2D array from variables:
int[] row0 = [1, 2, 3];
int[] row1 = [4, 5, 6];
int[] row2 = [7, 8, 9];
int[][] twoDFromVariables = [row0, row1, row2]; 5. Spread element (..)
A spread element (..) allows you to inline values from one collection into another within a collection expression. For example, you can create a full alphabet collection by combining vowels, consonants, and the letter “y,” demonstrating its versatility in merging collections.
Example:
string[] vowels = ["a", "e", "i", "o", "u"];
string[] consonants = ["b", "c", "d", "f", "g", "h", "j", "k", "l", "m",
"n", "p", "q", "r", "s", "t", "v", "w", "x", "z"];
string[] alphabet = [.. vowels, .. consonants, "y"]; 5. Range (..) and From-the-End (^) Operators
Introduced in C# 8, the range operator (..) simplifies slicing arrays or collections. The from-the-end operator (^) counts indices from the end.
Example:
int[] numbers = { 10, 20, 30, 40, 50 };
var range = numbers[1..4]; // Takes elements from index 1 to 3 (exclusive)
var lastTwo = numbers[^2..]; // Takes the last 2 elements
Console.WriteLine(string.Join(", ", range));
Console.WriteLine(string.Join(", ", lastTwo));
Output:
20, 30, 40
40, 50


