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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:58:44+00:00 2026-05-11T03:58:44+00:00

When programming interfaces, I’ve found I’m doing a lot of casting or object type

  • 0

When programming interfaces, I’ve found I’m doing a lot of casting or object type conversion.

Is there a difference between these two methods of conversion? If so, is there a cost difference or how does this affect my program?

public interface IMyInterface {     void AMethod(); }  public class MyClass : IMyInterface {     public void AMethod()     {        //Do work     }      // Other helper methods.... }  public class Implementation {     IMyInterface _MyObj;     MyClass _myCls1;     MyClass _myCls2;      public Implementation()     {         _MyObj = new MyClass();          // What is the difference here:         _myCls1 = (MyClass)_MyObj;         _myCls2 = (_MyObj as MyClass);     } } 

Also, what is ‘in general’ the preferred method?

  • 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-11T03:58:45+00:00Added an answer on May 11, 2026 at 3:58 am

    The answer below the line was written in 2008.

    C# 7 introduced pattern matching, which has largely replaced the as operator, as you can now write:

    if (randomObject is TargetType tt) {     // Use tt here } 

    Note that tt is still in scope after this, but not definitely assigned. (It is definitely assigned within the if body.) That’s slightly annoying in some cases, so if you really care about introducing the smallest number of variables possible in every scope, you might still want to use is followed by a cast.


    I don’t think any of the answers so far (at the time of starting this answer!) have really explained where it’s worth using which.

    • Don’t do this:

      // Bad code - checks type twice for no reason if (randomObject is TargetType) {     TargetType foo = (TargetType) randomObject;     // Do something with foo } 

      Not only is this checking twice, but it may be checking different things, if randomObject is a field rather than a local variable. It’s possible for the ‘if’ to pass but then the cast to fail, if another thread changes the value of randomObject between the two.

    • If randomObject really should be an instance of TargetType, i.e. if it’s not, that means there’s a bug, then casting is the right solution. That throws an exception immediately, which means that no more work is done under incorrect assumptions, and the exception correctly shows the type of bug.

      // This will throw an exception if randomObject is non-null and // refers to an object of an incompatible type. The cast is // the best code if that's the behaviour you want. TargetType convertedRandomObject = (TargetType) randomObject; 
    • If randomObject might be an instance of TargetType and TargetType is a reference type, then use code like this:

      TargetType convertedRandomObject = randomObject as TargetType; if (convertedRandomObject != null) {     // Do stuff with convertedRandomObject } 
    • If randomObject might be an instance of TargetType and TargetType is a value type, then we can’t use as with TargetType itself, but we can use a nullable type:

      TargetType? convertedRandomObject = randomObject as TargetType?; if (convertedRandomObject != null) {     // Do stuff with convertedRandomObject.Value } 

      (Note: currently this is actually slower than is + cast. I think it’s more elegant and consistent, but there we go.)

    • If you really don’t need the converted value, but you just need to know whether it is an instance of TargetType, then the is operator is your friend. In this case it doesn’t matter whether TargetType is a reference type or a value type.

    • There may be other cases involving generics where is is useful (because you may not know whether T is a reference type or not, so you can’t use as) but they’re relatively obscure.

    • I’ve almost certainly used is for the value type case before now, not having thought of using a nullable type and as together 🙂


    EDIT: Note that none of the above talks about performance, other than the value type case, where I’ve noted that unboxing to a nullable value type is actually slower – but consistent.

    As per naasking’s answer, is-and-cast or is-and-as are both as fast as as-and-null-check with modern JITs, as shown by the code below:

    using System; using System.Diagnostics; using System.Linq;  class Test {     const int Size = 30000000;      static void Main()     {         object[] values = new object[Size];         for (int i = 0; i < Size - 2; i += 3)         {             values[i] = null;             values[i + 1] = 'x';             values[i + 2] = new object();         }         FindLengthWithIsAndCast(values);         FindLengthWithIsAndAs(values);         FindLengthWithAsAndNullCheck(values);     }      static void FindLengthWithIsAndCast(object[] values)             {         Stopwatch sw = Stopwatch.StartNew();         int len = 0;         foreach (object o in values)         {             if (o is string)             {                 string a = (string) o;                 len += a.Length;             }         }         sw.Stop();         Console.WriteLine('Is and Cast: {0} : {1}', len,                           (long)sw.ElapsedMilliseconds);     }      static void FindLengthWithIsAndAs(object[] values)             {         Stopwatch sw = Stopwatch.StartNew();         int len = 0;         foreach (object o in values)         {             if (o is string)             {                 string a = o as string;                 len += a.Length;             }         }         sw.Stop();         Console.WriteLine('Is and As: {0} : {1}', len,                           (long)sw.ElapsedMilliseconds);     }      static void FindLengthWithAsAndNullCheck(object[] values)             {         Stopwatch sw = Stopwatch.StartNew();         int len = 0;         foreach (object o in values)         {             string a = o as string;             if (a != null)             {                 len += a.Length;             }         }         sw.Stop();         Console.WriteLine('As and null check: {0} : {1}', len,                           (long)sw.ElapsedMilliseconds);     } } 

    On my laptop, these all execute in about 60ms. Two things to note:

    • There’s no significant difference between them. (In fact, there are situations in which the as-plus-null-check definitely is slower. The above code actually makes the type check easy because it’s for a sealed class; if you’re checking for an interface, the balance tips slightly in favour of as-plus-null-check.)
    • They’re all insanely fast. This simply will not be the bottleneck in your code unless you really aren’t going to do anything with the values afterwards.

    So let’s not worry about the performance. Let’s worry about correctness and consistency.

    I maintain that is-and-cast (or is-and-as) are both unsafe when dealing with variables, as the type of the value it refers to may change due to another thread between the test and the cast. That would be a pretty rare situation – but I’d rather have a convention which I can use consistently.

    I also maintain that the as-then-null-check gives a better separation of concerns. We have one statement which attempts a conversion, and then one statement which uses the result. The is-and-cast or is-and-as performs a test and then another attempt to convert the value.

    To put it another way, would anyone ever write:

    int value; if (int.TryParse(text, out value)) {     value = int.Parse(text);     // Use value } 

    That’s sort of what is-and-cast is doing – although obviously in a rather cheaper way.

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

Sidebar

Ask A Question

Stats

  • Questions 115k
  • Answers 115k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer CP source.log %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%.log But it's locale dependent. I'm not sure… May 11, 2026 at 10:27 pm
  • Editorial Team
    Editorial Team added an answer Chances are that the sed you are using doesn't understand… May 11, 2026 at 10:27 pm
  • Editorial Team
    Editorial Team added an answer the JMF (Java Media Framework) is a good starting point.… May 11, 2026 at 10:27 pm

Related Questions

When programming interfaces, I've found I'm doing a lot of casting or object type
When programming in C++ against the browser's DOM each engine has a different set
When programming C++ we used to create copy constructors when needed (or so we
Imagine having the following scenario: You need to create a system where the Back
I agree, that programming against interfaces is a good practice. In most cases in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.