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.
pattern matching is C# 7.0的更多相关文章
- C#9.0 终于来了,带你一起解读Pattern matching 和 nint 两大新特性玩法
一:背景 1. 讲故事 上一篇跟大家聊到了Target-typed new 和 Lambda discard parameters,看博客园和公号里的阅读量都达到了新高,甚是欣慰,不管大家对新特性是多 ...
- Beginning Scala study note(5) Pattern Matching
The basic functional cornerstones of Scala: immutable data types, passing of functions as parameters ...
- scala pattern matching
scala语言的一大重要特性之一就是模式匹配.在我看来,这个怎么看都很像java语言中的switch语句,但是,这个仅仅只是像(因为有case关键字),他们毕竟是不同的东西,switch在java中, ...
- Zhu-Takaoka Two-dimensional Pattern Matching
Two dimensional pattern matching. Details may be added later.... Corresponding more work can be foun ...
- [Scala] Pattern Matching(模式匹配)
Scala中的match, 比起以往使用的switch-case有著更強大的功能, 1. 傳統方法 def toYesOrNo(choice: Int): String = choice match ...
- Symbols of String Pattern Matching
Symbols of String Pattern Matching in Introduction to Algorithms. As it's important to be clear when ...
- KMP string pattern matching
The function used here is from the leetcode. Details can be found in leetcode problem: Implement str ...
- [PureScript] Break up Expressions into Cases in PureScript using Simple Pattern Matching
Pattern matching in functional programming languages is a way to break up expressions into individua ...
- openssl 生成证书上 grpc 报 legacy Common Name field, use SANs or temporarily enable Common Name matching with GODEBUG=x509ignoreCN=0
最近用传统的方式 生成的证书上用golang 1.15. 版本 报 grpc 上面 ➜ ~ go version go version go1.15.3 darwin/amd64 上面调用的时候报错了 ...
随机推荐
- React容器组件和展示组件
Presentational and Container Components 展示组件 - 只关心它们的样子. - 可能同时包含子级容器组件和展示组件,一般含DOM标签和自定的样式. ...
- 联想 A5(L18011) 免解锁BL 免rec Magisk Xposed ROOT 救砖 ZUI 3.9.068
>>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...
- 04--Spring知识汇总
1. @Autowried注解 Spring 2.5 引入了 @Autowired 注释,它可以对类成员变量.方法及构造函数进行标注,完成自动装配的工作. 通过 @Autowired的使用来消除 se ...
- 【SQLite】select into 语句
sqlite不支持类似sqlserver中的select into 语法 在SQL Server中,我们要将一个表中的数据复制到一个新表中,可以这样写: SELECT * INTO newtable ...
- Tomcat8 连接池
1.所有的tomcat项目共用一个连接池配置 1.1 修改conf->context.xml文件,在Context节点下配置 <Resource name="jdbc/myDat ...
- crontab错误处理
crontab任务跑着跑着突然停了,莫名奇妙,查看日志,发现以下错误: 网上搜了一下报错,提示说是调整打开最大进程数,设置如下
- redis中关于使用string类型还是hash类型
前篇:最近在做一个将redis中大数据量进行合并缩减优化的工作,其中一项按月将数据进行合并.将一个月的数据放入一个key-value键值对中. 例:p2d20180901-3.p2d20180902- ...
- mybatis批量操作(foreach)
foreach可以在SQL语句中通过拼接的方式进行集合迭代.foreach元素的属性主要有collection,item,index,separator,open,close. item属性:表示循环 ...
- js-2018-11-01 关于break和continue语句
1.label语句 语法:label: statement 加标签语句一般都要与for语句等循环语句配合使用. 2.break语句 立即退出循环,强制执行循环后面的语句. 3.continue语句 立 ...
- python 未知
import timeimport requestsfrom bs4 import BeautifulSoupimport threading def format_str(s): return s. ...