c#-8.0

Default interface methods and default values for Auto Properties

自古美人都是妖i 提交于 2021-01-27 13:00:28
问题 Given that an auto property compiles to a get_method, a set_method and a private variable and since C# 8 is introducing default interface methods Can properties in Interfaces have default implementations? Especially a get only property? 回答1: Yes. Auto properties aren't a special type of property. They are a convenience feature that generates the code needed to store property values in a backing field. You can specify a default implementation for properties, both for getters and setters. You

What is the default value of Non-Nullable reference types in C# 8?

拜拜、爱过 提交于 2020-12-30 07:28:23
问题 If I enable nullable reference types, what will be the value of the following string if I declare it like so? string text; 回答1: The value will be null . Bear in mind that the new system for nullable reference types will only warn you about problems, it will not give you an error , and that means that the code will still compile, with warnings. If you declare this class: public class Test { private string text; } You'll get this warning for your class: CS8618: Non-nullable field 'text' is

In C# 8, why does type inference on new expressions result in nullable references? [duplicate]

雨燕双飞 提交于 2020-12-25 01:31:48
问题 This question already has answers here : Why does Visual Studio Type a Newly Minted Array as Nullable? (3 answers) Closed 6 months ago . If I have the C# 8 code: class Foo {} And later: #nullable enable var bar = new Foo(); Then the type of bar is Foo? . This seems clearly incorrect, as a new expression can't return null . Why would bar be a nullable reference? I even looked up the Nullable Reference Type Specification, and found the following: Expressions that are never null The null state

Iterating an IAsyncEnumerable in a function returning an IAsyncEnumerable with cancellation

◇◆丶佛笑我妖孽 提交于 2020-12-09 05:10:39
问题 As the title says, I have to following function: public async IAsyncEnumerable<Job> GetByPipeline(int pipelineId, [EnumeratorCancellation] CancellationToken cancellationToken = default) { await foreach (var job in context.Jobs.Where(job => job.Pipeline.Id == pipelineId) .AsAsyncEnumerable() .WithCancellation(cancellationToken) .ConfigureAwait(false)) { yield return job; } } I have trouble wrapping my head around where the cancellation token is going, and a nagging feeling that I am using it

Multiple statements in a switch expression: C# 8

橙三吉。 提交于 2020-12-08 06:53:11
问题 Switch expressions were introduced in C# 8. There's plenty of places in codebases, which may be rewritten in this new style. For example, I have some code, which is used for parsing packets from a stream of bytes: switch (command) { case Command.C1: return new P1(); case Command.C2: return new P2(); default: stream.Position++; return null; } The problem is - it can't be converted to a switch expression like return command switch { Command.C1 => new P1(), Command.C3 => new P2(), _ => { stream

How to solve “The issue with T?”/nullable constraint on type parameter?

久未见 提交于 2020-12-05 11:10:50
问题 I'm designing an interface in C# 8.0 with nullable enabled, targeting .Net Standard 2.0 (using the Nullable package) and 2.1. I am now facing The issue with T? . In my example, I am building an interface for a cache which stores Stream s/byte data, identified by a string key, i.e. the file system could by a trivial implementation. Every entry is additionally identified by a version, which should be generic. This version could for example be another string key (like an etag), an int or a date