匿名方法
匿名方法(Anonymous methods)提供了一种传递代码块作为委托参数的技术。匿名方法是没有名称只有主体的方法 。
在匿名方法中不需要指定返回类型,它是从方法主体内的return语句推断的。
匿名方法语法:
delegate (参数) {方法体};
匿名方法特点:
1.没有方法名(由系统默认名字)
2.只能在定义的时候调用,其他地方无法调用
匿名方法用处:
当你需要一个临时方法,该方法使用次数极少或者你需要方法代码很简短。
匿名方法使用注意:
1.匿名方法不能访问外部方法的ref和out参数
2.匿名方法内的局部变量不可以与外部方法的局部变量是重名,否则编译会报重定义错误。
3.匿名方法可以访问外部方法定义的局部变量。
4.只能用作委托方法绑定。
代码示例:
class Program
{
public delegate void Del(); //创建委托
static void Main(string[] args)
{
Del F = delegate() //实例=delegate
{
Console.WriteLine("小鸡炖蘑菇");
};
F(); //直接调用
FF1();
Console.ReadKey();
}
static void FF1() //直接调用
{
Console.WriteLine("天王盖地虎");
}
}
----------------------------------------------------------------->
小鸡炖蘑菇
天王盖地虎
委托可以通过匿名方法调用,也可以通过命名方法调用,即通过向委托对象传递方法参数。
class Program
{
public delegate void Del(string a);//声明委托
static string b = "qwe";//变量
static void Main(string[] args)
{
Del del = delegate (string c) //使用匿名方法创建委托实例
{
Console.WriteLine(c);
};
del("小鸡炖蘑菇");
Console.ReadKey();
}
static void FF1() //静态方法
{
Console.WriteLine(b+"天王盖地虎"); //输出
}
}
--------------------------------------------------------------------
del = new Del(FF1); //使用命名方法实例化委托
del("宝塔镇河妖");
Console.ReadKey();
}
static void FF1(string n) //静态方法
{
Console.WriteLine(b+"天王盖地虎"); //输出
}
----------------------------------------------------------------------
Lambda表达式
Lambda表达式就是匿名方法演变过来的,是一种可用于创建委托的匿名函数。
语法格式:
=>"转到"
Lambda与匿名方法的一般规则如下:
1.参数数量必须与委托类型的参数数量相同;
2.每个输入的参数必须都能够隐式转换为委托的参数类型;
3.返回值(如果有)必须能够隐式转换为委托的返回类型。
代码示例
无返回值无参数
class Program
{
public delegate void DEl();
static void Main(string[] args)
{
DEl dEl;
dEl = () => { Console.WriteLine("一二三四五"); }; //Lambda表达式
//dEl = () => Console.WriteLine("上山打老虎"); //方法体只有一行时可简化
dEl();
Console.ReadKey();
}
}
无返回值含参数
class Program
{
public delegate void DEl(int a,string b);
static void Main(string[] args)
{
DEl dEl;
dEl = (int a, string b) => { Console.WriteLine("一二三四五"); };
//dEl = (int a, string b) => Console.WriteLine("上山打老虎"); //简化1
//dEl = (a, b) => Console.WriteLine("老虎没打着"); //简化2
dEl(1,"w");
Console.ReadKey();
}
}
有返回值含一个参数,方法体只有return语句
class Program
{
public delegate int DEl(string b);
static void Main(string[] args)
{
DEl dEl;
dEl = (string a) => { return 1; };
//dEl = (s) => { return 1; }; //简化1
//dEl = a => 1; //简化2
dEl("www");
Console.ReadKey();
}
}
有返回值含多个参数,方法体自由return语句
class Program
{
public delegate int DEl(string b,int a);
static void Main(string[] args)
{
DEl dEl;
dEl = (string a, int b) => { return 1; };
dEl = (a, b) => { return 1; };
dEl = (a,b) => 1; //简化1
dEl("qwe",1); //简化2
Console.ReadKey();
}
}
有返回值含多个参数,方法体有多行语句
class Program
{
public delegate int DEl(string b,int a);
static void Main(string[] args)
{
DEl dEl;
dEl = (string a, int b) => { Console.WriteLine("一二三四五");return 1; };
// dEl = (a, b) => { Console.WriteLine("一二三四五");return 1; }; //简化1
dEl("qwe",1);
Console.ReadKey();
}
}
扩展方法
扩展方法能够向现有类型“添加”方法,而无需创建新的派生类型,重新编译或以其他方式修改原始类型。
语法要求:
- 此方法必须是一个静态方法
- 此方法必须放在静态类中
- 此方法的第一个参数必须以this开头,并且制定此方法是扩展自哪个类
扩展方法的特点
- 扩展方法扩展自哪个类,就必须是此类型的变量来使用,其他类型无法使用;
- 拓展方法中的this后面的参数不属于方法的参数;
- 如果扩展方法和实例方法具有相同的签名,则优先调用实例方法;
- 扩展自父类上的方法,可以被子类的对象直接使用;
- 扩展方法最终还是被编译器议成:静态类,静态方法();
class Program
{
static void Main(string[] args)
{
P1 p1 = new P1();
p1.Sc();
Console.ReadKey();
}
}
class P1
{
}
static class P2 //静态类
{
public static void Sc(this P1 p1) //静态方法
{
Console.WriteLine("好好");
}
}
class Program
{
static void Main(string[] args)
{
string a = "qwe";
a.Sc("asd");
Console.ReadKey();
}
}
static class P1
{
public static void Sc(this string a,string b)
{
int c = b.Length;
Console.WriteLine(c);
}
}
--------------------------------------------------------
返回3