What's New in C# 8.0

What's New in C# 8.0

What's New in C# 8.0: In this article, I'll walk you through some of the new features introduced in the 8th version of C# and show some use cases.

After 18 years of existence, C# is still as young and “sharp” as ever. Indeed, by open-sourcing its compiler and design, Microsoft strengthened the position) of C# in the market by making it available to all developpers on GitHub.

Contributing and voting to set the language roadmap are the main contributors to its success in the last few years.

Last month, the C# language team in Microsoft announced the release of the preview version of VS 2019 and .NET Core 3.0.

The exciting things about it is that this version includes the new C# 8 that we can play with to test some of the new features.

In this article, I’ll walk you through some of the new features introduced in the 8th version of C# and show some use cases.

Ranges and Indices

C# 8 introduces two new types and operators for collections manipulation and indexing. Basically we will have a more interesting and elegant way to index and slide collections.

New types: System.Index and System.Range.

New Operators : .. and ^.

Let’s see some examples:

Index d1 = 2;  // number 2 from beginning
Index d2 = ^3; // number 3 from end

string[] week = { "Monday",
"Sunday" };

Console.WriteLine($"{week[d1]}, {week[d2]}"); // "Wednesday , Friday "

The element selection is 0-based if you are counting from the beginning and 1-based if you are counting from the end.

  • Get the last element:
//Old Style
var lastDay = week[week.Count -1]; // Sanday
//New Style
var lastDay = week[^1]; // Sanday
  • Get a range of elements:
//Old Style
var days = week.ToList().GetRange(2,3); // Wednesday,Thursday,Friday
var days = week.Skip(2).Take(3); // Wednesday,Thursday,Friday
//New Style

var days = week[1..^2]; // Wednesday,Thursday,Friday

// [1..^2] mean
// 1 : skip elements from the begining until the index 1
// ^2 : skip 2 element from the end


This new feature will be available in any framework implementing .NET Standard 2.1. For example, it will be available in .NET Core 3.0 but not in the .NET framework 4.8. If you want more informations about .NET Standard you can check this article.

Default Implementations of Interface Members

For this part, we will imagine that we have a three year old application with a bunch of classes and interfaces, and that 30 of these classes them implement an Interface called IPrintable.

This interface includes a signature for each printer operation.

Let’s say you want to add a new method that does exactly the same thing as Print, but which has a different signature. In this case, there is no way around reimplementing the new method for all 30 classes.

With C# 8, you can define a default implementation for the new interface member.

Let’s check how this works.

interface IPrintable
void Print(string header, string text);

// New overloadvoid Print(Document doc) => Print(doc.Header, doc.Text); 


class Printer : IPrintable
public void Print(string header, string text) { ... }
// Print(Document) gets default implementation

Switch Expressions

We can say that this is a light or short version of a switch statement with a new syntax using the => operator.

var volume = shape switch
Cube c => Math.Pow(length,3),
Prisme p => p.Width * p.Height * p.length,
Cylinder cy => Math.PI * Mathf.Pow(cy.Radius,2) * cy.heigth,
_ => throw new UnknownShapeException(shape)
New Target-Typed Expressions

To build a collection with typed objects we are obliged to specify the type for each element or use the new statement for the object construction.

This is an example:

Month[] months = {new Month("January",31),new Month("April",30)};

With the new target-typed expression there’s no need to specify the object type or class name we can do like this:

Month[] months = {new("January",31),new("April",30),new("June",31)};
Async Streams

C# 5 made it possible to have some asynchronous features like awaiting a result with await and async. In C# 8, we get an evolution of this feature adding the IAsyncEnumerable which allows us to yield results asynchronously.

This feature can be very useful if you are developing an IoT app and you are dealing with a lot of asynchronous calls returning data.

Here is an example:

 static async IAsyncEnumerable<string> GetElementsAsync()
await Task.Delay(2000);
yield return new Element();

// we can use the await with a foreach too

await foreach (var name in GetNamesAsync())
// do some stuff

We also need to add async to our Main method to make it work

static async Task Main(string[] args)
Nullable Reference Types

As we all know there are two* types in C# : reference type and value type. struct is a typical example of a value type while class is a reference type. If you want more information about those two check this link.

In this section, we will talk about the famous, the one, the only null pointer exception and how to prevent it from happening.

It’s too simple — you need just to activate the null reference types feature in your project and you will get a warning if you are doing something wrong.

// add this line to your .csproj


For example, if you are doing something like:

string name = null;

Console.WriteLine($"My name is {name}");

You will get a first warning saying “Assigning null to a non-nullable type.”

We will fix it by changing string to **string **and checking if the name is null with ??.

string? name = null;

Console.WriteLine($"My name is {name ?? '?'}");

You can get his type of warning if you are Resharper too :p

  • C# includes 3 types not 2 . I discovered the third one this week it’s called “Pointer Type”. I’ve never used this type but it looks like the C/C++ pointer.

You can download Visual Studio 2019 here.

You can download the .NET core 3.0 preview here.

What’s New in C# 8.0

To follow the evolution of C# you can follow those GitHub repositories :



Originally published by ahd Ben kheder at https://dzone.com

C/C++ vs. Rust: A developer’s perspective

C/C++ vs. Rust: A developer’s perspective

In this post, you'll see the difference between Rust and C/C++ in a developer’s perspective

C++ is an incredibly fast and efficient programming language. Its versatility knows no bounds and its maturity ensures support and reliability are second to none. Code developed in C++ is also extremely portable, all major operating systems support it. Many developers begin their coding journey with the language, and this is no coincidence. Being object-oriented means it does a very good job of teaching concepts like classes, inheritance, abstraction, encapsulation and polymorphism. Its concepts and syntax can be found in modern languages like C#, Java and Rust. It provides a great foundation that serves as a high speed on ramp to the more popular, easier to use and modern alternatives.

Now it’s not all rosy. C++ has a very steep learning curve and requires developers to apply best practices to the letter or risk ending up with unsafe and/or poor performing code. The small footprint of the standard library, while most times considered a benefit, also adds to the level of difficulty. This means successfully using C++ to create useful complex libraries and applications can be challenging. There is also very little offered in terms of memory management, developers must do this themselves. Novice programmers could end up with debugging nightmares as their lack of experience leads to memory corruption and other sticky situations. This last point has lead many companies to explore fast performing, safe and equally powerful alternatives to C++. For today’s Microsoft that means Rust.

The majority of vulnerabilities fixed and with a CVE [Common Vulnerabilities and Exposures] assigned are caused by developers inadvertently inserting memory corruption bugs into their C and C++ code - Gavin Thomas, Microsoft Security Response Center
Rust began as a personal project by a Mozilla employee named Graydon Hoare sometime in 2006. This ambitious project was in pre-release development for almost a decade, finally launching version 1.0 in May 2015. In what seems to be the blink of an eye it has stolen the hearts of hordes of developers going as far as being voted the most loved language four years straight since 2016 in the Stack Overflow Developer Survey.

The hard work has definitely paid off. The end result is very efficient language which is characteristically object oriented. The fact that it was designed to be syntactically similar to C++ makes it very easy to approach. But unlike the aforementioned it was also designed to be memory safe while also employing a form of memory management without the explicit use of garbage collection.

The ugly truth is software development is very much a trial and error endeavor. With that said Rust has gone above and beyond to help us debug our code. The compiler produces extremely intuitive and user friendly error messages along with great direct linking to relevant documentation to aid with troubleshooting. This means if the problem is not evident, most times the answer is a click away. I’ve found myself rarely having to fire up my browser to look for solutions outside of what the Rust compiler offers in terms of explanation and documentation.

Rust does not have a garbage collector but most times still allocates and release memory for you. It’s also designed to be memory safe, unlike C++ which very easily lets you get into trouble with dangling pointers and data races. In contrast Rust employs concepts which help you prevent and avoid such issues.

There are many other factors which have steered me away from C++ and onto Rust. But to be honest it has nothing to do with all the great stuff we’ve just explored. I came to Rust on a journey that began with WebAssembly. What started with me looking for a more efficient alternative to JavaScript for the web turned into figuring out just how powerful Rust turns out to be. From its seamless interop…

Automatically generate binding code between Rust, WebAssembly, and JavaScript APIs. Take advantage of libraries like web-sys that provide pre-packaged bindings for the entire web platform. – Rust website
To how fast and predictable its performance is. Everything in our lives evolves. Our smartphones, our cars, our home appliances, our own bodies. C++ while still incredibly powerful, fast and versatile can only take us so far. There is no harm in exploring alternatives, especially one as exceptional and with as much promise as Rust.

What do you guys think? Have you or would you give Rust a try? Let us know your thoughts in the comments section below.

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

Further reading

Why you should move from Node.js to Rust in 2019

Rust Vs. Haskell: Which Language is Best for API Design?

7 reasons why you should learn Rust programming language in 2019

An introduction to Web Development with Rust for Node.js Developers

Why do C and C++ allow the expression (int) + 4*5?

Why is this&nbsp;<em>(adding a type with a value)</em>&nbsp;possible? (tried with g++ and gcc.)

(int) + 4*5;

Why is this (adding a type with a value) possible? (tried with g++ and gcc.)

I know that it doesn't make sense (and has no effect), but I want to know why this is possible.