using System;namespace iuhxq
{
class App
{
static string[] s1 = {"a","b","c","d"};
static string[] s2 = {"1","2","3","4"}; private delegate string MyDelegate(string[] s); private static string Concat(string[] s)
{
return String.Concat(s);
} private static string Sum(string[] s)
{
int result = 0;
foreach (string si in s)
{
result += Int32.Parse(si);
} return result.ToString();
} private static void Run(MyDelegate del, string[] s)
{
Console.WriteLine(del(s));
} public static void Main()
{
Run(new MyDelegate(Concat), s1);
Run(new MyDelegate(Sum), s2);
}
};
}
{
class App
{
static string[] s1 = {"a","b","c","d"};
static string[] s2 = {"1","2","3","4"}; private delegate string MyDelegate(string[] s); private static string Concat(string[] s)
{
return String.Concat(s);
} private static string Sum(string[] s)
{
int result = 0;
foreach (string si in s)
{
result += Int32.Parse(si);
} return result.ToString();
} private static void Run(MyDelegate del, string[] s)
{
Console.WriteLine(del(s));
} public static void Main()
{
Run(new MyDelegate(Concat), s1);
Run(new MyDelegate(Sum), s2);
}
};
}
解决方案 »
- 无效的 SQL语句;期待 'DELETE'、'INSERT'、'PROCEDURE'、'SELECT'、或 'UPDATE'。
- 如何将保存RGB值的数组转化成一张BMP图像并且显示
- 【求助】winform下控件DrawToBitmap得到图像保存到MemoryStream时出错
- 请求这样的委托为什么得不到结果呢,结果总是空的?急!
- C#引用了EXCELCOM组件,为什么在USING EXCEL的时候提示找不到命名空间?
- 送分!
- 请问编译 Dxperience 8.2.2源代码步骤是怎么样的?
- C#serialport。datareceived事件无法正常触发
- 关于枚举类型的问题
- 一个不懂的问题,希望大哥能帮帮忙
- 问个关于读取网页保存到本地的问题。。。请高手来指点下
- WinFrom里怎么样在打开新窗体的同时关闭自己的父窗体,高手指点下。。
{
class App
{
static string[] s1 = {"a","b","c","d"};
static string[] s2 = {"1","2","3","4"}; private delegate string MyDelegate(string[] s); private static string Concat(string[] s)
{
return String.Concat(s);
} private static string Sum(string[] s)
{
int result = 0;
foreach (string si in s)
{
result += Int32.Parse(si);
} return result.ToString();
} private static void Run(MyDelegate del, string[] s)
{
Console.WriteLine(del(s));
} public static void Main()
{
Run(new MyDelegate(Concat), s1);
Run(new MyDelegate(Sum), s2);
Run(new MyDelegate(Sum)+new MyDelegate(Concat), s2);//这条为什么不是输出101234?
}
};
}
using System;delegate void MyDelegate(string s);class MyClass
{
public static void Hello(string s)
{
Console.WriteLine(" Hello, {0}!", s);
} public static void Goodbye(string s)
{
Console.WriteLine(" Goodbye, {0}!", s);
} public static void Main()
{
MyDelegate a, b, c, d; // Create the delegate object a that references
// the method Hello:
a = new MyDelegate(Hello);
// Create the delegate object b that references
// the method Goodbye:
b = new MyDelegate(Goodbye);
// The two delegates, a and b, are composed to form c:
c = a + b;
// Remove a from the composed delegate, leaving d,
// which calls only the method Goodbye:
d = c - a; Console.WriteLine("Invoking delegate a:");
a("A");
Console.WriteLine("Invoking delegate b:");
b("B");
Console.WriteLine("Invoking delegate c:");
c("C");
Console.WriteLine("Invoking delegate d:");
d("D");
}
}
跟这个有什么区别吗?
委托是C#里的一种新模式,可以对应C/C++里的函数指针.与函数指针的不同如下:
1. delegate是类型安全的,也就是不能把签名(C里面叫原型)不一样的方法(C里面叫函数)添加到delegate中.
2. delegate内部有一个previous引用,也就是说它们可以组成一个链表.所以delegate可以一次调用多个类型一致的方法.
从字面上理解,可以把一个delegate理解成一些方法的委托人,你可以通过他调用这些方法.:)
private static void Run(MyDelegate del, string[] s)第一个参数的类型是MyDelegate,即一个委托方法类型,那么当你
Run(new MyDelegate(Sum)+new MyDelegate(Concat), s2);
进行调用的时候,你的意思是想分别用两个委托方法处理s2,然后再进行字符串合并;
但是编译器并不是按照你所想的运行,首先对第一个参数进行处理,如下
new MyDelegate(Sum)+new MyDelegate(Concat)
即要匹配MyDelegate类型,那么以上的参数值匹配成到函数中,真正起作用的是“new MyDelegate(Concat)”这部分,那么就相当于
Run( new MyDelegate(Concat), s2);
效果。
d = c - a;这些只是delegate重载了+/-操作符,和重载的+=操作符道理一样.delegate在内部还是要调用类似Delegate Add(Delegate d);这样的方法.因为只有基础类型像int,float 才配备操作符的IL指令.
this.Load += new System.EventHandler(this.Page_Load); private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}this.Button1.Click += new System.EventHandler(this.Button1_Click); private void Button1_Click(object sender, System.EventArgs e)
{ }