当前位置:网站首页>C 11 more practical NAMEOF
C 11 more practical NAMEOF
2022-06-13 09:44:00 【Dotnet cross platform】
C# 11 More practical nameof
Intro
from C# 6.0 Start , We can start using nameof
To reference the parameter name , Where possible nameof
The place of , It is generally recommended to use for automatic update during refactoring , Prior to C# In the version , If you want to add a method parameter or a method attribute Pass through nameof
To reference a method will compile an error , from C# 11 Start , Expanded nameof
Scope of action , We can use it , Here are a few examples
Samples
Method parameter reference
stay .NET 6 A new caller information is introduced in Attribute CallerArgumentExpression
, Using it, we can simplify many logs or metrics Record and so on , Automatically get the information of the call , But there is a fly in the ointment , We need to use strings to refer to method parameters , As shown below :
public static ICollection<T> NotEmpty<T>([NotNull] ICollection<T> collection, [CallerArgumentExpression("collection")] string? paramName = null)
{
if (collection.Count == 0)
{
throw new ArgumentException("The collection could not be empty", paramName);
}
return collection;
}
Examples like this , We quote collection
Parameters can only use strings , This is for us collection
When refactoring parameter names , Will still need to be manually modified CallerArgumentExpression
Parameter name in , In the new version , We will be able to use directly nameof(collection)
Instead of a string , as follows :
public static ICollection<T> NotEmpty<T>([NotNull] ICollection<T> collection, [CallerArgumentExpression(nameof(collection))] string? paramName = null)
{
NotNull(collection, paramName);
if (collection.Count == 0)
{
throw new ArgumentException("The collection could not be empty", paramName);
}
return collection;
}
In this way, it is not easy to make a manual error and input the parameter name incorrectly , and nameof
Can provide a better refactoring experience
Attribute reference
In addition to the method parameters referenced in the method parameters , We can also use the Attribute Reference method parameters in , Examples are as follows :
[Description(nameof(nameExpression))]
public bool M(string name, [CallerArgumentExpression(nameof(name))]string nameExpression = null)
{
return string.IsNullOrEmpty(name);
}
Thanks to the C# Constant interpolation string property in , We can also be in Attribute Using constant interpolation strings in , as follows :
[Description($"{nameof(nameExpression)} IsNullOrEmpty")]
public bool M(string name, [CallerArgumentExpression(nameof(name))]string nameExpression = null)
{
return string.IsNullOrEmpty(name);
}
More
At present, the support of this feature has been completed and merged into the main branch , Expect the next version of SDK We can use it after publishing ~~
References
https://github.com/dotnet/csharplang/blob/main/proposals/extended-nameof-scope.md
https://sharplab.io/#v2:EYLgtghglgdgNAExAagD4AEBMBGAsAKHQAYACdbAOgGEB7MABxpgFMYAXAWRoWYBsBuAsTKUASgFd2UMM2p16UXswBOAZRUA3KAGNmAZ0H4hAZjKYSVEgG8CJOyQDaAEX3blUemyhMAFABIAIisYCBkaADMfEJkAUQAPemV9PW8YAEoAXxIAST0AOXFeXgB5ZRiGNgBPALSAXVt7dFNgGhpeEg4fclJo5jhHKggilQBBZQBzcRl2eMTk1KjQ5gjFmTS67pJe2aS9FKYSAF4twt40kga7G3x7W7IAdhEiClyCotLyz0rV5jTDW4yBAyQA
边栏推荐
- C# 11 新特性:接口中的静态抽象成员
- Trees and binary trees: the concept of binary trees
- [most comprehensive and detailed explanation] detailed explanation of knapsack problem
- Execution order of subclass and parent constructor
- A hot MySQL training topic, making you a master of SQL
- VGA常用分辨率及计算方法
- Summary of string, vector and array learning
- [51nod 3062] n queen problem V2 [bit operation DFS]
- Acwing 787. Merge sort
- acwing 788. Number of pairs in reverse order
猜你喜欢
Acwing 787. Merge sort
LeetCode 1. Sum of two numbers
Jenkins access openldap user authentication
MySQL中redo日志和undo日志简述
The turtle library displays the system time
[51nod p2673] shortest path [heap optimization Dijk]
(bfs) acwing 847. Hierarchy of points in the graph
Protocol UART of schematic diagram
Pxxx local socket communication
1-2 24:00 (20 points) [CSP certification true question]
随机推荐
Exercise 7-10 finding specified characters (15 points)
Node-RED系列(二四):在Node-RED中使用mysql节点实现数据库的增删改查
Instruction level parallelism (?)
go-zero微服务实战系列(三、API定义和表结构设计)
Tree and binary tree: storage structure of binary tree
LeetCode 5289. Fair distribution of cookies (DFS)
ThingsBoard教程(二一):使用消息类型和数据处理节点对数据处理后保存
A hot MySQL training topic, making you a master of SQL
Protocol UART of schematic diagram
Analysis and implementation of variable parameters in C language
Jenkins integrates LDAP. The problem of login failure of Jenkins users caused by LDAP configuration error is solved
SpEL表达式 简单使用
Amadahl's Law (a little thought)
[51nod p2106] an odd number of times [bit operation]
一篇文章读懂:Spark运行模式
Classes and objects -- object model and this pointer
[51nod p2527] or and sum [bit operation]
架构师必备:系统容量现状checklist
VDD,DVDD,AVDD,VCC,AFVDD,DOVDD,IOVDD
微信小程序客服自动回复——PHP实现