开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

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


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

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

查看: 599|回复: 1
收起左侧

[C#图文教程] 易转C# - 3

[复制链接]
结帖率:95% (84/88)
发表于 2023-5-28 02:06:41 | 显示全部楼层 |阅读模式   美国
本帖最后由 陽陽陽 于 2023-5-28 02:07 编辑

杂类:


快速命名方法:

光标点到方法那里,然后连续按两次Ctrl + R


正文:

1:多播(multicast)委托:


可以理解成把好几个子程序封装成一个,参数返回值必须一致
可以通过class实现不同参数
student stu1 = new student() {PenColor = ConsoleColor.Yellow};
然后再添加Action调用。

简单例子:
[C#] 纯文本查看 复制代码
        private void button2_Click(object sender, EventArgs e)
        {
            Action<string, int> test = new Action<string, int>(tesActFu);
            Action<string, int> test2 = new Action<string, int>(tesActFu);
            test += test2;
            test.Invoke("123",123);
        }

        public void tesActFu(string t1, int t2)
        {
            Console.WriteLine(t1);
            Console.WriteLine(t2);
        }



2:BeginInvoke方法:

理解成启动一个线程进行调用委托
[C#] 纯文本查看 复制代码
action1.BeginInvoke(null,null);


第一个参数是回调方法,暂时还没学。
New Bing例子:

[C#] 纯文本查看 复制代码
using System;
using System.Threading;

class Program
{
    static void Main(string[] args)
    {
        Action<string> action1 = new Action<string>(PrintMessage);
        action1.BeginInvoke("Hello World!", new AsyncCallback(CallBackMethod), null);

        Console.WriteLine("Press any key to exit...");
        Console.ReadKey();
    }

    static void PrintMessage(string message)
    {
        Console.WriteLine(message);
    }

    static void CallBackMethod(IAsyncResult ar)
    {
        Console.WriteLine("Asynchronous operation completed.");
    }
}


3:线程:

[C#] 纯文本查看 复制代码
using System.Threading;

Student stu1 = new Student() {ID = 1, PenColor = ConsoleColor.Yellow};
Thread Thread1 = new Thread(new ThreadStart(stu1.DoHomework));



4:Task:

[C#] 纯文本查看 复制代码
using System.Threading.Tasks;

Student stu1 = new Student() {ID = 1, PenColor = ConsoleColor.Yellow};
Task task1 = new Task(new Action(stu1.DoHomework));



5:接口:

Java用接口完全替代了指针,而C#既有指针(委托)也有接口
拒绝思考。

[C#] 纯文本查看 复制代码
interface IProductFactory{
    Product Make();
}

class PizzaFactory: IProductFactory{
    public Product Make(){
        你的方法();
    }
}

IProductFactory pizzaFac = new PizzaFactory();
// IProductFactory toyFac = new ToyFactory();



6:事件:

直接易语言搬过来理解

  
子程序名返回值类型公开备 注
_时钟2_周期事件  
调整层次 ( #最高层 )




7:事件参数:

C#比较灵活,好多按钮可以挂接一个事件,然后通过参数判断是哪个按钮触发的事件。

[C#] 纯文本查看 复制代码
        private void button2_Click(object sender, EventArgs e)
        {
            Action<string, int> test = new Action<string, int>(tesActFu);
            Action<string, int> test2 = new Action<string, int>(tesActFu);
            test += test2;
            test.Invoke("123",123);
        }


object sender 这个参数 sender可以看做是 this.button1 类型的

e参数没讲,网上的实在是难懂,求个理解方法。


8:参数手动挂接 - 方法1:

[C#] 纯文本查看 复制代码
        private void Form1_Load(object sender, EventArgs e)
        {
            //button2.Click += Button2_ClickTest;
            //Func<int> testFunc = new Func<int>(testThread);
            //AsyncCallback testCallBack_AC = new AsyncCallback(testCallBack);

            //testFunc.BeginInvoke(testCallBack_AC, null);

            //Calculator ACalculator = new Calculator();
            //Calc calc1 = new Calc(ACalculator.Add);
            //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);
            //Action<string, int> testAct = tesActFu;
            //testAct.Invoke("qqq", 22);
            button2.Click += testClick;
        }
        private void testClick(object sender, EventArgs e)
        {
            Console.WriteLine("qwertyuiop");
        }


也可以这样写:

[C#] 纯文本查看 复制代码
this.button2.Click += new EventHandler(testClick);


9:参数手动挂接 - 方法2 - 匿名方法:

[C#] 纯文本查看 复制代码
            this.button2.Click += delegate (object sender, EventArgs e)
            {
                // do something here...
            }


非常少见,已经过时


9:参数手动挂接 - 方法3 - Lambda 表达式:

[C#] 纯文本查看 复制代码
            this.button2.Click += (object sender_, EventArgs e_) => {
                // do something here
            };


如果挂接在一个事件下,不要和事件参数重名。


10:WPF:

基本和C#一样,没怎么讲。


大佬帮忙改改错,万分感谢。

签到天数: 6 天

发表于 2023-5-29 14:23:18 | 显示全部楼层   湖北省武汉市
感谢分享
回复 支持 反对

使用道具 举报

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

本版积分规则 致发广告者

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

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

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