Questions tagged «c#-8.0»

有关与使用C#语言8.0版进行开发有关的问题。此版本添加了可为空的引用类型,默认接口方法,异步枚举以及其他功能和增强功能



4
C#8带有多个条件的开关表达式具有相同的结果
如何编写switch表达式以支持返回相同结果的多个案例? 对于版本8之前的C#,可以这样编写一个开关: var switchValue = 3; var resultText = string.Empty; switch (switchValue) { case 1: case 2: case 3: resultText = "one to three"; break; case 4: resultText = "four"; break; case 5: resultText = "five"; break; default: resultText = "unkown"; break; } 当我使用带有表达式语法的C#版本8时,就像这样: var switchValue = 3; var resultText …


2
无效是什么!声明是什么意思?
我最近看过以下代码: 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; public Person(string firstName, string lastName, string middleName) { FirstName = firstName; LastName = lastName; MiddleName = …

3
“ {}”是什么意思?
我有时会看到以下代码,却不知道该表达式实际上正在测试什么。 public static void Something(string[] value) { if (value is { }) { DoSomethingElse(); } }
36 c#  c#-8.0 

2
创建空的IAsyncEnumerable
我有一个这样写的接口: public interface IItemRetriever { public IAsyncEnumerable<string> GetItemsAsync(); } 我想编写一个不返回任何项目的空实现,如下所示: public class EmptyItemRetriever : IItemRetriever { public IAsyncEnumerable<string> GetItemsAsync() { // What do I put here if nothing is to be done? } } 如果可以使用普通的IEnumerable,我可以return Enumerable.Empty<string>();,但是没有找到任何东西AsyncEnumerable.Empty<string>()。 解决方法 我发现这可行,但是很奇怪: public async IAsyncEnumerable<string> GetItemsAsync() { await Task.CompletedTask; yield break; } 任何的想法?

1
为什么要声明一个与父作用域中的变量同名的子变量?
我最近写了一些代码,在其中我无意中将变量名重用为函数中声明的操作的参数,该函数已经具有相同名称的变量。例如: var x = 1; Action<int> myAction = (x) => { Console.WriteLine(x); }; 当我发现重复项时,我很惊讶地看到代码可以完美地编译和运行,这并不是基于我对C#范围的了解而期望的行为。一些快速的Google搜索出现SO问题,它们抱怨类似的代码确实会产生错误,例如Lambda Scope Clarification。(我将示例代码粘贴到IDE中,以确保它可以运行;只是为了确保它可以正常运行。)此外,当我在Visual Studio中进入“重命名”对话框时,第一个x突出显示为名称冲突。 为什么此代码有效?我在Visual Studio 2019中使用C#8。


3
对DTO使用可空引用类型的最佳实践
我有一个DTO,它是通过从DynamoDB表中读取来填充的。说当前看起来像这样: public class Item { public string Id { get; set; } // PK so technically cannot be null public string Name { get; set; } // validation to prevent nulls but this doesn't stop database hacks public string Description { get; set; } // can be null } 有什么最佳实践可以解决这个问题?我宁愿避免使用非参数构造函数,因为它与Dynamo …

1
转换IAsyncEnumerable到列表
因此,在C#8中,我们增加了IAsyncEnumerable接口。 如果我们有一个法线,IEnumerable我们可以制作一个List或几乎任何我们想要的其他收藏。感谢那里的Linq。 var range = Enumerable.Range(0, 100); var list = range.ToList(); 好吧,现在我想异步地将其转换IAsyncEnumerable为a List和this。该情况下已经有Linq实现了吗?如果没有,那我该如何转换呢?

1
什么是C#8中的未知可空性?
在C#8.0中,我们可以有可为空的引用类型。文档指出存在4种可为空性。前三个非常清楚,但我无法理解“未知”的含义。文档说它与泛型一起使用,但是当我尝试对泛型中T类型的无约束变量调用方法时,它只会发出警告,就好像该类型可以为空。我看不到unknown和nullable之间的区别。为什么未知存在?它如何表现出来?

4
使用System.Text.Json异步反序列化列表
可以说,我请求一个包含许多对象列表的大json文件。我不希望它们一次全部出现在内存中,但我宁愿一个个地读取和处理它们。所以我需要将异步System.IO.Stream流转换为IAsyncEnumerable<T>。如何使用新的System.Text.JsonAPI来做到这一点? private async IAsyncEnumerable<T> GetList<T>(Uri url, CancellationToken cancellationToken = default) { using (var httpResponse = await httpClient.GetAsync(url, cancellationToken)) { using (var stream = await httpResponse.Content.ReadAsStreamAsync()) { // Probably do something with JsonSerializer.DeserializeAsync here without serializing the entire thing in one go } } }

5
C#8中的不可为空的引用类型在运行时可以为null吗?
在我看来,真的不能保证非空变量永远不会有null。想象一下,我有一个具有不为空的属性的类: public class Foo { public Foo(string test) { Test = test; } public string Test {get;set;} } 现在看起来好像现在不能为空。但是,如果我们用另一个不使用可为空的上下文的库引用该类,则没有什么可以阻止它在其中发送null的。 那是正确的还是有一些运行时检查也可以确保这一点?

3
返回IAsyncEnumerable的方法是否有明确的命名约定?
在C#5引入async和之后await用于异步编程模型之后,C#社区达成了一种命名约定,即向返回等待类型的方法添加“ Async”后缀,如下所示: interface Foo { Task BarAsync(); } 从那时起,许多异步代码分析器(基于Roslyn的和基于非Roslyn的)都被编写为在异步编程周围检测代码气味时依赖于此命名约定。 既然C#8引入了异步枚举的概念,它们本身不是可以等待的,但是可以与结合使用await foreach,似乎有两种方法可以返回命名方法IAsyncEnumerable: interface Foo { // No "Async" suffix, indicating that the return value is not awaitable. IAsyncEnumerable<T> Bar<T>(); } 要么 interface Foo { // With "Async" suffix, indicating that the overall logic is asynchronous. IAsyncEnumerable<T> BarAsync<T>(); } 是否有关于上述选项的明确的命名约定准则(来自C#语言团队,.NET Foundation或其他机构),例如C#5命名约定是如何明确标准化的,而不是由程序员根据意见进行判断的?
10 c#  c#-8.0 

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.