Kamis, 06 Desember 2007

Generic Constraint


using System;


using System.Windows.Forms;


namespace Generic101


{


public partial class Form1 : Form


{


public Form1()


{


InitializeComponent();


}


private void Form1_Load(object sender, EventArgs e)


{


Testclass testdoang = new Testclass();


MethodA(testdoang);


MethodB(testdoang);


TestClass<Chubby> damn = new TestClass<Chubby>();


}


public void MethodA<I>(I parameter1) where I : IComparable


{


parameter1.CompareTo(this);


}


public void MethodB(IComparable param1)


{


param1.CompareTo(this);


}


}


public class Testclass: IComparable


{


#region IComparable Members


public int CompareTo(object obj)


{


throw new Exception("The method or operation is not implemented.");


}


#endregion


}


public interface A


{


void method1();


void method2();


}


public interface B


{


void method1();


void method3();


void method4();


}


public class C


{


public void method1()


{


}


public void method3(){}


}


public class TestClass<T> where T:C,A,B,new()


{


public TestClass()


{


T aclass=new T();


aclass.method1();


aclass.method2();


aclass.method3();


aclass.method4();


}


}


public class Chubby: C,A,B


{


public Chubby()


{


}


public new void method1()


{}


public new void method3()


{ }


public void method2()


{


}


public void method4()


{


// throw new NotImplementedException();


}


}


}

Tidak ada komentar: