Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In C#.NET language: This style of coding is recommended or the one below it?

        if (sheet.Models.Data is GroupDataModel)
        {
            GroupDataModel gdm = (GroupDataModel)sheet.Models.Data;
            Group group = gdm.GetGroup(sheet.ActiveCell.Row.Index);   
            if (group!=null && controller != null)
            {
                controller.CheckApplicationState();
            }
        }

or this one:

        var gdm = sheet.Models.Data as GroupDataModel;
        if (gdm != null)
        {
            Group group = gdm.GetGroup(sheet.ActiveCell.Row.Index);   
            if (@group!=null && controller != null)
            {
                controller.CheckApplicationState();
            }
        }
share|improve this question
    
Relevant: stackoverflow.com/q/132445/102937 –  Robert Harvey Oct 30 '12 at 19:56
add comment

3 Answers

up vote 7 down vote accepted

If you intend to cast (or use as) if the result of calling is is true, you should just call as and avoid two cast attempts.

I prefer the second version for this reason.

share|improve this answer
    
hmm what is that "@" that is being added in second version to the group variable? it is now @group –  BDotA Oct 30 '12 at 20:35
    
@BDotA - Looks like a typo to me. –  Oded Oct 30 '12 at 20:36
3  
@BDotA - The @ before a variable name is ignored - it allows you to use reserved keywords as variable names. Like var @class, for instance. –  Oded Oct 30 '12 at 20:41
1  
The usage of @ is called a verbatim identifier. stackoverflow.com/questions/11851540/… –  David Anderson - DCOM Oct 30 '12 at 20:47
1  
FxCop will actually warn against casting twice in the first one –  jk. Oct 30 '12 at 21:30
show 1 more comment

In short, if you already know what type it can cast to use a C-style cast:

var o = (string) iKnowThisIsAString; 

Note that only with a C-style cast can you perform explicit type coercion. If you don't know whether it's the desired type and you're going to use it if it is, use as keyword:

var txt = o as string;
if (txt != null) return txt.Replace("_","-");

//or for early return:
if (txt == null) return;

Note that as will not call any type conversion operators. It will only be non-null if the object is not null and natively of the specified type.

share|improve this answer
add comment

Fundamentally, they are two different things. But what do you think reads better?

if (sheet.Models.Data is GroupDataModel) 

or

var gdm = sheet.Models.Data as GroupDataModel;
if (gdm != null)

You've got your answer here. Oded made a very valid point, but programming isn't much different from writing articles. It needs to be easily readable by other engineers.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.