Questions tagged [c#-8.0]

For issues related to development with version 8.0 of C# language. This version adds nullable reference types, default interface methods, async enumerables and other features and enhancements. Use this tag when asking about features specific to this version of the C# language. You should also include the [c#] tag.

Version 8 of the programming language was released in September 2019.

New features include:

  • Readonly members
  • Default interface methods
  • Nullable reference types
  • Using declarations
  • Static local functions

Resources:

  1. What's new in C# 8.0
  2. 3 New C# 8 Features We Are Excited About
  3. github: 8.0 candidate milestone
547 questions
247
votes
3 answers

Does C# 8 support the .NET Framework?

In Visual Studio 2019 Advanced Build settings, C# 8 does not appear to be available for a .NET Framework project, only (as in the picture below) for a .NET Core 3.0 project: Does C# 8 support the .NET Framework?
James Harcourt
  • 6,017
  • 4
  • 22
  • 42
240
votes
6 answers

The annotation for nullable reference types should only be used in code within a '#nullable' context

I have a console app to try out the C# 8 null reference types. Switched the project to build with lang ver C# 8. Then the following code results in a warning. class Program { static void Main(string[] args) { string?…
Mihail Shishkov
  • 14,129
  • 7
  • 48
  • 59
223
votes
6 answers

C# 8 switch expression with multiple cases with same result

How can a switch expression be written to support multiple cases returning the same result? With C# prior to version 8, a switch may be written like so: var switchValue = 3; var resultText = string.Empty; switch (switchValue) { case 1: case…
huzle
  • 2,249
  • 2
  • 9
  • 8
216
votes
6 answers

What does null! statement mean?

I've recently seen the following code: public class Person { //line 1 public string FirstName { get; } //line 2 public string LastName { get; } = null!; //assign null is possible public string? MiddleName { get; } = null; …
isxaker
  • 8,446
  • 12
  • 60
  • 87
189
votes
2 answers

What is the difference between using and await using? And how can I decide which one to use?

I've noticed that in some case, Visual Studio recommends to do this await using var disposable = new Disposable(); // Do something Instead of this using var disposable = new Disposable(); // Do something What is the difference between using and…
Justin Lessard
  • 10,804
  • 5
  • 49
  • 61
184
votes
1 answer

Why doesn't the new hat-operator index from the C# 8 array-slicing feature start at 0?

C# 8.0 introduces a convenient way to slice arrays - see official C# 8.0 blogpost. The syntax to access the last element of an array is var value = new[] { 10, 11, 12, 13 }; int a = value[^1]; // 13 int b = value[^2]; // 12 I'm wondering why the…
Michael Pittino
  • 1,556
  • 4
  • 12
  • 18
141
votes
3 answers

What does exclamation mark mean before invoking a method in C# 8.0?

I have found a code written in C# seemingly version 8.0. In the code, there is an exclamation mark before invoking a method. What does this part of the code mean, and above all, what are its uses? var foo = Entity!.DoSomething();
icn
  • 1,783
  • 2
  • 7
  • 13
141
votes
6 answers

How to enable Nullable Reference Types feature of C# 8.0 for the whole project

According to the C# 8 announcement video the "nullable reference types" feature can be enabled for the whole project. But how to enable it for the project? I did not find any new appropriate option in the Project Properties window in Visual Studio…
137
votes
2 answers

C# 8 understanding await using syntax

I have next method: public async Task> GetQuotesAsync() { using var connection = new SqlConnection(_connectionString); var allQuotes = await connection.QueryAsync(@"SELECT [Symbol], [Bid], [Ask], [Digits] FROM…
Uriil
  • 11,948
  • 11
  • 47
  • 68
109
votes
2 answers

Convert IAsyncEnumerable to List

So in C#8 we got the addition of the IAsyncEnumerable interface. If we have a normal IEnumerable we can make a List or pretty much any other collection we want out of it. Thanks to Linq there. var range = Enumerable.Range(0, 100); var list =…
Twenty
  • 5,234
  • 4
  • 32
  • 67
85
votes
2 answers

When does IDE0063 dispose?

I'm trying to understand this C# 8 simplification feature: IDE0063 'using' statement can be simplified For example, I have: void Method() { using (var client = new Client()) { // pre code... client.Do(); // post…
Bizhan
  • 16,157
  • 9
  • 63
  • 101
62
votes
4 answers

How to treat ALL C# 8 nullable reference warnings as errors?

Using Visual Studio 2019 v16.3.2 with a .NET Core 3.0 project set to C# 8 and nullable reference types enabled. netcoreapp3.0 8.0
Jeff Walker Code Ranger
  • 4,634
  • 1
  • 43
  • 62
62
votes
4 answers

Nullable reference types with generic return type

I'm playing around a bit with the new C# 8 nullable reference types feature, and while refactoring my code I came upon this (simplified) method: public T Get(string key) { var wrapper = cacheService.Get(key); return wrapper.HasValue ?…
Razzie
  • 30,834
  • 11
  • 63
  • 78
56
votes
2 answers

What does "is { }" mean?

I see the following code sometimes, and have no idea what the expression is actually testing. public static void Something(string[] value) { if (value is { }) { DoSomethingElse(); } }
evan
  • 1,463
  • 1
  • 10
  • 13
56
votes
2 answers

Nullable reference types: How to specify "T?" type without constraining to class or struct

I want to create a generic class that has a member of type T. T may be a class, a nullable class, a struct, or a nullable struct. So basically anything. This is a simplified example that shows my problem: #nullable enable class Box { public…
Andent
  • 601
  • 5
  • 5
1
2 3
36 37