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 上面调用的时候报错了 ...
随机推荐
- html5——拖拽
基本情况 在HTML5的规范中,我们可以通过为元素增加draggable="true"来设置此元素是否可以进行拖拽操作,其中图片.链接默认是开启的. 拖拽元素 页面中设置了drag ...
- 检索COM类工厂中CLSID 为 {000209FF-0000-0000-C000-000000000046}的组件时失败, 原因是出现以下错误: 80070005
主要问题原因是Word权限配置问题 解决方案: 控制面板-管理工具-组件服务-计算机-我的电脑-DCOM配置 在列表中找到microsoft word97-2003 document 右键选择属性,选 ...
- 一款批量linux管理工具batchshell
BatchShell是什么? BatchShell是一款基于SSH2的批量文件传输及命令执行工具,它可以同时传输文件到多台远程服务器以及同时对多台远程服务器执行命令.BatchShell基于原生的sh ...
- Linux 配置JDK + MyEclipse
版本:Ubuntu16.04: jdK: Java SE Development Kit 8u102; My Eclipse: 10.6; JDK配置的细致步骤参见此处. 就一点要注意: 请使用代码进 ...
- (转)Struts2的标签库
http://blog.csdn.net/yerenyuan_pku/article/details/68638679 Struts2的标签库 对于一个MVC框架而言,重点是实现两部分:业务逻辑控制器 ...
- Dynamics 365 CRM Connected Field Service 自动发送command
上期降到了怎样部署connected field service(CFS) 我们假设现在IoT 设备是温度监控器, 当温度触发我们之前预设的温度值, IoT会通过IoT Hub 发送IoT Alert ...
- 使用GetMirror一次镜像多个实体
public static void GetMirror(this ObjectIdCollection ids, Point3d p1, Point3d p2, bool s, params Ent ...
- CAD调用移动命令
主要用到函数说明: _DMxDrawX::SendStringToExecuteFun 把命令当着函数执行,可以传参数,详细说明如下: 参数 说明 IDispatch* pParam 命令参数,IMx ...
- ubuntu14.0开机guest账号禁用方法
在终端里进入/usr/share/lightdm/lightdm.conf.d/目录 sudo vim 50-unity-greeter.conf 然后在文件里输入: [SeatDefaults] a ...
- 2018年为什么要学习Linux?Linux运维的前景还好吗?
Linux一直是很多人入行IT的首选,无论是从入行难度还是职业寿命来说,Linux运维都比开发有着更大的优势.为了得到高薪工作,很多人在Linux学习过程中付出了非常大的努力,最终也得到了不错的收获. ...