是什么在C#和函数指针在C ++中的代表有什么区别? [英] What is the difference between delegate in c# and function pointer in c++?

查看:242
本文介绍了是什么在C#和函数指针在C ++中的代表有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
是否有函数指针在C#?

我M有意寻找在C#委托之间的差异, 在C ++函数指针

I'm interested in finding the difference between delegate in C# and function pointer in C++.

推荐答案

在C#中的委托是一个内置的迭代器类型安全的函数指针。

A delegate in C# is a type-safe function pointer with a built in iterator.

它保证指向与指定的签名有效的函数(不像C,其中的指针可以转换为指向谁知道是什么)。它还支持通过多种约束功能迭代的概念。

It's guaranteed to point to a valid function with the specified signature (unlike C where pointers can be cast to point to who knows what). It also supports the concept of iterating through multiple bound functions.

在C#中,代表们的多播的意思是他们可以通过多种功能迭代。例如:

In C#, delegates are multi-cast meaning they can iterate through multiple functions. For example:

class Program
{
   delegate void Foo();

   static void Main(string[] args)
   {
      Foo myDelegate = One;
      myDelegate += Two;

      myDelegate(); // Will call One then Two
   }

   static void One()
   {
      Console.WriteLine("In one..");
   }

   static void Two()
   {
      Console.WriteLine("In two..");
   }
}

这篇关于是什么在C#和函数指针在C ++中的代表有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆