Nullability Coding Guideline for Properties and Fields
维护者通常 1 天内回复
@MarkMichaelis 已经在做这个了。
开始于 2020年7月15日。
评估
这个 Issue 还没有评估数据。
描述
When faced with the option of enabling nullable reference types, we have several choices for reference type based properties.
- Given a non-nullable read-write property, should we A) use a full property implementations with a backing field and setter validation to check for null or B) use automatically implemented properties that doesn't prevent null assignment (communicating non-null intent but allowing null in the implementation)? A
- Given a non-nullable property, should we A) require (in the guidelines) the property be assigned before instantiation is complete or B) allow the default null value to remain after instantiation even though the property intent is non-nullable? A
- Do we enable nullable reference type support for a new project, A) enabling the ability to declare intent on the nullability of our API or B) ignore the feature? A
- Do we A) enable nullable reference type support for a brown field project and then turn it off for all files until warnings can be processed or B) ignore the null reference type feature, C) enable it on a per-file basis as nullability support is added to the file (given #nullable enable/disable pre-compiler directives, we could do this at a sub-file level)? Prefer A or C
- For automatically implemented properties, are we comfortable with a guideline that limits code to only 1) nullable automatically implemented properties or 2) read-only non-nullable instantiation initialized properties? Yes
Based on the above choices, here are the recommended coding standards:
-
DO enable nullable on new projects and migrate towards enabling nullable on existing projects.
-
DO implement non-nullable read/write reference properties with a nullable backing field, a null forgiveness operator when returning the field from the getter, and non-null validation in the property setter.
-
DO assign non-nullable reference type properties before instantiation completes.
-
DO use a nullable reference types for all properties and fields that are not initialized before instantiation completes.
-
DO implement non-nullable reference-type automatically implemented as read-only.
-
DO decorate non-nullable automatically implemented reference type properties the with nullability modifier and theNonNullandDisallowNullattributes if the property values are initialized automatically by an external agent.
Example:
[DisallowNull][NotNull]
public string? Name { get; set; }
This should be compatible with the existing coding standards around properties and fields:
- DO favor automatically implemented properties over properties with simple backing fields when no additional logic is required
- DO favor automatically implemented properties over fields
- AVOID accessing the backing field of a property outside the property, even from within the containing class.
- DO create read-only automatically implemented properties in C# 6.0 (or later) rather than read-only properties with a backing field if the property value should not be changed.
Examples:
- Employee (possibly a DTO) has nullable automatically implemented properties or non-nullable properties with backing fields to check for null, or a
class Employee
{
public Employee(string name, string ssn)
{
Name = name;
Ssn = ssn ?? throw new System.ArgumentNullException(nameof(ssn));
}
public string? Title {get;set;}
public string Ssn {get;}
public string Name
{
get => _Name!;
set => _Name = value ?? throw new System.ArgumentNullException(nameof(value));
}
private string? _Name;
}
- The
TestContextproperty is initialized by an outside agent (MSTest) so it is nullable, but marked as not-null so that no checking is required before dereferencing.
[TestClass]
public class ProgramTests
{
[System.Diagnostics.CodeAnalysis.NotNull]
public TestContext? TestContext { get; set; }
[TestMethod]
public void Main_AccessingFields_WriteFieldValues()
{
Assert.IsNotNull(TestContext);
}
}
- 主要语言
- C#
- 星标
- 12
- 派生
- 16
- 平均合并
- 2 分钟
- 30 天内合并 PR
- 10
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
IntelliTect/CodingGuidelines 的其他 Issue
-
难度 3/5 1-2 天 新手友好度 25/100
IntelliTect/CodingGuidelines#290 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 45/100
IntelliTect/CodingGuidelines#253 ·
维护者通常 1 天内回复
-
Review naming recommendation for fields可能重新可做 @MarkMichaelis 于 1323 天前认领,目前没有进行中的 PR。 未关闭.editorconfig C# coding guidelines proposal
IntelliTect/CodingGuidelines#249 · 6 条评论 · 1 个 reaction · 已指派 2 人 ·
维护者通常 1 天内回复
-
analyzer C# coding guidelines proposal
难度 5/5 一周以上 新手友好度 35/100
IntelliTect/CodingGuidelines#231 ·
维护者通常 1 天内回复
-
bug
难度 3/5 1-2 天 新手友好度 35/100
IntelliTect/CodingGuidelines#230 ·
维护者通常 1 天内回复
查看 IntelliTect/CodingGuidelines 的全部 Issue
相似的 Issue
-
copilot documentation
难度 1/5 1-3 小时 新手友好度 88/100
维护者通常 2 天内回复
-
[Rust][Flaky Test] multiple_deadlines_fire_in_order asserts a wall-clock gap instead of firing order未关闭CI/CD ⚒️ Flaky-tests 🐦
难度 2/5 1-3 小时 新手友好度 88/100
valkey-io/valkey-glide#7255 ·
维护者通常 3 天内回复
-
bug good first issue
难度 1/5 1 小时以内 新手友好度 92/100
unoplatform/Uno.Core#99 ·
-
难度 2/5 1-3 小时 新手友好度 84/100
-
难度 2/5 1-3 小时 新手友好度 86/100
aws/aws-dotnet-ai#75 ·