Guest User

Untitled

a guest
Jul 25th, 2013
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             List<Item> l = new List<Item>()
  13.                                {
  14.                                    new Item("1", "2")
  15.                                };
  16.             Console.WriteLine(l.Contains(new Item("1", "2"))); // True
  17.             Console.WriteLine(l.IndexOf(new Item("1", "2"))); // 0
  18.             Console.Read();
  19.         }
  20.     }
  21.  
  22.     public class Item
  23.     {
  24.         private string param1;
  25.         private string param2;
  26.  
  27.         public Item(string p1, string p2)
  28.         {
  29.             this.param1 = p1;
  30.             this.param2 = p2;
  31.         }
  32.  
  33.         public override bool Equals(object obj)
  34.         {
  35.             return obj is Item
  36.                    && ((Item)obj).param1 == this.param1
  37.                    && ((Item)obj).param2 == this.param2;
  38.         }
  39.  
  40.         public override int GetHashCode()
  41.         {
  42.             return param1.GetHashCode() ^ param2.GetHashCode();
  43.         }  
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment