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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:28:16+00:00 2026-05-16T06:28:16+00:00

Is there any point in doing this? public static void Write<T>(T value) { textWriter.Write(value.ToString());

  • 0

Is there any point in doing this?

public static void Write<T>(T value)
{
    textWriter.Write(value.ToString());
}

…as supposed to this:

public static void Write(object value)
{
    textWriter.Write(value.ToString());
}

Setting aside the obvious null dereference possibility, If I where to write a lot of value types using this method wouldn’t the former be much better because it will have it’s own version of the write method to call, or is it just gonna bloat the binary in terms of a lot of additional code being generated?

The performance implication of such a thing might be negligible, but I’m curious, it’s a lot more compact than providing an overload for each and every value type in the BCL, like most writers in the BCL already do.

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

    From what I understand, in both cases boxing occurs.

    The latter is obvious as the value is already boxed.

    The former is less obvious, but as a virtual method is called on a valuetype, it will need to be boxed to perform the callvirt.

    Edit: I just checked the emitted IL, and there is no explicit boxing occuring in the generic case. Something rings a bell though.

    Edit 2: I might have been confusing myself with the case using interfaces. There clearly boxing occurs.

    Edit 3: Boxing does occur, if ToString() is not overriden in the value type.

    I get this from ECMA-335 part 3 pg 25 (only noting the last case):

    If thisType is a value type and
    thisType does not implement method
    then ptr is dereferenced, boxed, and
    passed as the ‘this’ pointer to the
    callvirt of method

    This last case can only occur when
    method was defined on System.Object,
    System.ValueType, or System.Enum and
    not overridden by thisType. In this
    last case, the boxing causes a copy of
    the original object to be made,
    however since all methods on
    System.Object, System.ValueType, and
    System.Enum do not modify the state of
    the object, this fact can not be
    detected.

    Edit 4: Here is a similar question on SO.

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

Sidebar

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.