freeCodeCamp/guide/chinese/csharp/nameof-expressions/index.md

35 lines
1.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
title: nameof Expressions
localeTitle: 表达名称
---
# 表达名称
有时您需要变量类型或成员的字符串名称例如引发异常记录或触发属性更改事件。在C6.0之前,您可以将字符串文字用于此类目的。
#
```
public void ProcessStudent(Student student)
{
if (student == null) throw new ArgumentNullException("student");
}
```
但是如果要重命名student参数则必须记住也要修改字符串文字。现在使用nameof表达式您不必使用字符串文字如果您使用的名称不正确编译器将能够发出警告。
#
```
public void ProcessStudent(Student student)
{
if (student == null) throw new ArgumentNullException(nameof(student));
}
```
nameof表达式可能有用的一些示例包括
* 在参数验证期间抛出异常
* 在设置MVC操作链接时传递操作名称
* 在实现INotifyPropertyChanged的类中触发属性更改事件时需要传递属性的名称
* 在注册XAML依赖项属性时传递属性的名称
* 记录时包括变量,类型或成员名称
应该注意的是如果您使用限定名称提供nameof编译器将为最右侧的名称生成一个字符串。