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 6038115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:10:18+00:00 2026-05-23T06:10:18+00:00

I am wondering if using the keyword as in the following code is a

  • 0

I am wondering if using the keyword “as” in the following code is a safe way (i.e. won’t blow up) of casting in C#:

public void abc(ref Object dataSource)
{
    DataTable table = dataSource as DataTable;
}

Is there a safer way of casting?

  • 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. Editorial Team
    Editorial Team
    2026-05-23T06:10:19+00:00Added an answer on May 23, 2026 at 6:10 am

    It won’t blow up… but that doesn’t necessarily mean it’s safe.

    Typically when I use a cast for a reference conversion, it’s because I really, really think that the execution-time type is the one I’m specifying. If it’s not, that indicates a bug in my code – and I’d rather that manifested itself as an exception.

    If you’ve got bad data in your system, then continuing as if everything was fine is the dangerous path, not the safe path. That’s the way that as will take you, whereas a cast would throw an InvalidCastException, aborting whatever you’re doing before you get the chance to cause mayhem with the bad data.

    as is good if it’s valid for the object not to be of the given type – if it doesn’t indicate a bug. You almost always see the pattern of:

    Foo x = y as Foo;
    if (x != null)
    {
        ...
    }
    

    See MSDN for more details about what as does.

    Note also that you probably don’t want to use ref in your method. See my article on parameter passing for more details. Most of the time if I see people using ref a lot, it’s because they don’t understand what it really means 🙂

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

Sidebar

Related Questions

I'm wondering if using the following Javascript code is reliable: if (!document.cookie) { alert('Cookies
Possible Duplicate: Casting vs using the ‘as’ keyword in the CLR I recently learned
I am wondering if it is possible while using the keyword 'new' to return
I have written the following C99 code and was wondering about the struct declaration.
I'm wondering if this is completely thread-safe and whether or not the volatile keyword
I was wondering if using HTML entities in meta tags (like keywords and description)
I've been wondering if using long descriptive variable names in WinForms C# matters for
I am wondering about using the abstraction in Zend Db RowSet instead of joins,
I was wondering if using a getter to get the same property over and
I'm just wondering when using CDATA in XML if i get the text content

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.