https://visualstudio.microsoft.com/zh-hans/downloads/ VS 解决方案说明
public class Furniture{ const double salesTax = .065; private double purchPrice; private string vendor, inventoryID; public Furniture(string vendor, string inventID, double purchPrice) { this.vendor = vendor; this.inventoryID = inventID; this.purchPrice = purchPrice; } public string MyVendor { get { return vendor; } } public double CalcSalesTax(double salePrice) { return salePrice * salesTax; } } using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows;namespace 委托之Action与Func{ class Program { static void Main(string[] args) { Console.WriteLine("【1】演示Action 委托:"); var actDele = new Action(TestMethods.SelfIntroduce); actDele += TestMethods.SayHi; actDele.Invoke();//调用委托 ShowTool.PrintDelimiter(50); Console.WriteLine("【2.1】演示Func<int,int,int>委托:"); var func1 = new Func<int, int, int>(TestMethods.Add2Num); Console.WriteLine("请输入第一个数:"); int a = ShowTool.GetNumInit(); Console.WriteLine("请输入第二个数:"); int b = ShowTool.GetNumInit(); Console.WriteLine(func1.Invoke(a, b)); ShowTool.PrintDelimiter(50); Console.WriteLine("【2.2】演示Func<double,double,double>委托:"); var func2 = new Func<double,double,double>(TestMethods.Multiple2Num); Console.WriteLine("请输入第一个数:"); double x = ShowTool.GetNumDouble(); Console.WriteLine("请输入第二个数:"); double y = ShowTool.GetNumDouble(); Console.WriteLine(func2.Invoke(x, y)); Console.ReadKey(); } } class TestMethods { internal static void SayHi() { Console.WriteLine("Hello,Dell Mao"); } public static void SelfIntroduce() { Console.WriteLine("Hi,My name is Dell Mao!"); } public static int Add2Num(int a, int b) { return a + b; } public static double Multiple2Num(double a, double b) { return a * b; } } class ShowTool { public static void PrintDelimiter(int lenght)//打印一个分隔符 { if (lenght < 8) { lenght = 8; } for (int i = 0; i < lenght; i++) { Console.Write("="); } Console.WriteLine(); } public static int GetNumInit() { int a = 0; try { a = Convert.ToInt32(Console.ReadLine()); return a; } catch (Exception) { MessageBox.Show("您输入的数据格式有误,将使用默认值0。"); return a; } } public static double GetNumDouble() { double a = 0; try { a = Convert.ToDouble(Console.ReadLine()); return a; } catch (Exception) { MessageBox.Show("您输入的数据格式有误,将使用默认值-999。"); return a; } } }} 请忽略我的排版,关注我的内容,吐槽一下公众号免费的编辑工具真是不太好用,收费的小编用不起。 好了,关于 C的委托 这次先总结这么多吧,下节我们再总结总结事件的基础。 在此诚挚地感谢:Timothy 刘铁猛,他的视频教程《C入门详解》免费分享,相当给力。 感兴趣的小伙伴们请自行搜索。
</double,double,double></int,int,int> 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |