C#泛型是我学Java和C++这些静态语言中,最强大的没有之一。当然比起动态语言Python,PHP,Javascript就差远了。
1, delegate的泛型。Action<in TDataType> 通用代理方法原型void function ( TDatatype input);
public delegate void Action<in T>(
T obj
)
T obj
)
以前我们使用delegate的时候。过程如下:
//声明
delegate void DisplayMessage(string message);
private static void ShowWindowsMessage(string message)
{
MessageBox.Show(message);
}
public static void Main()
{
//定义
DisplayMessage messageTarget;
//赋值
if (Environment.GetCommandLineArgs().Length > 1)
messageTarget = ShowWindowsMessage;
else
messageTarget = Console.WriteLine;
//运行
messageTarget("Hello, World!");
}
delegate void DisplayMessage(string message);
private static void ShowWindowsMessage(string message)
{
MessageBox.Show(message);
}
public static void Main()
{
//定义
DisplayMessage messageTarget;
//赋值
if (Environment.GetCommandLineArgs().Length > 1)
messageTarget = ShowWindowsMessage;
else
messageTarget = Console.WriteLine;
//运行
messageTarget("Hello, World!");
}
使用Action<in T>( T obj)之后,就去掉了声明Delegate方法的步骤:
private static void ShowWindowsMessage(string message)
{
MessageBox.Show(message);
}
public static void Main()
{
//定义
Action<String> messageTarget;
//赋值
if (Environment.GetCommandLineArgs().Length > 1)
messageTarget = ShowWindowsMessage;
else
messageTarget = Console.WriteLine;
//运行
messageTarget("Hello, World!");
}
{
MessageBox.Show(message);
}
public static void Main()
{
//定义
Action<String> messageTarget;
//赋值
if (Environment.GetCommandLineArgs().Length > 1)
messageTarget = ShowWindowsMessage;
else
messageTarget = Console.WriteLine;
//运行
messageTarget("Hello, World!");
}
但是如果我的方法有多个参数怎么办?别担心,微软为我们写了T1-T16参数的Action的东东,膜拜中。
2, Fun<TDataType, out TResult> 有返回值的方法泛型, 方法的原型是 TResult function(TDataType input)
public delegate TResult Func<in T, out TResult>(
T arg
)
T arg
)
以前我们使用delegate的时候。过程如下:
//声明
delegate string ConvertMethod(string inString);
private static string UppercaseString(string inputString)
{
return inputString.ToUpper();
}
public static void Main()
{
//定义和赋值
ConvertMethod convertMeth = UppercaseString;
string name = "Dakota";
//应用
Console.WriteLine(convertMeth(name));
}
delegate string ConvertMethod(string inString);
private static string UppercaseString(string inputString)
{
return inputString.ToUpper();
}
public static void Main()
{
//定义和赋值
ConvertMethod convertMeth = UppercaseString;
string name = "Dakota";
//应用
Console.WriteLine(convertMeth(name));
}
使用Fun<T,TResult>( T obj)之后,就去掉了声明Delegate方法的步骤:
private static string UppercaseString(string inputString)
{
return inputString.ToUpper();
}
public static void Main()
{
//定义和赋值
Fun<String,String> convertMeth = UppercaseString;
//匿名方法:
// convertMeth = delegate(string s){ return s.ToUpper();};
//Lambda表达式,更多参考:http://msdn.microsoft.com/zh-cn/library/bb397687.aspx
//convertMeth = s =>{ return s.ToUpper();};
string name = "Dakota";
//应用
Console.WriteLine(convertMeth(name));
}
{
return inputString.ToUpper();
}
public static void Main()
{
//定义和赋值
Fun<String,String> convertMeth = UppercaseString;
//匿名方法:
// convertMeth = delegate(string s){ return s.ToUpper();};
//Lambda表达式,更多参考:http://msdn.microsoft.com/zh-cn/library/bb397687.aspx
//convertMeth = s =>{ return s.ToUpper();};
string name = "Dakota";
//应用
Console.WriteLine(convertMeth(name));
}