pattern matching is C# 7.0
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/is
原来的版本
private static string DateTimeFormat(DateTime date) { string result = string.Empty; if (Logger == null) { return result; } var appenders = GetAppenders(); var appender = appenders.FirstOrDefault(x => x is RollingFileAppender) as RollingFileAppender; if (appender != null) { result = date.ToString(appender.DatePattern, DateTimeFormatInfo.InvariantInfo); } else { result = date.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo); } return result; }
优化后的版本
private static string DateTimeFormat(DateTime date) { string result = string.Empty; if (Logger == null) { return result; } var appenders = GetAppenders(); if (appenders.FirstOrDefault(x => x is RollingFileAppender) is RollingFileAppender appender) { result = date.ToString(appender.DatePattern, DateTimeFormatInfo.InvariantInfo); } else { result = date.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo); } return result; }
涉及到的新知识点
Type pattern
When using the type pattern to perform pattern matching, is
tests whether an expression can be converted to a specified type and, if it can be, casts it to a variable of that type. It is a straightforward extension of the is
statement that enables concise type evaluation and conversion. The general form of the is
type pattern is:
expr is type varname
where expr is an expression that evaluates to an instance of some type, type is the name of the type to which the result of expr is to be converted, and varname is the object to which the result of expr is converted if the is
test is true
.
The is
expression is true
if any of the following is true:
-
expr is an instance of the same type as type.
-
expr is an instance of a type that derives from type. In other words, the result of expr can be upcast to an instance of type.
-
expr has a compile-time type that is a base class of type, and expr has a runtime type that is type or is derived from type. The compile-time type of a variable is the variable's type as defined in its declaration. The runtime type of a variable is the type of the instance that is assigned to that variable.
-
expr is an instance of a type that implements the type interface.
If exp is true
and is
is used with an if
statement, varname is assigned and has local scope within the if
statement only.
相关文章
- C# 代码重启windows服务
- 编写高质量代码改善C#程序的157个建议[正确操作字符串、使用默认转型方法、却别对待强制转换与as和is]
- c# 获取相对路径
- ActiveMQ C#消息队列系列一(安装)
- C# 中的值类型和引用类型
- C# Json反序列化 数据协定类型 无法反序列化 因为未找到必需的数据成员
- C#中的AS、IS运算符与强制类型转换
- C# 枚举操作工具类
- C# 如何识别一个字符串是否Json格式
- Atitit. c# 语法新特性 c#2.0 3.0 4.0 4.5 5.0 6.0 attilax总结 1. 版本历史1 1.1. C# 1.0-纯粹的面向对象2 1.2. C# 2.0
- Atitit. 数据约束 校验 原理理论与 架构设计 理念模式java php c#.net js javascript mysql oracle
- Atitit.得到网络邻居列表java php c#.net python
- (四十七)c#Winform自定义控件-树表格(treeGrid)-HZHControls
- c# winform快捷键设置
- C#+无unsafe的非托管大数组(large unmanaged array in c# without 'unsafe' keyword)
- C#开发的OpenRA的游戏主界面怎么样创建8
- C#获取桌面快捷方式路径并读取其目标路径
- [C#]c#中数据的同步加锁机制 的几种方法
- 【UiPath2022+C#】UiPath数据类型