委托,跟类很相似,能够定义对象,但是区别是

1,委托必须有关键字delegate。

2,委托有类型修饰符,比如void,string,int。修饰符取决于他的方法返回类型。

3,委托没有方法实现。

 delegate void printFunction();

委托的作用是给委托对象添加几个方法,在使用委托的时候同时调用使用几个方法。

每次委托调用,他的委托的方法都会实现。

调用委托的方式:

委托名();

给委托添加方法的方法有:

1,通过实例调用方法添加。例如:

test t = new test();
printFunction p = new printFunction(t.print1);

2,通过静态方法调用方法添加。例如:

test t = new test();
printFunction p = new printFunction(test.print1);

但是print1必须是静态方法。

3,赋值委托:不用new 委托了。

 test t = new test();
printFunction p =t.print1; 或者
test t = new test();
printFunction p =test.print1;

3,组合委托:相当于把第一个委托的方法和第二个委托的方法一起加给第三个委托。

test t = new test();
printFunction p = new printFunction(t.print1);
printFunction p2 = new printFunction(t.print2);
printFunction p3 = p + p2;
p3();

4,利用+=,和—=来添加和减少方法。

test t = new test();
printFunction p = new printFunction(t.print1);
p += t.print2;

这种方法的实质其实就是创建了一个新委托,相当于左边的委托加上右边方法的组合委托。

实例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication4
{
delegate void printFunction();
class test
{
public void print1()
{
Console.WriteLine("第一个打印");
}
public void print2()
{
Console.WriteLine("第二个打印");
}
}
class Program
{
static void Main(string[] args)
{
test t = new test();
printFunction p = new printFunction(t.print1);
p += t.print2;
p += t.print1;
p += t.print2;
p();
Console.WriteLine(".....................");
printFunction p2 = new printFunction(t.print2);
printFunction p3 = p + p2;
p3();
Console.ReadLine();
}
}
}

结果:

带有返回值的委托

:如果委托有返回值 ,并且调用列表中有一个以上的方法会发生以下情况:

1,调用列表最后一个方法的返回值就是委托调用返回的值。

2,调用列表中除了最后一个方法,其他方法的返回值都会被忽略。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication4
{
delegate int printFunction();
class test
{
int val = ;
public int print1()
{
val += ;
return val;
}
public int print2()
{
val += ;
return val;
}
}
class Program
{
static void Main(string[] args)
{
test t = new test();
printFunction p =t.print1;
p += t.print2;
p += t.print1;
Console.WriteLine("value is {0}",p()); Console.ReadLine();
}
}
}

结果:

调用带引用参数的委托。

如果委托带有引用参数。参数值会跟着调用列表中的一个或多个方法的返回值 而改变。

在调用委托列表的下一个方法时,参数的新值会传递给下一个方法。

注意

1,委托和调用的方法的参数是一样的。

2,传值是在调用委托的时候才通过委托传递给方法过去的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication4
{
delegate void printFunction(ref int x);
class Program
{
public void add1(ref int x)
{ x += ; }
public void add2(ref int x)
{ x += ; }
static void Main(string[] args)
{
Program pm = new Program();
printFunction p = pm.add1;
p += pm.add2;
p += pm.add1; int x = ; p(ref x); //这里才真正传值,把x传递过去,前面只是给定义,并且通过调用方法改变x的值。 Console.WriteLine("{0}",x);
Console.ReadLine();
}
}
}

运行结果:

12.

委托--delegate的更多相关文章

  1. [.NET] C# 知识回顾 - 委托 delegate (续)

    C# 知识回顾 - 委托 delegate (续) [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/6046171.html 序 上篇<C# 知识回 ...

  2. [C#] C# 知识回顾 - 委托 delegate

    C# 知识回顾 - 委托 delegate [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/6031892.html 目录 What's 委托 委托的属性 ...

  3. C# 委托Delegate(一) 基础介绍&用法

    本文是根据书本&网络 前人总结的. 1. 前言 定义&介绍: 委托Delegate是一个类,定义了方法的类型, 使得可以将方法当做另一个方法的参数来进行传递,这种将方法动态地赋给参数的 ...

  4. 为什么不能把委托(delegate)放在一个接口(interface)当中?

    stackoverflow上有人问,为什么不能把委托放在一个接口当中? 投票最多的第一个答案第一句话说,“A Delegate is just another type, so you don't g ...

  5. C# 代理/委托 Delegate

    本文转载自努力,努力,努力 1. 委托的定义:委托是函数的封装,它代表一"类"函数.他们都符合一定的签名:拥有相同的参数列表,返回值类型.同时,委托也可以看成是对函数的抽象,是函数 ...

  6. c# 委托 delegate

    委托是一种存储函数引用的类型,在事件和事件的处理时有重要的用途 通俗的说,委托是一个可以引用方法的类型,当创建一个委托,也就创建一个引用方法的变量,进而就可以调用那个方法,即委托可以调用它所指的方法. ...

  7. 理解委托(delegate)及为什么要使用委托

    理解委托(delegate)及为什么要使用委托 委托:是一种定义方法签名的类型. 当实例化委托时,您可以将其实例与任何具有兼容签名的方法相关联. 您可以通过委托实例调用方法. 上述为官方说法,理解起来 ...

  8. 深入理解委托(Delegate)

    前言 委托其实一直以来都感觉自己应该挺熟悉的,直到最近又去翻了翻 CLR via C#,感觉我之前的理解可能还有失偏颇.在这记录一下. 之前文章的链接: 接口和委托的泛型可变性 C#高级编程笔记 De ...

  9. C# -- 使用委托 delegate 执行异步操作

    C# -- 使用委托 delegate 执行异步操作 委托是一种安全地封装方法的类型,它与 C 和 C++ 中的函数指针类似. 与 C 中的函数指针不同,委托是面向对象的.类型安全的和保险的. 委托的 ...

  10. 委托delegate

    委托delegate没有函数体.委托可以指向函数(要与指向的函数格式.类型相一致) namespace demo { public delegate double MyDelegate(double ...

随机推荐

  1. 【翻译】Netscaler真实表现性能调整

    源地址:https://msandbu.wordpress.com/2014/10/31/netscaler-and-real-performance-tuning/ 作者显然不是以英语为母语的,所以 ...

  2. gulp小记(无刷新重载样式)

    之前在使用sass的时候,使用了一个不错的工具koala,其实它的原理就是监视sass文件的变化,去编译css而gulp也能为我们做这样的事并且更多 使用gulp之前我们要做一些准备工作 1)安装no ...

  3. EntityFramework嵌套查询的五种方法

    这样的双where的语句应该怎么写呢: var test=MyList.Where(a => a.Flows.Where(b => b.CurrentUser == “”) 下面我就说说这 ...

  4. css实现垂直居中的方法

    1,设置其line-height值,使之与其高度相同 2,设置table结构,用vertical-align:middle; 3,应用定位,父级别:position:relative:子级:posit ...

  5. 自定义SharePoint 2013 元数据选择控件

    元数据在Sharepoint中是一个很常用的功能,他提供一个方法来管理企业中常用的关键词,可以更加统一的使用和更新.默认情况下,绑定在列表或库中的元数据字段可以设置是否允许为多个值.但是无法控制在弹出 ...

  6. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q81-Q84)

    Question 81You need to create a Web Part that creates a copy of the out-of-the-box Contribute permis ...

  7. SharedPreference.Editor的apply和commit方法异同

    这两个方法的区别在于: 1. apply没有返回值而commit返回boolean表明修改是否提交成功 2. apply是将修改数据原子提交到内存, 而后异步真正提交到硬件磁盘, 而commit是同步 ...

  8. ubuntu解决libstdc++.so.6: cannot open shared object file: No such file or directory:问题

    解决libstdc++.so.6: cannot open shared object file: No such file or directory:原因在于,在13.10 版本中,ia32_lib ...

  9. 【读书笔记】iOS-NSString的length

    NSString的length方法能够准确无误地处理国际字符串,如含有俄文,中文或者日本文字符的字符串,以及使用Unicode国际字符标准的字符串.在C语言中处理这些国际字符串是件令人非常头疼的事情 ...

  10. 【原/转】UITableview性能优化总结

    UITableView作为ios中使用最频繁的控件之一,其性能优化也是常常要面对的,尤其是当数据量偏大并且设备性能不足时.本文旨在总结tableview的几个性能优化tips,并且随着认识的深入,本文 ...