boxing and generics

This is a discussion on boxing and generics within the C# Programming forums, part of the General Programming Boards category; I have two ref parameters for a generic method like this: Code: static T Min<T>(ref T a, ref T b) ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    330

    boxing and generics

    I have two ref parameters for a generic method like this:

    Code:
        
    static T Min<T>(ref T a, ref T b) where T : IComparable<T>
    {
      return a.CompareTo(b) < 0 ? a : b ;
    }
    Now when I call this with int parameters

    Code:
          int a = 0, b = 1;
    
          int c = Min(ref a, ref b);
    will a and b get boxed or not?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,166
    I believe if you're relying on the ICompareable<T>.CompareTo() method, then yes, because the parameter is type object. int has another overload that takes an int as a parameter, which would avoid boxing, but that isn't available in your generic method.

    EDIT: You might find this interesting. A value copy of MyType is returned from the Min() function so the original value stays the same:
    Code:
        class Program
        {
            static void Main(string[] args)
            {
                MyType lower = new MyType { Num = 5 }, higher = new MyType { Num = 7 };
    
                MyType min = Min(ref lower, ref higher);
                min.Num = 100;
                Console.WriteLine("lower = {0}, higher = {1}, min = {2}", lower.Num, higher.Num, min.Num);
            }
    
            static T Min<T>(ref T a, ref T b) where T : IComparable<T>
            {
                return a.CompareTo(b) < 0 ? a : b;
            }
        }
    
        struct MyType : IComparable<MyType>
        {
            public int Num;
    
            public int CompareTo(MyType other)
            {
                return Num.CompareTo(other.Num);
            }
        }
    My output:
    Code:
    lower = 5, higher = 7, min = 100
    Last edited by itsme86; 03-23-2012 at 11:22 AM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    I found the answer by accident in the book CLR via C#

    they are not boxed because T has the IComparable<T> constraint and not IComparable.

    If you do the following it would be boxed:

    Code:
    int x =1, y = 2;
    
    IComparable c = x;
    
    x.CompareTo(y); // boxed

    This will not be boxed:

    Code:
    int x =1, y = 2;
    
    IComparable<int> c = x;
    
    x.CompareTo(y); // not boxed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trying to wrap my brain around generics...
    By monk64 in forum C# Programming
    Replies: 3
    Last Post: 06-10-2010, 08:52 AM
  2. Native generics?
    By audinue in forum Tech Board
    Replies: 6
    Last Post: 02-04-2009, 09:16 PM
  3. generics question
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-11-2008, 02:34 AM
  4. generics type instantiation
    By George2 in forum C# Programming
    Replies: 4
    Last Post: 05-08-2008, 03:41 AM
  5. Using 'new' to replace GetType() when using generics
    By MisterT in forum C# Programming
    Replies: 3
    Last Post: 10-14-2006, 03:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21