Free Web Development Online Tutorials, Learn to Code
Learn C# programming Language, new c# features in visual studio 2019, C# Development Guide

C# New Features

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

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 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);
    }
    
Comment
Name
Email
Website
Subscribe
 
free C# tutorial online


c# .net core new features
Other Online Tutorial

C# .net Interview Questions Answers

.Net C# Examples | Join .Net C# Course