C#中的is关键字原来会做null检查
来源:互联网 发布:淘宝店铺怎么进货 编辑:程序博客网 时间:2024/05/29 15:34
internal class ShouldLimitNameLengthConverter : IValueConverter { const int mIdealNameLengthMax = 20; public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is string) return ((string)value).Length > mIdealNameLengthMax; throw (new NotSupportedException()); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { Debug.Assert(false); throw (new NotSupportedException()); } }
在改一个用户发来的crash report,通过windbg调试dump文件,确认了crash发生在Convert函数,异常是NotSupportedException,那么原因很简单,就是value is string返回了true。这个converter是WPF的DataTemplate调用的,绑定是一个对象的Text属性,是String类型。照理说没问题啊!百思不得其解!终于查了MSDN发现,in关键字是会check null的,只要对象为空就返回false:
An is expression evaluates totrue if the provided expression isnon-null, and the provided object can be cast to the provided type without causing an exception to be thrown.
呵呵,长知识了!
- C#中的is关键字原来会做null检查
- C#中的is,as关键字
- C#中的关键字is和as
- C#的null关键字
- C# null关键字用法
- C# 图片处理 (看完后你会知道 PS原来是这样做的) (转帖)
- Java中的关键字null
- C#的is关键字
- C#中的类型转换-------is,as关键字释疑
- C#中的is和as关键字(摘抄)
- c# 关键字做变量
- C#中的null
- C#中的null
- C# 中的 null
- C#中的null与""
- C#关键字 - is 和 as
- C# is和as关键字
- 讨论B树索引中的 is null/is not null
- Android 文件锁研究 FileLock
- IOS4的多任务机制概述
- JAVA程序员面试32问
- 十个经典排序算法总结版(1)前五个
- PKU 2318 叉积+二分
- C#中的is关键字原来会做null检查
- ARC与非ARC下的Weak-Strong Dance
- jQuery animate()方法详解、实例、扩展(详细版)
- 滑雪
- mini2440启动代码分析
- ABAP对话框
- oledb 连接Excel文件所对应的的多种连接方式
- 开发者必备的6款源码搜索引擎
- tablespace status