C# delegate 委托变量的用法

using System;

//C#中的委托类似于C或C++中的函数指针,但两者有本质区别:C或C++不是类型安全的,但C#中的委托是面向对象的,而且是类型安全的。
//从技术角度来讲==>委托是一个引用类型,用来封装带有特定签名和返回类型的方法

namespace qinmi
{
public delegate int mydelegateTest(int i, int j);//声明一个委托
class calculate
{
public static int add(int i, int j)
{
return i + j;
}
public static int minus(int i, int j)
{
return i - j;
}
}

class test
{
static void Main(string[] args)
{
mydelegateTest delegatehe = new mydelegateTest(calculate.add);//声明委托实例,并用calculate.add对其实例化,实际就是将委托和方法连接起来
int he = delegatehe(100, 9);//开始调用委托,就像是使用静态成员方法calculate.add(int i,int j)一样
Console.WriteLine(”方法add的结果:{0}”, he);

mydelegateTest delegatecha = new mydelegateTest(calculate.minus);
int cha = delegatecha(100, 9);
Console.WriteLine(”方法minus的结果:{0}”, cha);
Console.ReadKey();
}
}
}

一点心得:感觉NET比ASP多了点东西,编程思想虽然说是面向对象,不过最基本的还是一个逻辑在里面,多了的一些东西其实也就是把一些东西给封装了起来,调用就更加方便了,列给级别先,从大到小:命名空间->委托->方法->属性…大致就这样

原文:
Rating: 4.0/10 (1 vote cast)

Related posts:

  1. Nintendo Adds Camera to Game Player Nintendo Co. is releasing a new version of its popular...
  2. My New Built PC   CPU: CPU INTEL|C2D E8500 3.16G 775 45N R Board:...
  3. How to block stealing image? Stealing image is theift action, but seems not many people...
  4. Celine Dion San Antonio Concert ...
  5. Another freaking day The Dow Jones Industrial Average rose nearly 170 points at...

Leave a Reply

Preview:

Tags:
Separate individual tags by commas