本文介绍了它的代码是更好的:使用" AS"或QUOT;是"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
This code:
if (sheet.Models.Data is GroupDataModel)
{
GroupDataModel gdm = (GroupDataModel)sheet.Models.Data;
and this code:
GroupDataModel gdm = sheet.Models.Data as GroupDataModel;
if (gdm != null)
{
Do you recommend any of the two styles above over the other one?
解决方案
I think its better to go for as because it convert as well as you can do check easily ..second one is good
More about this: Explicit and Implicit Casting of object and Role of 'is' and 'as' keyword in Explicit casting
Difference between as and is
as operator do conversion form one type to another type and return Null if converstionfails. There is no need to conversion again if its convertible.
is operator checks weather one object is convertible in another type or not and return false if not. So need to convert object to base type if it convertible.
这篇关于它的代码是更好的:使用" AS"或QUOT;是"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!