Csharp/C Sharp/Generics/Generic Class
Версия от 15:31, 26 мая 2010; (обсуждение)
Содержание
A Generic Class with Two Type Parameters
using System;
class MyGenericClass<T, V> {
T ob1;
V ob2;
public MyGenericClass(T o1, V o2) {
ob1 = o1;
ob2 = o2;
}
public void showTypes() {
Console.WriteLine("Type of T is " + typeof(T));
Console.WriteLine("Type of V is " + typeof(V));
}
public T getob1() {
return ob1;
}
public V getob2() {
return ob2;
}
}
public class Test {
public static void Main() {
MyGenericClass<int, string> obj = new MyGenericClass<int, string>(1, "string");
obj.showTypes();
int v = obj.getob1();
Console.WriteLine("value: " + v);
string str = obj.getob2();
Console.WriteLine("value: " + str);
}
}
A simple generic class
using System;
class MyGenericClass<T> {
T ob;
public MyGenericClass(T o) {
ob = o;
}
public T getob() {
return ob;
}
public void showType() {
Console.WriteLine("Type of T is " + typeof(T));
}
}
public class Test {
public static void Main() {
MyGenericClass<int> iOb;
iOb = new MyGenericClass<int>(102);
iOb.showType();
int v = iOb.getob();
Console.WriteLine("value: " + v);
MyGenericClass<string> strOb = new MyGenericClass<string>("Generics add power.");
strOb.showType();
string str = strOb.getob();
Console.WriteLine("value: " + str);
}
}
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 - ((MyClass) obj).val;
}
}
class CompareDemo {
public static bool contains<T>(T what, T[] obs) where T : IComparable {
foreach(T v in obs)
if(v.rupareTo(what) == 0)
return true;
return false;
}
public static void Main() {
int[] nums = { 1, 2, 3, 4, 5 };
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.");
}
}
Create relationship between two type parameters
using System;
class A {
}
class B : A {
}
// Here, V must inherit T.
class Gen<T, V> where V : T {
}
class Test {
public static void Main() {
// This declaration is OK because B inherits A.
Gen<A, B> x = new Gen<A, B>();
// This declaration is in error because
// A does not inherit B.
// Gen<B, A> y = new Gen<B, A>();
}
}
Demonstrate the default keyword
using System;
class MyClass {
}
class Test<T> {
public T obj;
public Test() {
obj = default(T);
}
}
class DefaultDemo {
public static void Main() {
Test<MyClass> x = new Test<MyClass>();
if(x.obj == null)
Console.WriteLine("x.obj is null.");
Test<int> y = new Test<int>();
if(y.obj == 0)
Console.WriteLine("y.obj is 0.");
}
}
Generic class with interface
using System;
using System.Collections.Generic;
public interface IShape
{
double Area {
get;
}
}
public class Circle : IShape
{
public Circle( double radius ) {
this.radius = radius;
}
public double Area {
get {
return 3.14 * radius * radius;
}
}
private double radius;
}
public class Rect : IShape
{
public Rect( double width, double height ) {
this.width = width;
this.height = height;
}
public double Area {
get {
return width*height;
}
}
private double width;
private double height;
}
public class Shapes<T>
where T: IShape
{
public double TotalArea {
get {
double acc = 0;
foreach( T shape in shapes ) {
acc += shape.Area;
}
return acc;
}
}
public void Add( T shape ) {
shapes.Add( shape );
}
private List<T> shapes = new List<T>();
}
public class Test
{
static void Main() {
Shapes<IShape> shapes = new Shapes<IShape>();
shapes.Add( new Circle(3) );
shapes.Add( new Rect(7, 5) );
Console.WriteLine( "Total Area: {0}", shapes.TotalArea );
}
}
Generic Fields
using System;
using System.Collections.Generic;
using System.Text;
class MyType<T, U> {
private T _myFirstDataMember;
private U _mySecondDataMember;
public MyType(T val1, U val2) {
this._myFirstDataMember = val1;
this._mySecondDataMember = val2;
}
public T GetFirstDataMember() {
return this._myFirstDataMember;
}
public U GetSecondDataMember() {
return this._mySecondDataMember;
}
}
class MyApp {
static void main(String[] args) {
MyType<string, string> testType = new MyType<string, string>("Val1", "Val2");
Console.WriteLine(testType.GetFirstDataMember());
Console.WriteLine(testType.GetSecondDataMember());
}
}
"This" Reference for Generic Types
using System;
public class Starter{
public static void Main(){
MyGenericClass<int> obj=new MyGenericClass<int>();
obj.MethodA();
}
}
class MyGenericClass<T> {
public T MethodA() {
T var=default(T);
Console.WriteLine(this.GetType().ToString());
return var;
}
}