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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:20:17+00:00 2026-05-23T14:20:17+00:00

Say I have a generic method in C# that accepts two values of type

  • 0

Say I have a generic method in C# that accepts two values of type T:

public void M<T>(T a, T b)
{
    ...
}

Inside body of M() I wish to compare both input values for equality. Since I don’t know anything about their run-time types except that they are the same type, I could do this using the object.Equals() static method and let it choose the best way:

public void M<T>(T a, T b)
{
    if (object.Equals(a, b))
    {
        ...
    }
    else
    {
        ...
    }
}

The problem I see here is needless boxing of the two values when T isn’t a reference type. I would like to avoid that penalty, because M() is called very frequently.

My question is: is there a better way to go about this? I’m obviously interested in a solution that wouldn’t involve too much analysis of T up front, which would offset gains from boxing evasion.

TIA.

  • 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-23T14:20:17+00:00Added an answer on May 23, 2026 at 2:20 pm
    if(EqualityComparer<T>.Default.Equals(a,b))
    {...}
    

    this uses IEquatable<T> when available to avoid boxing, and handles value-types, reference-types, and “lifted” usage against Nullable<T> to avoid boxing in almost all scenarios.

    When IEquatable<T> is not available, it must defer to object.Equals, so boxing of value-types may occur.

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

Sidebar

Related Questions

Say I have a method that is expecting a generic collection parameter of a
say, I have a generic command trait with an execute method that takes an
Which is your preference? Let's say we have a generic Product table that has
Say that i have a generic dictionary with data like this (I hope the
I have a two generic abstract types: Entity and Association . Let's say Entity
Lets say have this immutable record type: public class Record { public Record(int x,
Is it possible to have a generic method that requires a delegate(?) for a
Let's say I have a ActionResult method that has a pageNumber parameter and a
I have a generic interface, say IGeneric. For a given type, I want to
I have a question concerning a generic type parameter. Let's say I have 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.