cost 287 ms
如何指示方法返回值的属性不是 null 因为它是在方法内部检查的? - How to indicate that a property of a return value from a method is not null because it was checked inside the method?

如何向 C# 中的 static null 流分析表明方法返回的属性 object 不是 null 因为它已被验证? 请参阅下面的代码。 让我们有这个验证方法public static class AuthenticationHeader { public static bool TryP ...

如何避免构造期间填充的私有字段“可能为空”的编译器错误 - How to avoid compiler errors that private fields populated during construction "may be null"

一个非常简单的示例,但通常您在构造期间设置了私有 class 字段或属性,并希望将其提取到辅助方法中: 因为_list是在单独的方法中而不是在构造函数中填充的,所以编译器会发出警告“CS8618 - 非空变量在退出构造函数时必须包含非空值。考虑将其声明为可空。” 这是正确的,但作为开发人员,我知道该 ...

如何在 function 调用返回后告诉 C# 可空类型检查器变量不能是 null? - How to tell C# nullable type checker that a variable cannot be null after a function call returns?

简洁版本如何让 C# 可空类型检查编译器在调用返回后意识到变量customer不能是 null: .NET 小提琴: https://do.netfiddle.net/ZcgRCV 长版如果我有这样的代码: 当我尝试访问 .FirstName 时,编译器会(正确地)警告我customer可能是. ...

你能制定一个全局规则,让“虚拟”属性自动为空吗? - Can you make a global rule that `virtual` properties are automatically nullable?

通过virtual关键字如何与 EPiServer CMS 的EntityFramework实现交互,其中空属性将被读取为null ,有没有办法在项目级别定义所有virtual属性都可以为空? 在任何情况下,这些属性本质上都是可空的,我们需要在 EPiServer 项目中处理这些属性时直接检查它们 ...

如何知道类型是否为“字符串”? 或“字符串”? - How to know if the type is "string?" or "string"?

我需要做一个检查器来确保所有参数都得到了很好的实现。 但是我无法区分字符串变量是否被不必要地使用为空(?)。 对于所有其他类型,我可以区分。 结果: 串=假; 细绳? =假的; 结果: 串=假; 细绳? =假的; 结果: 串=假; 细绳? =假的; 有没有人对我如何区分有任何建议? 谢谢 ...

在 NRT 感知代码中,当任务可能无法运行时如何使用 WhenAll? - How to use WhenAll when tasks may not be run, in NRT aware code?

几年前有个问题How to WhenAll when some tasks can be null? 它有一个相当简单的顶级答案: 但是在 modern.Net 6 中,我们有 NRT,这会导致编译器警告,因为您正在放置Task? 在Task数组中。 如果您只是将其更新为: 您仍然会收到警告,因为 ...

为什么发出关于可空类型的警告,即使没有空值? - Why is a Warning about a nullable type issued, even though there can be no null value?

为什么我在 list2 的行中收到警告? 我在这里过滤掉了所有的空值。 警告指出,在 select 方法中可能会取消引用空值。 这是我在 list2 行中收到的警告: 在 list3 的行中没有发出警告,但是 Select-Statement 中的检查总是没有意义的。 ...

DataAnnotations Validator Class 不复制对不可空字符串的隐式必需检查 - DataAnnotations Validator Class Not Replicating Implicit Required Check for Non-Nullable String

当使用DataAnnotations为传入的 controller 请求验证模型时, 不可为 null 的引用类型将被隐式视为必需类型,除非您将它们声明为可为 null (即string?而不是string )。 此行为会导致通过 Postman / 等访问端点时The PropertyName f ...

使引用类型可为空有什么意义? - What is the point of making a reference type nullable?

可空引用类型的文档说: 编译器使用这些注释来帮助您查找代码中潜在的 null 引用错误。 不可为 null 的引用类型和可为 null 的引用类型之间没有运行时差异。 编译器不会为不可为 null 的引用类型添加任何运行时检查。 好处在于编译时分析。 编译器会生成警告,帮助您查找并修复代码中潜在的 ...

通用 Activator.CreateInstance(typeof(T), msg) 标记为可为空 - Generic Activator.CreateInstance(typeof(T), msg) marked as nullable

我有这种通用方法,可以根据上下文抛出不同T的不同异常: Activator.CreateInstance()返回Nullable<T>而不是T ,因此我在var exception上收到可能的 null 警告。 我想不出var exception可能最终成为 null 的情况。编译器似 ...

可空字符串(字符串?)和初始化为可原谅的字符串有什么区别 null(字符串 s = null!) - What is the difference between a nullable string (string?) and a string initiated to a forgiven null (string s = null!)

可空字符串(字符串?)和初始化为可原谅的字符串有什么区别 null(字符串 s = null!) 在任何情况下您都希望使用其中一种吗? 它们在功能上是等价的吗? ...

为什么我需要分配默认值? 到类中的成员属性? - Why do I need to assign default! to a member property in a class?

我正在通过 class 为 ASP.NET Core 6 进行培训,在 class 声明中,它具有: 如果我理解default! 正确地(我对 C# 3 有经验,所以这对我来说是新的)它在创建 object 时为该属性分配默认值。 由于此属性是 class,因此它正在分配 null。而! 告诉编译 ...

Pattern Matching is not { } foo of nullable type keep showing it as nullable type - Pattern Matching is not { } foo of nullable type keep showing it as nullable type

我对“is”运算符有点困惑,在写完这个之后使用 VS2022 和 Resharper: 正如 Resharper 所建议的那样,将is not int id更改为is not { } id并且悬停id将导致int ,因此不再有可为空的int? 在这种情况下: loc将是一个不可为 null 的L ...

错误 null 参考警告 - False null reference warning

我正在按照 int.TryParse 的行在 C# 中编写一些这样的函数: 我的问题是我找不到解决此问题的优雅方法,您可以在每次调用 TryGetSomeObject 时编写额外的不必要的 null 检查或在 function 中创建一个无意义的 SomeObject 空实例(并非总是可能)以在发生 ...

CS8625 无法将 null 文字转换为 API 调用的不可空引用类型警告,该调用可能预期 null - CS8625 Cannot convert null literal to non-nullable reference type warning for API call that may expect null

我正在清除我的 C# 代码中的所有 null 参考警告,并在调用 Win32 API 方法时出现警告。 根据方法文档,我可以传入一个空字符串,因此可以使用string.Empty解决警告。 但是,对于一般情况,我们如何才能消除此警告并允许空值 - 其他真正期望 null 不只是空字符串的 API ...


 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM