Comparing Instances of a Type Parameter : Generic Class « Generics « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Generics » Generic ClassScreenshots 
Comparing Instances of a Type Parameter
Comparing Instances of a Type Parameter
 

using System;

class MyClass : IComparable {
  public int val;

  public MyClass(int x) { 
    val = x; 
  }
  public int CompareTo(object obj) {
    return val - ((MyClassobj).val;
  }
}

class CompareDemo {
  public static bool contains<T>(T what, T[] obswhere T : IComparable {
    foreach(T v in obs)
      if(v.CompareTo(what== 0)
        return true;
    return false;
  }

  public static void Main() {
    int[] nums = 1234};

    if(contains(2, nums))
      Console.WriteLine("2 is found.");

    string[] strs = "one""two""three"};

    if(contains("two", strs))
      Console.WriteLine("two is found.");

    MyClass[] mcs = new MyClass(1)new MyClass(2),
                      new MyClass(3)new MyClass(4) };

    if(contains(new MyClass(3), mcs))
      Console.WriteLine("MyClass(3) is found.");

  }
}
           
         
  
Related examples in the same category
1.A simple generic classA simple generic class
2.Declare the generic class.
3.A Generic Class with Two Type ParametersA Generic Class with Two Type Parameters
4.Generic Fields
5.Create relationship between two type parameters
6.Demonstrate the default keywordDemonstrate the default keyword
7.'This' Reference for Generic Types
8.Generic class with interfaceGeneric class with interface
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.