01.我们写泛型代码的时候经常有大量的"<“和”>“符号,这样有时候代码一多,也难免会让开发者在阅读代码过程中会觉得有点晕的,此时我们觉得晕的时候肯定就会这样想:是不是能够省掉一些”<" 和">"符号的呢?你有这种需求了,类型推断,意味着编译器会在调用一个泛型方法时自动判断要使用的类型
class Program
{
static void Main(string[] args)
{
int a = 100;
int b = 200;
// 此时编译器可以根据传递的实参 1和2来判断应该使用Int类型实参来调用泛型方法
// 可以看出有了类型推断之后少了<>,这样代码多的时候可以增强可读性
Method(ref a,ref b);
WriteLine(a);//200
WriteLine(b);//100
ReadKey();
}
static void Method<T>(ref T t1,ref T t2)
{
T temp = t1;
t1 = t2;
t2 = temp;
}
}
02泛型约束
类型约束就是用where 关键字来限制能指定类型实参的类型数量
C#编译器会提示错误信息:“T”不包含“CompareTo”的定义,并且找不到可接受类型为“T”的第一个参数的扩展方法“CompareTo”。 这是因为此时类型参数T可以为任意类型,然而许多类型都没有提供CompareTo方法
指定一个类型约束,让C#编译器知道这个类型参数一定会有CompareTo方法的,这样编译器就不会报错了,我们可以将上面代码改为(代码中T:IComparable为类型参数T指定的类型实参都必须实现泛型IComparable接口):
class MaxT<T> where T:IComparable<T>
{
static T Max(T t1,T t2)
{
if (t1.CompareTo(t2)>0)
{
return t1;
}
return t2;
}
}
C# 中有4种约束
引用类型约束
表示形式为 T:class
static void Main(string[] args)
{
Test1 test1=new Test1();
MyClass<Test1> myClass=new MyClass<Test1>();
myClass.Sum(test1);
}
public class Test
{
}
public class Test1:Test
{
public Test1()
{
}
}
class MyClass<T> where T:Test //必须继承基类
{
public void Sum(T t)
{
WriteLine(t.GetType());
}
}
class Program
{
static void Main(string[] args)
{
Stack<string> stack=new Stack<string>();
Myclass<float> myclass=new Myclass<float>(10);
WriteLine(myclass.First); //10
WriteLine(myclass.Seconf); //0
ReadKey();
}
}
class Compara<T> where T:struct
{
public T t;
public void Sum(T t)
{
this.t = t;
WriteLine(t);
}
}
interface IPair<T> where T:struct
{
T First { get; set; }
T Seconf { get; set; }
}
class Myclass<T>:IPair<T> where T : struct
{
public T First { get; set; }
public T Seconf { get; set; }
//初始化
public Myclass(T first)
{
this.First = first;
//T默认值
this.Seconf = default(T);
}
}
class Program
{
static void Main(string[] args)
{
Myclass<int,double> myclass=new Myclass<int, double>(10,500d);
WriteLine();
}
}
interface IPair<TFirst,TSecond>
where TFirst:struct
where TSecond:struct
{
TFirst First { get; set; }
TSecond Second { get; set;}
}
public class Myclass<TFirst, TSecond>:IPair<TFirst,TSecond> where TFirst : struct where TSecond : struct
{
public TFirst First { get; set; }
public TSecond Second { get; set; }
public Myclass(TFirst First, TSecond Second)
{
this.First = First;
this.Second = Second;
}
}
泛型方法:
public static class MathEx
{
public static T Max<T>(T first,params T[] values) where T :IComparable<T>
{
T maxitem = first;
foreach (var item in values)
{
if (item.CompareTo(maxitem)>0)
{
maxitem = item;
}
}
return maxitem;
}
public static T Min<T>(T first,params T[] values) where T:IComparable<T>
{
T minitem = first;
foreach (var item in values)
{
if (item.CompareTo(minitem)<0)
{
minitem=item;
}
}
return minitem;
}
}
class Program
{
static void Main(string[] args)
{
WriteLine(MathEx.Max<int>(10, 50, 200));
WriteLine(MathEx.Min<float>(10.2f,50f,2.2f));
ReadKey();
}
}
class Program
{
static void Main(string[] args)
{
//类型推断
WriteLine(MathEx.Max(10, 50, 200));
WriteLine(MathEx.Min(10.2f,50f,2.2f));
ReadKey();
}
}