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

Given that I have the following in my knowledge-database: 1 0 6 20 0
I am working on a site that is using the unfortunate practice of wrapping
The following example is one way in JavaScript to create and manipulate objects, i.e.
I have a web-service running on Windows Azure which returns JSON that I consume
I have a table in SQL with more than 1 million records which I
I have writen a fastcGI application using C and C++ I have a free
I have several UserControls that are .cs files and extend WebControl. I add them
I executed the following query both in phpMyAdmin & MySQLdb (python). SELECT *, (SELECT
EDIT: I am using Firefox 3.1+ specifically, as this is an intranet application. I
I've seen a few different questions on this but I can't seem to find

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.