How do I write test code to exercise a C# generic Pair<TKey, TValue> ?

Posted by Scott Davies on Stack Overflow See other posts from Stack Overflow or by Scott Davies
Published on 2010-04-30T01:02:09Z Indexed on 2010/04/30 1:07 UTC
Read the original article Hit count: 304

Filed under:
|
|

Hi,

I am reading through Jon Skeet's "C# in Depth", first edition (which is a great book). I'm in section 3.3.3, page 84, "Implementing Generics". Generics always confuse me, so I wrote some code to exercise the sample. The code provided is:

using System; 
using System.Collections.Generic;

public sealed class Pair<TFirst, TSecond> : IEquatable<Pair<TFirst, TSecond>>
{
    private readonly TFirst first; 
    private readonly TSecond second;

    public Pair(TFirst first, TSecond second) 
    {
        this.first = first; 
        this.second = second;
    }


    ...property getters...


    public bool Equals(Pair<TFirst, TSecond> other) 
    {
        if (other == null) 
        {
            return false;
        } 
        return EqualityComparer<TFirst>.Default.Equals(this.First, other.First) &&    
            EqualityComparer<TSecond>.Default.Equals(this.Second, other.Second);
    }

My code is:

class MyClass
{
    public static void Main (string[] args)
{
    // Create new pair.
    Pair thePair = new Pair(new String("1"), new String("1"));

    // Compare a new pair to previous pair by generating a second pair.
    if (thePair.Equals(new Pair(new string("1"), new string("1"))))
        System.Console.WriteLine("Equal");
    else
        System.Console.WriteLine("Not equal");
    }
}

The compiler complains:

"Using the generic type 'ManningListing36.Paie' requires 2 type argument(s) CS0305"

What am I doing wrong ?

Thanks,

Scott

© Stack Overflow or respective owner

Related posts about c#

Related posts about generics