find and solve || findandsolve.com
Please wait.....

Welcome back.






Before Read Terms of use

Programming c# 8.0 build windows, web, and desktop applications

Feature C#8.0 

C# 8.0 it has been in the works for quite some time, event as we build and shipped the minor release C#7.1,7.2 and 7. The current plan is that C# 8.0 will ship at the same time as .NET Core 3.0. However, the features will start to come alive with the previews of Visual Studio 2019 that we are working on.
 
Almost 21 years since Microsoft released the first version of the C# language. It’s going to be released at the same time as .NET Core 3.0. This means that just like .NET Core 3.0 preview, C# 8 is also included in the Visual Studio 2019 versions.
New features in C# 8.0
There are a number of smaller improvements in the works as well. Trickle out over the coming months. 
Nullable reference types
string data type also support nullable reference. Before C# data type is not access null variable for string data type. 
For Example  
string val=null; // Warning: Assignment of null to non-nullable reference type
What can you do if you want to null variable? Then very easy to use a nullable
 reference type such as string? 

string? val=null //This is okay for string value null 
If you try to use nullable reference you need to check it for null first. 
Then compiler analyzes the flow of your code given below.
 
         if (val != null)
            {
                Console.WriteLine(val.Length); // right syntax  
            }
            Console.WriteLine(val.Length); // Warning: it may be null reference exception 
 
  Null-coalescing assignment
      List<int> num = null;
            int? s = null; 
            num ??= new List<int>();
            num.Add(s ??= 15);
            num.Add(s ??= 30);
            Console.WriteLine(string.Join(" ",num));  // output: 15 15
            Console.WriteLine(s);  // output: 15
 
Recursive patterns 
We’re allowing patterns to contain other patterns: 
   IEnumerable<string> GetCustomerList()
         {
           foreach (var c in CustomerList)
            {
              if(c is Customer { IsActive: false,Status:false, Name: string name}) 
 
             yield return name;
 
            }
         }
 Ranges and Indices
Two new types and operators for collections manipulation and indexing. 
Types: System.Range and System.Index. 
Operators : ..and ^. 
For Example:
 
  Index s1 = 4;  // number 4 from beginning
  Index s2 = ^ 5; // number 5 from end
  int[] number = { 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12};
  Console.WriteLine($"{number[s1]}, {number[s2]}"); // "5, 7"
 
Static local functions 
We can add the static modifier to local functions to ensure that local function doesn't 
capture any variables from the engrossing scope.
 
Asynchronous streams
C# 8.0 create and consume streams asynchronously method that returns
an asynchronous stream has three properties given below:
It's declared with the async modifier.
It returns an IAsyncEnumerable<T>.
The method contains yield return statements to return successive elements in the asynchronous stream.
 
Using the await foreach statement: 
   await foreach (var n in NumberList())
            {
                Console.WriteLine(n);
            }
 
Stackalloc in nested expressions 
Span<int> num = stackalloc[] { 10, 20, 30, 40, 50, 60 };
var isd = num.IndexOfAny(stackalloc[] { 20, 40, 60, 80 });
Console.WriteLine(isd); 
// output: 10 

Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.

Comments



Report Response