Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 137101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:03:11+00:00 2026-05-11T07:03:11+00:00

I find that using the following: TreeViewItem i = sender as TreeViewItem; if(i !=

  • 0

I find that using the following:

TreeViewItem i = sender as TreeViewItem; if(i != null){ ... } 

is easier to write and understand than:

if(sender.GetType() == typeof(TreeViewItem)){     TreeViewItem i = (TreeViewItem)sender;     ... } 

Are there compelling reasons not to use the first construct?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T07:03:12+00:00Added an answer on May 11, 2026 at 7:03 am

    I prefer casts to as in most cases because usually if the type of the object is wrong, that indicates a bug. Bugs should cause exceptions IMO – and an InvalidCastException at exactly the line which performs the cast is a lot clearer than a NullReferenceExceptionmuch later in the code.

    as should be used when it’s valid and legal to have been passed a reference to an object of the type that you don’t want. That situation does come up, but not as often as normal casting in my experience.

    Comparing types using GetType(), however, is very rarely the right solution – it’s only appropriate when you want to check for the exact type involved rather than a compatible type.

    I’ve written a significantly longer answer about the ‘cast vs as’ discussion elsewhere.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very surprised to find that the following compiles: #include <iostream> using namespace std;
I'm new to vim and I find that I am using the following command
In MS SQL Server, using the following data, how do I find empIds that
Using ICU 4.0 regex library, I find that the following regex is exhibiting exponential
I find that after running or debugging my application a few times using eclipse,
-o changes the output filename (I found that using --help) But I can't find
I've only been using SSIS briefly, but I find that my complaints are numerous.
When I wait on a non-signaled Event using the WaitForSingleObject function, I find that
Possible Duplicate: Comparing Two Arrays Using Perl I am trying to find elements that
I am writing a Perl script (in Windows) that is using File::Find to index

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.