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 上面调用的时候报错了 ...
随机推荐
- CSS——display:flex
Flex是Flexible Box的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性. 设为Flex布局以后,子元素的float.clear和vertical-align属性 ...
- web移动端适配
/*** html节点字体大小随屏幕大小改变 用于rem布局***/首先这是一个立即执行函数(function (doc, win) { var docEl = doc.documentElement ...
- ipc (进程间通信
进程间通信(IPC,Inter-Process Communication),指至少两个进程或线程间传送数据或信号的一些技术或方法.进程是计算机系统分配资源的最小单位(严格说来是线程).每个进程都有自 ...
- C# 获取表中最大值
; if (db.LPicture.Any()) { // LPicture Newmode = db.LPicture.Where(n => ).FirstOrDefault(); start ...
- antiSMASH数据库:微生物次生代谢物合成基因组簇查询和预测
2017年4月28日,核酸研究(Nucleic Acids Research)杂志上,在线公布了一个可搜索微生物次生代谢物合成基因组簇的综合性数据库antiSMASH数据库 4.0版,前3版年均引用2 ...
- PHP 之ftp客户端类封装实现
<?php /** * Class FtpClient */ class FtpClient { private $host = '';//远程服务器地址 private $user = ''; ...
- CAD在网页中如何设置实体闪烁?
主要用到函数说明: MxDrawXCustomFunction::Mx_TwinkeEnt 闪烁实体.详细说明如下: 参数 说明 McDbObjectId id 被闪烁的实体对象id LONG lCo ...
- kernel-常见参数或宏
kernel-常见参数或宏 get_online_cpus get_online_cpus(); get_online_mems(); kstrdup_const 分配内存 cache_name = ...
- JavaScript day3(转义符)
转义符(escape character) 当你定义一个字符串必须要用单引号或双引号来包裹它.那么当你需要在字符串中使用一个: " 或者 ' 时该怎么办呢? 在 JavaScript 中可 ...
- (C/C++学习)11.随机数组的快速查找
说明:利用随机函数生成一个随机数组,然后对数组进行排列,再利用二分查找快速查找一个数. 一.生成随机数组 time_t ts; //等价于long ts; unsigned int num = tim ...