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

  • SEARCH
  • Home
  • 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 7014319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:30:21+00:00 2026-05-27T22:30:21+00:00

Could anyone give me a definition of Type Casting and why and when should

  • 0

Could anyone give me a definition of Type Casting and why and when should we use it?

I tried looking for material online, but couldn’t find anything that would explain me why exactly we need type casting and when to use it.

An example would be great!

  • 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-27T22:30:22+00:00Added an answer on May 27, 2026 at 10:30 pm

    Type casting is the mechanism of forcing an object into a particular type. In layman’s terms it is picking up a letter (the abstract) from the mailbox and forcing it into a bill (the specific, the concrete).

    This is typically relevant in software development because we deal with a lot of abstraction so there are times when we’ll inspect a property of a given object and not immediately know the real concrete type of the said object.

    In C# for example I might be interested in examining the contents of a datasource linked to a particular control.

    object data = comboBox.DataSource;
    

    The .DataSource property is of type “object” (which in C# is as abstract as you can get), this is because the DataSource property supports a range of different concrete types and wants to allow the programmer to choose from a wide range of types to use.

    So, returning to my examination. With this object type, there isn’t a great deal I can do with “object data”.

    data.GetType();     // tells me what type it actually is
    data.ToString();    // typically just outputs the name of the type in string form, might be overridden though
    data.GetHashCode(); // this is just something that gets used when the object is put in a hashtable
    data.Equals(x);      // asks if the object is the same one as x
    

    That’s because these are the only APIs defined on the class object in C#. To access more interesting APIs I’m going to have to CAST the object into something more specific.
    So if, for example I KNOW the object is a ArrayList I can cast it to one:

     ArrayList list = (ArrayList) data;
    

    Now I can use the API of an arraylist (if the cast worked). So I can do things like:

     list.Count; // returns the number of items in the list
     list[x];    // accesses a specific item in the list where x is an integer
    

    The cast I showed above is what is known as a “hard” cast. It forces the object into whatever datatype I wanted and (in C#) throws an exception if the cast fails. So typically you only want to use it when you are 100% sure that the object is or SHOULD be that type.

    C# also supports what is known as a “soft” cast that returns null if the cast fails.

    ArrayList list = data as ArrayList;
    if(list != null)
    {
      // cast worked
    }
    else
    {
      // cast failed
    }
    

    You’d use the soft cast when you’re less sure of the type and want to support multiple types.

    Lets say you’re the author of the comboBox class. In that case you might want to support different types of .DataSource so you’d probably write the code using soft casts to support many different types:

    public object DataSource
    {
      set
      {
        object newDataSource = value;
        ArrayList list = newDataSource as ArrayList;
        if(list != null)
        {
           // fill combobox with contents of the list
        }
        DataTable table = newDataSource as DataTable;
        if(table != null)
        {
           // fill combobox with contents of the datatable
        }
        // etc
      }
    }
    

    Hope that helps explain type casting to you and why it is relevant and when to use it! 🙂

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

Sidebar

Related Questions

Could anyone give an example of why it would be advantageous to use MVC
Could anyone give me a good use case for ActionScript dynamic classes? Because it
HI Could anyone give a sample program to implement the is_same_type type trait in
Could anyone give me examples of how to use parameterized queries with MySQL/PHP please?
Could anyone give me examples, how I could use diffstat with subversion? I mean,
I checked the API for Maps, only to find for JavaScript. Could anyone give
Could anyone give me suggestions/advice on making type level integers in OCaml (3.12) supporting
Could anyone give me an example or a good place to start looking on
Could anyone give me some help with this please? The content needs to appear
Could anyone give some pointers on why the impure computations in Haskell are modelled

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.