Demonstrate using a simple generic interface with parameter constraint. - CSharp Custom Type

CSharp examples for Custom Type:Generics

Description

Demonstrate using a simple generic interface with parameter constraint.

Demo Code

using System;/* w  w w  .j  a  va 2 s  .c  o m*/
class Program
{
   static void Main(string[] args)
   {
      Student s = new Student();
      s.Level = GradeLevel.Freshman;
      Console.WriteLine("Sudent is a {0}", s.Level);
      Employee e = new Employee();
      e.Level = EmployeeLevel.GrandPoobah;
      Console.WriteLine("Employee is a {0}", e.Level);
   }
   interface ICategorizable<T> where T : struct
   {
      // Gets or sets an object's category.
      T Level { get; set; }
   }
   enum GradeLevel
   {
      Freshman, Sophomore, Junior, Senior, Graduate, Playboy
   }
   class Student : ICategorizable<GradeLevel>
   {
      public GradeLevel Level { get; set; }
   }
   enum EmployeeLevel
   {
      FullTime, PartTime, Intern, Manager, Executive, GrandPoobah
   }
   class Employee : ICategorizable<EmployeeLevel>
   {
      public EmployeeLevel Level { get; set; }
   }
}

Result


Related Tutorials