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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:02:20+00:00 2026-05-13T21:02:20+00:00

I am trying to get a difference between the type casting methods. eg. Method

  • 0

I am trying to get a difference between the type casting methods.

eg.

Method 1

public byte fun()
{
   object value=1;
   return (byte)value; // this gives me error
}

Method 2

public byte fun()
{
   object value=1;
   return byte.Parse(value.ToString()); // this runs 
}

Method 3

public byte fun()
{
   object value=1;
   return Convert.ToByte(value); // this runs
}

What is the difference between all the three.
How they are working internally.
What are value type and refrence type here.
Which function can convert value type to ref type and vice versa

Edit 2

When i writes this line what datatype ‘1’ will be treated by default int32, byte or something else.

object value=1;

  • 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-13T21:02:21+00:00Added an answer on May 13, 2026 at 9:02 pm

    There are a lot of questions here.

    Method 1 fails because you cannot do an unbox and a cast in a single operation. You’re setting “value” to a boxed integer. When you try to do the cast, you’re unboxing the integer and trying to cast to a byte in a single operation, which fails. This does work, btw:

    return (byte)( (int)value) ); // Unbox, then cast, in two operations
    

    Method 2 works because you’re converting the integer to a string, then using byte.Parse to convert it to a byte. This is very expensive, since it’s going to/from strings.

    Method 3 works because it sees that the object in value is IConvertible (int), and uses the appropriate conversion operation to convert to byte. This is probably a more efficient way of approaching it, in this case. Since “value” is storing an int, and int supports IConvertible, Convert.ToByte will basically do a null check, then call Convert.ToByte(int), which is quite fast (it does bounds checking, and a direct cast).

    I’d recommend reading Eric Lippert’s blog post titled Representation and Identity. It covers casting in detail, and explains why method 1 fails…

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

Sidebar

Related Questions

I am trying to get the difference between time which is in the form
I am trying to get the difference between 2 containers but the containers are
I am trying to learn the difference between getGenericParameterTypes and getParameterTypes methods. I know
What is the difference between using: $.ajax({ type: 'POST', url: Drupal.settings.basePath + 'module/get/' +
I've been trying to understand the difference between JavaScript's comparison operators: identity and equality.
I have been trying to overload my index method. Here are my index methods:
I am trying to write a quick snippet to demonstrate the difference between an
Setup: I was trying to understand the difference between identifying and non-identifying relationships when
I'm trying to use PHPUnit to unit test some class methods that return SQL.
I'm trying to get a queryset to issue its query over a different DB

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.