InvalidCastException, As and Is operator

question
question-interview @Image courtesy of ddpavumba / FreeDigitalPhotos.net

InvalidCastException, As and Is operator

Welcome to CodeSpread!

When we try to cast or convert an object, we sometimes fall into the trap of InvalidCastException.

What is InvalidCastException?

InvalidCastException: This exception is thrown when there is invalid casting operation.

MSDN says

InvalidCastException is thrown if:

  •   For a conversion from a Single or a Double to a Decimal, the source value is infinity, Not-a-Number (NaN), or too large to be represented as the destination type.
  •  A failure occurs during an explicit reference conversion.

How can we avoid InvalidCastException?

We have operators like “ is” and “as” which can be used to avoid cast failures or determine the case success without causing an exception.

Is operator

The “is” operator returns only a Boolean value. This operator is mainly used to match an object’s type.It is not involved in actual cast operation.

if (Adam is Employee)
{
Employee Adin = (Employee)Adam;
}

As operator

The “as” operator is used for actual cast operation where it returns the cast value if there is no exception and the cast is a success.

Employee Adin = Adam as Employee;

Note: The “as” operator more efficient .

Author: hershey

A passion for knowledge drives me to do programming, A passion for programming drives me to create something different, A passion for creation drives me to spread the knowledge.

Share This Post On

2 Comments

  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

More from CodeSpread:

  • Interview Series: Polymorphism Question- Answer UpdatedInterview Series: Polymorphism Question- Answer UpdatedWe are sharing our third question of the series in the form of comic, Please help our kid in the comic by sharing your answers through comments or Facebook  or Google plus or twitter[@CodeSpreadBlo...
  • One Minute Video on Int.Parse,Int.TryParse and Convert Operation in C#One Minute Video on Int.Parse,Int.TryParse and Convert Operation in C#We have taken a new step in our journey by traversing a new path of video channel. This is our inaugural video. Please subscribe to codespread channel http://www.youtube.com/user/codespread &nbs...
  • Styles of Source Code Comments in C#, SQL, XML, HTML, CSS, JavaScriptStyles of Source Code Comments in C#, SQL, XML, HTML, CSS, JavaScriptDevelopers take months to write beautiful pieces of code and reviewer gets only few hours or I say, minutes to provide suggestions and improvisations. Then it becomes a hard deal for a developer to...
  • My first experience with LISTS in C#My first experience with LISTS in C#Hi guys, this is Sathish, Lets discuss the concepts of lists. These are the concepts which I have planned to discuss with you guys today. What is a List and how it is advantageous than array...
  • Asp.Net: More about CookiesAsp.Net: More about CookiesWe have seen, how our cookies looks in our last article Cookies Part 1. In this article, we will talk about their properties,limitations and technical part. Properties and Limitations Cook...
  • C#: Anonymous Type is not anonymous!C#: Anonymous Type is not anonymous!Why Anonymous Type? Anonymous types provide you the ease of having set of properties (Read only) into a single object without knowing the type. It’s the job of compiler to generate the type inform...
  • Don’t Mess with App_OfflineDon’t Mess with App_OfflineOne of the most useful features of ASP.NET is the "App_Offline.htm" . It provides a much easy way to make the ASP.NET application offline while we do our modifications on the site. How we do it? ...
  • How to view Intermediate Code in C# using Ildasm.ExeHow to view Intermediate Code in C# using Ildasm.ExeHi guys, this is Sathish again, today let’s discuss how to view the intermediate code in c#. We know that CLR (Common Language Runtime) is used to convert the MSIL (Microsoft Intermediate Language...