开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 793|回复: 4
收起左侧

[闲聊] C#委托已经接近崩溃,求个理解方法

[复制链接]
结帖率:95% (84/88)
发表于 2023-5-21 09:47:31 | 显示全部楼层 |阅读模式   美国
快明白怎么回事了,但是这个例子太绕了,绕了3天。
明明就是指针,但是这比指针还难啊。




[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.IO;

namespace Test2
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }
        public void nothing()
        {

            //ADic.Add("w", 2);
            //Console.WriteLine(ADic["w"]);
            //Console.WriteLine(ADic.ContainsKey("w"));
            //Bitmap img = new Bitmap("C:\\ESouceCode\\Eweb-main\\易网页1.0\\photos\\宣传AND图片\\陽.png");
            //pictureBox1.Image = img;
            //string test = "qwerty112233445566";
            //Console.WriteLine(test.Substring(0, 1));
            //Console.WriteLine(test.Replace("3", "ddd"));
            //Console.WriteLine(test);
            //int[] intarray = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
            //IEnumerator theenumerator = intarray.GetEnumerator();
            //while (theenumerator.MoveNext())
            //{
            //    Console.WriteLine(theenumerator.Current);
            //}
            //int[] arr = new int[] { 1, 2, 3, 4, 5 };
            //foreach (int i in arr)
            //{
            //    Console.WriteLine(arr[i - 1]);
            //}
            //List<int> thelist = new List<int>();
            //thelist.Insert(0, 1);
            //thelist.Insert(0, 2);
            //thelist.Insert(0, 3);
            //thelist.Insert(0, 4);
            //thelist.Insert(0, 4);
            //thelist.Insert(0, 4);
            //thelist.Insert(0, 4);
            //thelist.Insert(0, 4);
            //thelist.Insert(0, 5);
            //thelist.RemoveAt(0);
            //throw new Exception("e");
            //List<int> rem = new List<int> { 4, 2 };
            //thelist.RemoveAll(member => rem.Contains(member));
            //thelist.Remove(4);
            //foreach (int i in thelist)
            //{
            //    Console.WriteLine(i);
            //}
            //Console.WriteLine(Math.PI);
            //test t1 = new test();
            //test t2 = new test();
            //t1.SetI(2);
            //t1.PrintI();
            //t2.SetI(9);
            //t2.PrintI();
            //t1.PrintI();
            //int i = 0;
            //ecde(ref i);

            //try
            //{
            //    MessageBox.Show("2");
            //}
            //catch(OverflowException)
            //{
            //    MessageBox.Show("3");
            //}
            //catch (AccessViolationException)
            //{

            //}
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            ProductFactory TheproductFactory = new ProductFactory();
            WarpFactroy ThewarpFactroy = new WarpFactroy();

            Func<Product> fun1 = new Func<Product>(TheproductFactory.MakePizza);
            Func<Product> fun2 = new Func<Product>(TheproductFactory.MakeToyCar);

            Box box1 = ThewarpFactroy.WarpPoroduct(fun1);
            Box box2 = ThewarpFactroy.WarpPoroduct(fun2);

            Console.WriteLine(box1.Product.Name);
            Console.WriteLine(box2.Product.Name);
        }


        class Product
        {
            public string Name { get; set; }
        }
        class Box
        {
            public Product Product { get; set; }
        }
        class WarpFactroy
        {
            public Box WarpPoroduct(Func<Product> getProduct)
            {
                Box box = new Box();
                Product product = getProduct.Invoke();
                box.Product = product;
                return box;
            }
        }
        class ProductFactory
        {
            public Product MakePizza()
            {
                Product product = new Product();
                product.Name = "Pizza";
                return product;
            }
            public Product MakeToyCar()
            {
                Product product = new Product();
                product.Name = "Toy Car";
                return product;
            }
        }
    }
};



有没有什么更简单的例子来理解啊。

结帖率:92% (11/12)

签到天数: 13 天

发表于 2023-5-21 12:21:25 | 显示全部楼层   重庆市重庆市
小明无敌 发表于 2023-5-21 11:52
是不是有点类似于 JavaScript当中 实例和原型这样?

不清楚JavaScript当中 实例和原型
回复 支持 反对

使用道具 举报

结帖率:98% (98/100)

签到天数: 10 天

发表于 2023-5-21 11:52:58 | 显示全部楼层   美国
神女软件定制 发表于 2023-5-21 10:03
Func是一个泛型类型

C#里面的委托是面向对象的,不仅包含了函数指针,还包含对象的指针

是不是有点类似于 JavaScript当中 实例和原型这样?   
回复 支持 反对

使用道具 举报

结帖率:92% (11/12)

签到天数: 13 天

发表于 2023-5-21 10:09:47 | 显示全部楼层   重庆市重庆市
只是你选择的这个例子不太好,写法应该是和设计模式有关的,不用理解这个流程上的写法,简化个委托理解委托就行
回复 支持 反对

使用道具 举报

结帖率:92% (11/12)

签到天数: 13 天

发表于 2023-5-21 10:03:20 | 显示全部楼层   重庆市重庆市
Func<Product>是一个泛型类型

C#里面的委托是面向对象的,不仅包含了函数指针,还包含对象的指针

不用去关心底层实现,理解还算简单的

评分

参与人数 1好评 +1 精币 +3 收起 理由
陽陽陽 + 1 + 3 奉上小小红包希望笑纳

查看全部评分

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 793400750,邮箱:wp@125.la
网站简介:精易论坛成立于2009年,是一个程序设计学习交流技术论坛,隶属于揭阳市揭东区精易科技有限公司所有。
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表