C/C++函数指针、C#委托是什么?

函数指针

#include<stdio.h>

//声明函数指针
typedef int(*Calc)(int a, int b);
int Add(int a, int b)
{
	return a + b;
}
int Sub(int a, int b) {
	return a - b;
}

int main() {
	Calc funcPoint1 = &Add;
	Calc funcPoint2 = &Sub;
	
	int x = 120;
	int y = 140;
	int z = 0;
	z = Add(x, y);
	z = funcPoint1(x, y);
	printf("%d+%d=%d\n", x, y, z);

	z = Sub(x, y);
	z = funcPoint2(x, y);
	printf("%d-%d=%d\n", x, y, z);
	system("pause");


}

一切皆地址
变量(数据):是以某个地址为起点中的一段内存中所存储的值;
函数(算法):是以函数名为地址起点的一段内存中所存储的一组机器语言指令;

C#中委托是什么?

委托(delegate)是函数指针的‘升级版’;

委托的简单使用
  • Action委托
  • Func委托
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FunctionPointerExampleCsharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Calculator ca = new Calculator();
            Action action = new Action(ca.Report);
            ca.Report();
            action.Invoke();
            action();

            Func<int, int, int> func = new Func<int, int, int>(ca.Add);
            Func<int, int, int> func2 = new Func<int, int, int>(ca.Sub);

            int result=func.Invoke(12, 34);
            Console.WriteLine(result);
            result=func2.Invoke(123, 34);
            Console.WriteLine(result);
            result =func(12, 34);
            Console.WriteLine(result);
            result =func2(123, 34);
            Console.WriteLine(result);


        }
    }
    class Calculator
    {
        public void Report()
        {
            Console.WriteLine("Hello,Tom!");
        }
        public int Add(int a, int b)
        {
            return a + b;
        }
        public int Sub(int a, int b)
        {
            return a - b;
        }
    }
}

相关推荐

  1. C/C++函数指针C#委托什么

    2024-06-15 10:34:02       36 阅读
  2. C语言什么指向函数指针

    2024-06-15 10:34:02       42 阅读
  3. 事件委托什么

    2024-06-15 10:34:02       29 阅读
  4. C#面:委托什么?事件一种委托

    2024-06-15 10:34:02       39 阅读
  5. C#中的委托什么?事件一种委托

    2024-06-15 10:34:02       99 阅读
  6. C#面:阐述什么泛型委托

    2024-06-15 10:34:02       32 阅读
  7. 什么函数指针?如何定义和使用函数指针

    2024-06-15 10:34:02       38 阅读
  8. 什么CI/CD流水线

    2024-06-15 10:34:02       28 阅读
  9. C语言什么悬空指针

    2024-06-15 10:34:02       59 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-15 10:34:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-15 10:34:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-15 10:34:02       82 阅读
  4. Python语言-面向对象

    2024-06-15 10:34:02       91 阅读

热门阅读

  1. 富格林:力争打破黑幕安全盈利

    2024-06-15 10:34:02       29 阅读
  2. Leetcode(top100)最长连续序列

    2024-06-15 10:34:02       34 阅读
  3. 微服务与分布式面试题

    2024-06-15 10:34:02       32 阅读
  4. B树与B+树与Mysql innodb的B+树和其相关索引

    2024-06-15 10:34:02       24 阅读
  5. 【AI开发】LangGraph基础

    2024-06-15 10:34:02       35 阅读
  6. mmyolo尝试

    2024-06-15 10:34:02       25 阅读
  7. linux shell实现端口查询

    2024-06-15 10:34:02       22 阅读
  8. std::vector的emplace_back 与 push_back 比较

    2024-06-15 10:34:02       42 阅读
  9. 数据结构 ->反转链表

    2024-06-15 10:34:02       33 阅读
  10. 程序员该有怎么样的职业素养

    2024-06-15 10:34:02       30 阅读
  11. 高等数学与初等数学的分水岭是什么?

    2024-06-15 10:34:02       25 阅读