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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:47:08+00:00 2026-05-30T06:47:08+00:00

I am curious to know if all casts in C# result in boxing, and

  • 0

I am curious to know if all casts in C# result in boxing, and if not, are all casts a costly operation?

Example taken from Boxing and Unboxing (C# Programming Guide)

    int i = 123;
    // The following line boxes i.
    object o = i;  

This line obviously causes boxing (wrapping up the int type as an object).
This is an operation that is considered costly, since it creates garbage that will be collected.

What about casts from 2 different types of reference types? what is the cost of that? can it be properly measured? (compared to the previous example)

For example:

public class A
{
}

public class B : A
{
}

var obj = new B();
var obj2 = (A)obj; // is this an "expensive" operation? this is not boxing
  • 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-30T06:47:09+00:00Added an answer on May 30, 2026 at 6:47 am

    I am curious to know if all conversions in C# result in boxing.

    No. Only boxing conversions result in boxing, hence the name “boxing conversions”. Boxing conversions are all built-in conversions from value types to reference types — either to a class that the value type inherits from, or to an interface that it implements. (Or to an interface compatible with an interface it implements, via a covariant or contravariant reference conversion.)

    are all conversions a costly operation?

    No. Identity conversions are zero cost because the compiler can elide them entirely.

    What are the costs of implicit and explicit reference conversions?

    Implicit reference conversions are zero cost. The compiler can elide them entirely. That is, converting from Giraffe to its base type Animal, or Giraffe to its implemented interface type IAmATallMammal, are free.

    Explicit reference conversions involve a runtime check to verify that the reference does in fact refer to an object of the desired type.

    Whether that runtime check is “costly” or not depends on your budget.

    can that cost be properly measured?

    Sure. Decide what resource is relevant to you — time, say — and then carefully measure your consumption of time with a stopwatch.

    A question you did not ask but probably should have:

    What are the most expensive conversions?

    User-defined conversions are nothing more than a syntactic sugar for a method call; that method can take arbitrarily long, like any method.

    Dynamic conversions start the compiler again at runtime; the compiler may take arbitrarily long to perform a type analysis, depending on how hard an analysis problem you choose to throw at it.

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

Sidebar

Related Questions

I already know all the problems in programming based on timeline, but I'm curious
I am curious to know why this is happening. Please read the code example
Just curious to know if yui improves performance? As all the css and javascript
I'm curious to know if there's something similar to the Task Parallel Library from
Just curious: sure, we all know that the general case of type inference for
I am curious to know if eclipse has the feature of showing all the
I am just curious to know that why default css doesn't works for all
I am just curious to know if at all there is any technical/theoretical reasons
I am curious to know is it possible to create a conditional not null
i was just curious to know if all the codes provided by Apple is

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.