可空引用类型(认知)与注解(C# 6 模式)
C# 6 没有可空引用类型(string?)。学习安全处理 null:输入保护、清晰的文档、辅助方法 Require.NotNull,并与值类型的 Nullable 作对比。
可空引用类型(认知)与注解(C# 6 模式) 是 CoddyKit 上的免费 C# Academy 课时。 这是第 3 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 C# Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 C# Academy 课程共包含 3 节课。
NRT 注意事项与回退方案
概念:可空引用类型(例如 string?)会在较新版本的 C# 中增加编译器检查。
- C# 6 不支持 string?
- 使用防护检查、清晰的文档和简单的辅助方法
- 默认返回非空值
防范空值
为引用类型参数添加 ArgumentNullException 防护检查,然后将这些值视为非空。
using System;
public static class Greeter
{
// Contract: name must not be null (C# 6 has no string? to enforce)
public static string Hello(string name)
{
if (name == null) throw new ArgumentNullException("name"); // guard
return "Hello " + name.ToUpper(); // safe after guard
}
}
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine(Greeter.Hello("Ada"));
try
{
Console.WriteLine(Greeter.Hello(null)); // will throw (by design)
}
catch (Exception ex)
{
Console.WriteLine("Caught: " + ex.GetType().Name);
}
}
}
Require.NotNull 辅助方法
创建一个小型的 Require.NotNull 辅助方法,以减少样板代码并明确表达意图。
using System;
public static class Require
{
// Works for reference types; returns the validated value
public static T NotNull<T>(T value, string name) where T : class
{
if (value == null) throw new ArgumentNullException(name);
return value;
}
}
public sealed class Email
{
public string Address { get; private set; }
public Email(string address)
{
Address = Require.NotNull(address, "address").Trim();
}
}
public class Program
{
public static void Main(string[] args)
{
Email e = new Email("user@mail.com");
Console.WriteLine(e.Address);
}
}
Nullable<T> 与引用类型
Nullable<T>(例如 int?)用于值类型。引用类型本来就可以为 null;在 C# 6 中,我们使用防护检查。
using System;
public class Program
{
public static void Main(string[] args)
{
int x = 5; // value type (non-nullable)
int? maybe = null; // Nullable<T> works for value types
Console.WriteLine("x=" + x);
Console.WriteLine("maybe has value? " + maybe.HasValue);
Console.WriteLine("maybe or default: " + (maybe ?? -1));
}
}
记录空值契约
在 XML 文档中说明可空性,并保持一致:要么始终非空,要么明确记录何时可能为空。
using System;
/// <summary>
/// Parses "key=value" and returns the value part.
/// Returns null if the format is invalid.
/// </summary>
public static class Parser
{
// Explicitly document when null can be returned.
public static string TryGetValue(string line)
{
if (line == null) throw new ArgumentNullException("line");
int idx = line.IndexOf("=");
if (idx < 0 || idx == line.Length - 1) return null; // documented nullable return
return line.Substring(idx + 1);
}
}
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine(Parser.TryGetValue("a=1"));
Console.WriteLine(Parser.TryGetValue("oops")); // prints nothing (null)
}
}
空值安全检查清单(C# 6)
检查清单:
- 使用 ArgumentNullException 防护输入。
- 默认返回非空值;记录所有可能为空的返回值。
- 使用小型辅助方法(Require.NotNull)让代码保持整洁。
- 在合理的情况下,优先使用可避免空值的重载(例如使用空字符串而不是 null)。
C# 6 中的空值安全
总结
总结:NRT 是较新的功能。在 C# 6 中,请使用防护检查、清晰的文档、辅助方法以及明确的非空返回契约来确保安全。
常见问题解答
「可空引用类型(认知)与注解(C# 6 模式)」课时是免费的吗?
是的 — 「可空引用类型(认知)与注解(C# 6 模式)」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 C# Academy 课程的其余内容,请升级到 CoddyKit PRO。 C# Academy 课程共包含 3 节课。
「可空引用类型(认知)与注解(C# 6 模式)」这节课中我会学到什么?
C# 6 没有可空引用类型(string?)。学习安全处理 null:输入保护、清晰的文档、辅助方法 Require.NotNull,并与值类型的 Nullable 作对比。 你通过在浏览器中直接运行的动手代码来练习 C# Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 C# Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 C# Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 3 节。
「可空引用类型(认知)与注解(C# 6 模式)」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 C# Academy 课中编写并运行代码吗?
能。每节 C# Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 泛型方法/类型与约束
- 接口与委托中的变体(in/out)
- 可空引用类型(认知)与注解(C# 6 模式)