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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:52:36+00:00 2026-05-22T01:52:36+00:00

If you must pass a value type to a method, but for some reason,

  • 0

If you must pass a value type to a method, but for some reason, it must be passed as a reference type, is it faster to:

  1. Pass it as an object
  2. Pass is as a ValueType
  3. Pass it as a generic wrapper

I put some example code below to demonstrate exactly what I mean:.

public class Program
{
    public void Main()
    {
        var client = new IncrementedValueGetter();
        int i = 8675309;
        var byObject = client.IncrementObject(i);
        var byValueType = client.IncrementValueType(i);
        var byWrapper = client.IncrementWrapped(new ValueWrapper<int>(i));
    }
}
public class IncrementedValueGetter
{
    public int IncrementObject(object boxedValue)
    {
        return ((int)boxedValue) + 1;
    }
    public int IncrementValueType(ValueType boxedValueType)
    {
        return ((int) boxedValueType) + 1;
    }
    public int IncrementWrapped(ValueWrapper<int> valueWrapper)
    {
        return valueWrapper.Value + 1;
    }
}
public class ValueWrapper<T> 
    where T: struct
{
    private readonly T _value;
    public ValueWrapper(T value)
    {
        _value = value;
    }

    public T Value
    {
        get { return _value; }
    }
}
  • 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-22T01:52:37+00:00Added an answer on May 22, 2026 at 1:52 am

    The first two are actually equivalent, and just generate the standard box IL. The third one requires the construction of your wrapper class, which is likely more expensive than the box call.

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

Sidebar

Related Questions

I have a method that has a parameter of type DateTime that must not
What happens in memory when we pass a value type - which has been
I have some code that has the invariant that an object must be constructed
When i am creating SPFields the constructor states that you must pass in a
If a process with unicode enabled creates a named pipe, it must pass a
This must be a stupid question, but nevertheless I find it curious: Say I
Cannot validate this method because the domain mapping for the return type (XXXProxy) could
First the error: error: a value of type float * cannot be used to
I'm toying with the idea of making primitive .NET value types more type-safe and
Summarization: type MyObject = object end; MyRecord = record end; MyClass = class end;

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.