A base class constraint : Generic « Language Basics « C# / C Sharp

C# / C Sharp
1. 2D Graphics
2. Collections Data Structure
3. Components
4. Database ADO.net
5. Development Class
6. Event
7. File Stream
8. GUI Windows Form
9. Language Basics
10. Network
11. Office
12. Regular Expressions
13. Services Event
14. Thread
15. Web Services
16. Windows
17. XML
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# / CSharp Tutorial
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# / C Sharp » Language Basics » GenericScreenshots 
A base class constraint
A base class constraint


using System;

class MyBase {
  public void hello() {
    Console.WriteLine("Hello");
  }
}

class B : MyBase { }

class { }

class Test<T> where T : MyBase {
  T obj;

  public Test(T o) {
    obj = o;
  }

  public void sayHello() {
    obj.hello();
  }
}

class BaseClassConstraintDemo {
  public static void Main() {
    MyBase a = new MyBase();
    B b = new B();
    C c = new C();

    Test<MyBase> t1 = new Test<MyBase>(a);

    t1.sayHello();

    Test<B> t2 = new Test<B>(b);

    t2.sayHello();

    // The following is invalid because
    // C does not inherit MyBase.
    // Test<C> t3 = new Test<C>(c); // Error!
  }
}


           
       
Related examples in the same category
1. A simple generic classA simple generic class
2. A Generic Class with Two Type ParametersA Generic Class with Two Type Parameters
3. Use an interface constraintUse an interface constraint
4. A new() constructor constraint
5. Demonstrate a reference constraint
6. Demonstrate a value type constraint
7. Create relationship between two type parameters
8. Use multiple where clauses
9. Demonstrate the default keywordDemonstrate the default keyword
10. Demonstrate a generic structDemonstrate a generic struct
11. Demonstrate a generic methodDemonstrate a generic method
12. A simple generic delegateA simple generic delegate
13. Convert event to use generic delegateConvert event to use generic delegate
14. Demonstrate a generic interfaceDemonstrate a generic interface
15. Comparing Instances of a Type ParameterComparing Instances of a Type Parameter
16. A simple generic class hierarchyA simple generic class hierarchy
17. A derived class with its own type parametersA derived class with its own type parameters
18. A non-generic class can be the base class of a generic derived classA non-generic class can be the base class of a generic derived class
19. Overriding a virtual method in a generic classOverriding a virtual method in a generic class
20. A prototypical generic methodA prototypical generic method
21. Generic methods can overload nongeneric methodsGeneric methods can overload nongeneric methods
22. 'This' Reference for Generic Types
23. Derivation Constraint
24. A type parameter can be used as a constraintA type parameter can be used as a constraint
25. Reference Type ConstraintReference Type Constraint
26. Default Constructor ConstraintDefault Constructor Constraint
27. Nested generic TypesNested generic Types
28. Serialization for generic typeSerialization for generic type
29. Deserialize generic typeDeserialize generic type
30. Implement generic IEnumerable and IEnumeratorImplement generic IEnumerable and IEnumerator
31. Implement generic IEnumerableImplement generic IEnumerable
32. Generic class with interfaceGeneric class with interface
w_w_w._j__av__a___2___s_.__c___o___m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.