Possible Duplicate:
Casting vs using the 'as' keyword in the CLR
C#: “is” vs “as”
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 converstion
fails. 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.