委托是这样的吗

hhhglj_hhhglj 2008-07-04 03:55:10
public class a1
{ public delegata void del(string ss);
public void hhhglj()
{ messagebox.show("dddddddddddddd");
}
}
del ss=new del(hhhglj);
ss(textbox1.text);

这就是所谓的委托吗?其中实际的好处是什么呢?,我实在不清楚\不明白
...全文
163 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
sanweizhao111 2008-07-06
  • 打赏
  • 举报
回复
委托是用来实现一个线程去操作另外一个线程中的控件的,这是因为C#中不允许一个线程去直接对另外的线程中的控件进行操作,这是不安全的,它会引起异常.
委托是一个用来解决这种问题的方法,也可以使用BackGroundWork
Magicwords 2008-07-06
  • 打赏
  • 举报
回复
在异步中很好用
oktell 2008-07-04
  • 打赏
  • 举报
回复
委托,委托,不大好理解。

今天记住一句话:把函数当参数。
maddemon 2008-07-04
  • 打赏
  • 举报
回复
委托 顾名思意就是 某种方法的委托(代理)中心
比较生动的讲解请看http://www.cnblogs.com/JimmyZhang/archive/2007/09/23/903360.html
w4585 2008-07-04
  • 打赏
  • 举报
回复
关注下 ~~
Jueyoung 2008-07-04
  • 打赏
  • 举报
回复
FYI


using System;
using System.Collections.Generic;
using System.Text;

namespace Delegate_01
{ //the delegate defined here has none arguments

class Currency
{
public uint Dollars;
public ushort Cents;

/// <summary>
/// constructor function
/// </summary>
/// <param name="dollars"></param>
/// <param name="cents"></param>
public Currency(uint dollars, ushort cents)
{
if (cents >99)
{
dollars += (uint)(cents / 100);
cents = (ushort)(cents % 100);
}
this.Dollars = dollars;
this.Cents = cents;
}

/// <summary>
/// override ToString() function
/// </summary>
/// <returns></returns>
public override string ToString()
{
return string.Format("{0} Dollars {1} Cents", this.Dollars, this.Cents);
}

/// <summary>
/// add a static function with the same signature as Currency
/// </summary>
/// <returns></returns>
public static string GetCurrencyUnit()
{
return "Cognizant.com";
}
}

class Program
{
// define a delegate with none arguments
private delegate string GetAString();

static void Main(string[] args)
{
int x = 100;
GetAString astr = new GetAString(x.ToString); // int.ToString();
Console.WriteLine("x string is \"{0}\"", astr());
Currency balance = new Currency(13,153);
astr = new GetAString(balance.ToString); // the parameter should be the method's name only
// the ToString() function here was override in the class Currency
Console.WriteLine("balabce string is \"{0}\"", astr ());
astr = new GetAString(Currency.GetCurrencyUnit); // the parameter should be the method's name only
Console.WriteLine("GetAString : \"{0}\"", astr());

Console.ReadKey();
}
}
}



刚好这两天在学习委托。

委托要注意的地方就是
astr = new GetAString(balance.ToString)
balance.ToString 是一个 返回类型是 string 无参数的函数, 和 定义 delegate 的签名要一样! 
private delegate string GetAString();
bagegejin 2008-07-04
  • 打赏
  • 举报
回复
其实可以这样理解,委托可以看作是方法的集合,该方法带有相同的参数列表

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication7
{
class Program
{
public delegate void Del(string ss);

public static void Show(string ss)
{
Console.WriteLine(ss);
}

public static void ShowLZ(string ss)
{
Console.WriteLine("楼主,{0}", ss);
}

static void Main(string[] args)
{
Del del1 = Show;
del1("其实我的真正身份是Show,是del1装B装的");
Del del2 = ShowLZ;
del2("给分给我啊");
Console.ReadKey();
}
}
}

dcfeng 2008-07-04
  • 打赏
  • 举报
回复
委托还有一个重要的应用是在 事件 上,通过委托处理各种事件
事件引发---〉对应的handller(委托)---〉执行委托的方法
我姓区不姓区 2008-07-04
  • 打赏
  • 举报
回复
你这个写的有问题吧,del是一个带一个参数的委托,但hhhglj是一个无参方法,ss能实例化成功?
眼里进了砂 2008-07-04
  • 打赏
  • 举报
回复
如上面所说,委托可以将方法当作参数来传递,尤其是当把参数设定为委托时,更加方便灵活。 况且委托也可以和事件结合
gomoku 2008-07-04
  • 打赏
  • 举报
回复
平常我们能传递字符串,数组等等。
而委托使得我们能把函数传来传去。

using System;

public delegate void Del(string ss);
public class A1
{
public Del StringDeleted;
public DeleteString()
{
if( StringDeleted != null )
{
StringDeleted("Someone has deleted my string");
}
}
}
public class Program
{
static void Main()
{
A1 a = new A1();
a.DeleteString();
Console.ReadLine();

a.StringDeleted = OnStringDelete; //<--- 传递一个函数,以便得到通知。
a.DeleteString(); //Log: Someone has deleted my string
Console.ReadLine();
}

static void OnStringDelete(string ss)
{
Console.WriteLine("Log: " + ss );
}
}
松花皮蛋 2008-07-04
  • 打赏
  • 举报
回复
委托可以替代C++的函数指针,而且还可以用于回调函数.还是很有用处的,实例委托时候就传入具体的方法参数
是你写的这个del ss=new del(hhhglj);
fuadam 2008-07-04
  • 打赏
  • 举报
回复
能把方法当参数传递,使程序更抽象

110,010

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧