C# Language Features & Tips
A collection of C# language features and tips samples.
C# release history
Version Release | Date | Tool |
---|---|---|
1.0 | 2002 | Visual Studio |
2.0 | 2005 | Visual Studio 2005 |
3.0 | 2008 | Visual Studio 2008 |
4.0 | 2010 | Visual Studio 2010 |
5.0 | 2012 | Visual Studio 2012 |
6.0 | 2015 | Visual Studio 2015 |
C# 6 Language Features
- Auto-property Enhancements
- Expression Bodied Methods and Auto-Properties
- The nameof Expressions
- Null-conditional Operators
- String Interpolation
- Index Initializers
- Using Static
- Exception Filters
- Await in Catch and Finally Blocks
- Default Constructors in Structs
Abondoned features
Primary Contructors
public class Person(string firstName, string lastName)
{
public string FirstName { get; set; } = firstName;
public string LastName { get; set; } = lastName;
}
DeclarationExpressions
// Read number and print the power 2 of the number
if (int.TryParse(var line = Console.ReadLine(), out int i))
{
Console.WriteLine("Result: {0}", (var x = i) * x, x);
}
// Convert list of strings to list of integers
var strings = new List<string> { "123", "-5", "1337", "3777" };
var numbers = from s in strings select int.TryParse(s, out int i) ? i : -1;
Further readings
https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6
Some interesting point of view: http://sietch.net/ViewNewsItem.aspx?NewsItemID=250
C# 7 Features
- Tuples
- Pattern matching
- Record types
- Non-nullable reference types
- Local Functions
- Immutable Types
Full list of features as described in msdn: https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/
C# / .Net General discussion suggestions
Threads
- Lock vs Mutex vs Semaphore: When to use & Performance;
- Perhaps also look at System.Threading.Timer;
Performance of abstractions on loops: https://www.infoq.com/articles/For-Each-Performance
Contribute
Check out the contribution guidelines if you want to contribute to this project.