Use an interface constraint : Interface constraint « Generic « C# / CSharp Tutorial

C# / CSharp Tutorial
1. Language Basics
2. Data Type
3. Operator
4. Statement
5. String
6. struct
7. Class
8. Operator Overload
9. delegate
10. Attribute
11. Data Structure
12. Assembly
13. Date Time
14. Development
15. File Directory Stream
16. Preprocessing Directives
17. Regular Expression
18. Generic
19. Reflection
20. Thread
21. I18N Internationalization
22. GUI Windows Forms
23. 2D
24. Design Patterns
25. Windows
26. XML
27. ADO.Net
28. Network
29. Directory Services
30. Security
31. unsafe
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
C# / CSharp Tutorial » Generic » Interface constraint 
18. 18. 1. Use an interface constraint
 
 
using System; 
 
class NotFoundException : ApplicationException { } 
 
public interface IUserID 
  string Number 
    get; 
    set; 
  
 
  string Name 
    get; 
    set; 
  

 
class Engineer : IUserID 
  string name; 
  string number; 
 
  public Engineer(string n, string num) { 
    name = n; 
    number = num; 
  
 
  // Implement IUserID 
  public string Number 
    get return number; 
    set number = value; 
  
 
  public string Name 
    get return name; 
    set name = value; 
  

 
class Manager : IUserID 
  string name; 
  string number; 
 
  public Manager(string n, string num) { 
    name = n; 
    number = num; 
  
 
  // Implement IUserID 
  public string Number 
    get return number; 
    set number = value; 
  
 
  public string Name 
    get return name; 
    set name = value; 
  

 
class Guest 
 

 
class IDList<T> where T : IUserID 
  T[] idList; 
  int end
 
  public IDList() {  
    idList = new T[10]
    end 0
  
 
  public bool add(T newEntry) { 
    if(end == 10
       return false
 
    idList[end= newEntry; 
    end++; 
 
    return true
  
 
  public T findByName(string name) { 
 
    for(int i=0; i<end; i++) { 
      if(idList[i].Name == name)  
        return idList[i]
    
    throw new NotFoundException()
  
 
  public T findByNumber(string number) { 
    for(int i=0; i<end; i++) { 
      if(idList[i].Number == number)  
        return idList[i]
    
    throw new NotFoundException()
  

 
class MainClass 
  public static void Main() { 
    IDList<Engineer> plist = new IDList<Engineer>()
    plist.add(new Engineer("T""1"))
    plist.add(new Engineer("G""6"))
    plist.add(new Engineer("M""9"))
 
    try 
      Engineer frnd = plist.findByName("G")
      Console.Write(frnd.Name + ": " + frnd.Number)
    catch(NotFoundException) { 
      Console.WriteLine("Not Found")
    
 
    Console.WriteLine()
 
    IDList<Manager> plist2 = new IDList<Manager>()
    plist2.add(new Manager("H""8"))
    plist2.add(new Manager("C""2"))
    plist2.add(new Manager("N""4"))
 
    try 
      Manager sp = plist2.findByNumber("4")
      Console.WriteLine(sp.Name + ": " + sp.Number)
    catch(NotFoundException) { 
        Console.WriteLine("Not Found")
    
 
    // The following declaration is invalid 
    // because Guest does NOT implement IUserID. 
    // IDList<Guest> plist3 = new IDList<Guest>(); // Error! 
  
}

        
G: 6
N: 4
  
18. 18. Interface constraint
18. 18. 1. Use an interface constraint
w___w_w__.j_a_v___a2_s___.__c_om___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.