C# New Features and latest version

As a C# .net developer you may be interested to know what are the C# new features in c# latest version!

Here learn some of cool latest features in c# programming, features like out variable, tuples, pattern matching , async main, default literal, discard, generalised async, lazy list etc.

new c# features in visual studio 2019

You can experiment different version new features of C# by changing the C# development version in your visual studio.

Setting up C# version

If you want to change the version of C#, make the following changes in your Visual Studio project.

  1. Right-click on the project -> Properties
  2. Go to "Build" tab
  3. Scroll down and click on "Advanced"
  4. Set Language version to 7.1 or latest version you want to use
  5. Click on Ok, Save and close the tab

C# new features

C# new features in .Net

As a .Net C# developer you must know some of the new features in C# .Net

  1. out variable

    We can now declare out values inline as arguments, earlier it used to be like we need to declare the variable before using as a out parameter.

    if (int.TryParse(input, out var result))
        Console.WriteLine(result);
    else
        Console.WriteLine("Error, Could not parse input");
    
  2. Tuples in C# 7

    Now we can declare a variable with more than one data type

    (string Alpha, string Beta) namedLetters = ("a", "b");
    Console.WriteLine($"{namedLetters.Alpha}, {namedLetters.Beta}");
    

    We can also write function which will return composite result

    List<int> numbers = new List<int> { 1,5,9,15,43,61 };
    var result = GetTopBottom(numbers);
    private static (int, int) GetTopBottom(IReadOnlyCollection<int> numbers)
    {
    	int lowest = numbers.Min(n => n);
    	int highest = numbers.Max(n => n);
    	return (lowest, highest);
    }
    
  3. Pattern matching

    C# 7.0 offers the features for pattern matching We can check pattern using the is Operator with Pattern Matching

    class PatternMatchingExample
    {
        object[] data = { new Student("Arijit"), new Student("Kaykasha"),null, 39 };
        void Test()
        {
            foreach (var item in data)
            {
                if (item is Student)
                {
                    Student s = item as Student;
                    Console.WriteLine(s.FullName);
                }
                else
                {
                    Console.WriteLine("not a Student pattern");
                    throw new ArgumentNullException(nameof(item));
                }
            }
        }     
    }
    
  4. async Main in C# 7.1

    Main is usually an entry point in a C# console application, but now we can have async Main method in C# 7.1

    class Program
    {
    static void Main(string[] args)
    {
    Console.WriteLine("Hello World!");
    MainAsync(args).GetAwaiter().GetResult();
    Console.ReadLine();
    }
    static async Task MainAsync(string[] args)
    {
    Console.WriteLine("Do Stuff");
    await SomeAsyncMethod();
    }
    public static async Task SomeAsyncMethod()
    {
    await Task.Run(() =>
    {
    for (int i = 0; i < 10; i++)
                    {
                        Console.WriteLine("calling: " + DateTime.Now.Millisecond);
                    }
                });
            }
        }
    
  5. default literal expressions in C# 7.1

    We can use the default expression like example below.

    static void DoSomething(string text = default)
    {
        int myNumber = default;
        Console.WriteLine(myNumber);
        Console.WriteLine($"text:{text}");
    }
    
  6. Generalized async return types
  7. Discards in C# 7

    The new feature discards in C# enables a way to ignore some local variables.

    The discards are basically the way to ignore local variables which are irrelevant for the purposes of the implementation. here is a realtime example of Discards in C#

    var (_, _, roolNo, fullName, mobile)= GetStudentDetails(10);
    private static (string, DateTime, int, string, string) GetStudentDetails(int roolNumber)
        {
            string address=null;
            string fullName="arijit";
            string mobile= "980000011";
            DateTime DOB=DateTime.Now.AddYears(-18);
            int roolNo=10;
            return (address, DOB, roolNo, fullName, mobile);
    }
    

After c# 7.0 the next version was released as C# 8.0, not much new features but more of enhancement like pattern matching, asynchronous streams etc. C# 8.0 is supported for .NET Core 3.0 onwards framework and .NET Standard 2.1.

Latest features C# 9.0 brings many new features and enhancements, the new introduction of Record types, now like class we can create "record", like public record Car(string carType);, Learn more about C# 9.0 new features with real-time implementation example.

Microsoft also has released new version C# 10, C# 10 is supported on .NET 6

deconstruction with Assignment and declaration

(int x, int y) = point;

// assignment:
int x1 = 0;
int y1 = 0;
(x1, y1) = point;
 
.net core c# new features
.Net C# Programming Tutorials
Online Tutorials
C# .net Interview Questions Answers
.Net C# Examples | Join .Net C# Course