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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:17:40+00:00 2026-05-13T06:17:40+00:00

Sometimes I have to implement an interface or inherit a virtual (MustInherit) that the

  • 0

Sometimes I have to implement an interface or inherit a virtual (MustInherit) that the base method expects an object, whilst I know that the value I will be passing will always be an Integer for example.

What should be the best performance from the examples below:

Public Sub DoSomething(ByVal obj As Object)
    'option 1:
    Dim x As Integer = obj

    'option 2:
    Dim y = DirectCast(obj, Integer)
End Function

Considerations:

  • Option 1: No casting but is maybe not so proper, does it cost less performance?
  • Option 2: Casting when the type is known, but feels more safe.

Note: Please don’t comment with “Why wouldn’t you want to implement it in different way” etc. etc.
My question is not how to do this, I didn’t find an example of how to ask it, my question is just what option should be the rightes, and what will cost more performance.

  • 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-13T06:17:40+00:00Added an answer on May 13, 2026 at 6:17 am

    Your Option 1 is still casting — in fact, it’s doing more than that, it’s performing a conversion. For example, if obj is the string “1”, Option 1 will convert it to the integer 1, whereas Option 2 will fail with an InvalidCastException. (In the good old days, this was known as “evil type coercion,” which sounds way cooler than implicit conversion, but also highlights the potential danger that this approach can disguise errors and result in unanticipated behaviour.)

    So Option 1 will probably be slightly less performant because it’s doing a bit more work (conversion as opposed to a plain old cast). But the difference is likely to be insignificant if you’re only ever passing in integers. As always, if performance really matters, measure.

    Probably a more important consideration than perf is the desired behaviour. E.g. if someone passes in the string “1”, do you want it to work as if they’d passed in the integer 1? If so, go for Option 1. If you want it to fail in this case, go for Option 2.

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

Sidebar

Related Questions

Sometimes I have to work on code that moves the computer clock forward. In
Let's suppose I have an interface named Controller. Several classes implement this interface and
I want to implement a derived class that should also implement an interface, that
I have two Java class hierarchies that share a common ancestor and implement a
I know that virtual and static methods are opposing concepts, but I think that
I find it sometimes annoying that I have to initialise all POD-types manually. E.g.
I sometimes have difficulties with other people who wish to solve a problem when
I sometimes have issues with the incremental rebuild on visual C++ (2003 currently ).
In our application we sometimes have to make minor GUI modifications for different customers:
In my database application I sometimes have to deal with null strings in the

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.