01索引器
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _01索引器 8 { 9 class Program10 {11 static void Main(string[] args)12 {13 Person p = new Person();14 p[0] = "张三";15 p[1] = "李四";16 p[1, 1] = "新数组";17 p[2, 1] = "新数组";18 //p["张三"] = 15;19 //p["李四"] = 20;20 // p[]21 //p[0] = 1;22 //p[1] = 2;23 //Console.WriteLine(p[0]);24 //Console.WriteLine(p[1]);25 Console.ReadKey();26 }27 }28 29 30 public class Person31 {32 //int[] nums = new int[10];33 //public int this[int index]34 //{35 // get { return nums[index]; }36 // set { nums[index] = value; }37 //}38 //string[] names = new string[10];39 ////public string this[int index]40 ////{41 //// get { return names[index]; }42 //// set { names[index] = value; }43 ////}44 45 string[] names = new string[10];46 string[] newNames = new string[20];47 public string this[int index]48 {49 get { return names[index]; }50 set { names[index] = value; }51 }52 53 public string this[int index,int n]54 {55 get { return newNames[index]; }56 set { newNames[index] = value; }57 }58 //// Listlist = new List ();59 // Dictionary dic = new Dictionary ();60 // public int this[string index]61 // {62 // get { return dic[index]; }63 // set { dic[index] = value; }64 // }65 66 67 68 69 70 }71 }
2、单例模式
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms;10 11 namespace _2_单例模式12 {13 public partial class Form1 : Form14 {15 public Form1()16 {17 InitializeComponent();18 }19 20 private void button1_Click(object sender, EventArgs e)21 {22 Form2 frm2 = Form2.GetSingle();//new Form2();23 frm2.Show();24 }25 }26 }
1 namespace _2_单例模式 2 { 3 partial class Form1 4 { 5 ///6 /// 必需的设计器变量。 7 /// 8 private System.ComponentModel.IContainer components = null; 9 10 ///11 /// 清理所有正在使用的资源。12 /// 13 /// 如果应释放托管资源,为 true;否则为 false。14 protected override void Dispose(bool disposing)15 {16 if (disposing && (components != null))17 {18 components.Dispose();19 }20 base.Dispose(disposing);21 }22 23 #region Windows 窗体设计器生成的代码24 25 ///26 /// 设计器支持所需的方法 - 不要27 /// 使用代码编辑器修改此方法的内容。28 /// 29 private void InitializeComponent()30 {31 this.button1 = new System.Windows.Forms.Button();32 this.SuspendLayout();33 // 34 // button135 // 36 this.button1.Location = new System.Drawing.Point(357, 93);37 this.button1.Name = "button1";38 this.button1.Size = new System.Drawing.Size(75, 23);39 this.button1.TabIndex = 0;40 this.button1.Text = "button1";41 this.button1.UseVisualStyleBackColor = true;42 this.button1.Click += new System.EventHandler(this.button1_Click);43 // 44 // Form145 // 46 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);47 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;48 this.ClientSize = new System.Drawing.Size(646, 408);49 this.Controls.Add(this.button1);50 this.Name = "Form1";51 this.Text = "Form1";52 this.ResumeLayout(false);53 54 }55 56 #endregion57 58 private System.Windows.Forms.Button button1;59 }60 }
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms;10 11 namespace _2_单例模式12 {13 public partial class Form2 : Form14 {15 //全局唯一的单例16 public static Form2 FrmSingle=null;17 18 private Form2()19 {20 InitializeComponent();21 }22 23 public static Form2 GetSingle()24 {25 if (FrmSingle == null)26 {27 FrmSingle = new Form2();28 }29 return FrmSingle;30 }31 }32 }
1 namespace _2_单例模式 2 { 3 partial class Form2 4 { 5 ///6 /// Required designer variable. 7 /// 8 private System.ComponentModel.IContainer components = null; 9 10 ///11 /// Clean up any resources being used.12 /// 13 /// true if managed resources should be disposed; otherwise, false.14 protected override void Dispose(bool disposing)15 {16 if (disposing && (components != null))17 {18 components.Dispose();19 }20 base.Dispose(disposing);21 }22 23 #region Windows Form Designer generated code24 25 ///26 /// Required method for Designer support - do not modify27 /// the contents of this method with the code editor.28 /// 29 private void InitializeComponent()30 {31 this.SuspendLayout();32 // 33 // Form234 // 35 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);36 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;37 this.ClientSize = new System.Drawing.Size(494, 360);38 this.Name = "Form2";39 this.Text = "Form2";40 this.ResumeLayout(false);41 42 }43 44 #endregion45 }46 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Threading.Tasks; 5 using System.Windows.Forms; 6 7 namespace _2_单例模式 8 { 9 static class Program10 {11 ///12 /// 应用程序的主入口点。13 /// 14 [STAThread]15 static void Main()16 {17 Application.EnableVisualStyles();18 Application.SetCompatibleTextRenderingDefault(false);19 Application.Run(new Form1());20 }21 }22 }
03创建XML
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Xml; 7 namespace _03创建XML 8 { 9 class Program10 {11 static void Main(string[] args)12 {13 //通过代码来创建XML文档14 //1、引用命名空间15 //2、创建XML文档对象16 XmlDocument doc = new XmlDocument();17 //3、创建第一个行描述信息,并且添加到doc文档中18 XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);19 doc.AppendChild(dec);20 //4、创建根节点21 XmlElement books = doc.CreateElement("Books");22 //将根节点添加到文档中23 doc.AppendChild(books);24 25 //5、给根节点Books创建子节点26 XmlElement book1 = doc.CreateElement("Book");27 //将book添加到根节点28 books.AppendChild(book1);29 30 31 //6、给Book1添加子节点32 XmlElement name1 = doc.CreateElement("Name");33 name1.InnerText = "金瓶";34 book1.AppendChild(name1);35 36 XmlElement price1 = doc.CreateElement("Price");37 price1.InnerText = "10";38 book1.AppendChild(price1);39 40 XmlElement des1 = doc.CreateElement("Des");41 des1.InnerText = "好看";42 book1.AppendChild(des1);43 44 XmlElement book2 = doc.CreateElement("Book");45 books.AppendChild(book2);46 47 48 XmlElement name2 = doc.CreateElement("Name");49 name2.InnerText = "金瓶";50 book2.AppendChild(name2);51 52 XmlElement price2= doc.CreateElement("Price");53 price2.InnerText = "10";54 book2.AppendChild(price2);55 56 XmlElement des2 = doc.CreateElement("Des");57 des2.InnerText = "好看";58 book2.AppendChild(des2);59 60 doc.Save("Books.xml");61 Console.WriteLine("保存成功");62 Console.ReadKey();63 }64 }65 }
1 2 3
4、创建带属性的XML文档
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Xml; 7 namespace _4_创建带属性的XML文档 8 { 9 class Program10 {11 static void Main(string[] args)12 {13 XmlDocument doc = new XmlDocument();14 XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8","yes");15 doc.AppendChild(dec);16 17 XmlElement order = doc.CreateElement("Order");18 doc.AppendChild(order);19 20 XmlElement customerName = doc.CreateElement("CustomerName");21 customerName.InnerXml = "我是一个p标签
";22 order.AppendChild(customerName);23 24 XmlElement customerNumber = doc.CreateElement("CustomerNumber");25 customerNumber.InnerText = "我是一个p标签
";26 order.AppendChild(customerNumber);27 28 29 XmlElement items = doc.CreateElement("Items");30 order.AppendChild(items);31 32 XmlElement orderItem1 = doc.CreateElement("OrderItem");33 //给节点添加属性34 orderItem1.SetAttribute("Name", "充气娃娃");35 orderItem1.SetAttribute("Count", "10");36 items.AppendChild(orderItem1);37 38 XmlElement orderItem2 = doc.CreateElement("OrderItem");39 //给节点添加属性40 orderItem2.SetAttribute("Name", "充气娃娃");41 orderItem2.SetAttribute("Count", "10");42 items.AppendChild(orderItem2);43 44 XmlElement orderItem3 = doc.CreateElement("OrderItem");45 //给节点添加属性46 orderItem3.SetAttribute("Name", "充气娃娃");47 orderItem3.SetAttribute("Count", "10");48 items.AppendChild(orderItem3);49 50 doc.Save("Order.xml");51 Console.WriteLine("保存成功");52 Console.ReadKey();53 54 55 }56 }57 }
5、追击XML
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Xml; 7 using System.IO; 8 namespace _5_追击XML 9 {10 class Program11 {12 static void Main(string[] args)13 {14 //追加XML文档15 XmlDocument doc = new XmlDocument();16 XmlElement books;17 if (File.Exists("Books.xml"))18 {19 //如果文件存在 加载XML20 doc.Load("Books.xml");21 //获得文件的根节点22 books = doc.DocumentElement;23 }24 else25 {26 //如果文件不存在27 //创建第一行28 XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);29 doc.AppendChild(dec);30 //创建跟节点31 books = doc.CreateElement("Books");32 doc.AppendChild(books);33 }34 //5、给根节点Books创建子节点35 XmlElement book1 = doc.CreateElement("Book");36 //将book添加到根节点37 books.AppendChild(book1);38 39 40 //6、给Book1添加子节点41 XmlElement name1 = doc.CreateElement("Name");42 name1.InnerText = "c#开发大全";43 book1.AppendChild(name1);44 45 XmlElement price1 = doc.CreateElement("Price");46 price1.InnerText = "110";47 book1.AppendChild(price1);48 49 XmlElement des1 = doc.CreateElement("Des");50 des1.InnerText = "看不懂";51 book1.AppendChild(des1);52 53 54 doc.Save("Books.xml");55 Console.WriteLine("保存成功");56 Console.ReadKey();57 58 }59 }60 }
6、读取XML文档
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Xml; 7 namespace _6_读取XML文档 8 { 9 class Program10 {11 static void Main(string[] args)12 {13 //XmlDocument doc = new XmlDocument();14 ////加载要读取的XML15 //doc.Load("Books.xml");16 17 ////获得根节点18 //XmlElement books = doc.DocumentElement;19 20 ////获得子节点 返回节点的集合21 //XmlNodeList xnl = books.ChildNodes;22 23 //foreach (XmlNode item in xnl)24 //{25 // Console.WriteLine(item.InnerText);26 //}27 //Console.ReadKey();28 29 30 //读取带属性的XML文档31 32 //XmlDocument doc = new XmlDocument();33 //doc.Load("Order.xml");34 //Xpath35 36 //XmlDocument doc = new XmlDocument();37 //doc.Load("Order.xml");38 //XmlNodeList xnl = doc.SelectNodes("/Order/Items/OrderItem");39 40 //foreach (XmlNode node in xnl)41 //{42 // Console.WriteLine(node.Attributes["Name"].Value);43 // Console.WriteLine(node.Attributes["Count"].Value);44 //}45 //Console.ReadKey();46 //改变属性的值47 //XmlDocument doc = new XmlDocument();48 //doc.Load("Order.xml");49 //XmlNode xn = doc.SelectSingleNode("/Order/Items/OrderItem[@Name='190']");50 //xn.Attributes["Count"].Value = "200";51 //xn.Attributes["Name"].Value = "颜世伟";52 //doc.Save("Order.xml");53 //Console.WriteLine("保存成功");54 55 56 57 58 XmlDocument doc = new XmlDocument();59 60 doc.Load("Order.xml");61 62 XmlNode xn = doc.SelectSingleNode("/Order/Items");63 64 xn.RemoveAll();65 doc.Save("Order.xml");66 Console.WriteLine("删除成功");67 Console.ReadKey();68 69 ////获得文档的根节点70 //XmlElement order = doc.DocumentElement;71 //XmlNodeList xnl = order.ChildNodes;72 //foreach (XmlNode item in xnl)73 //{74 // 如果不是Items 就continue75 // //if (item[])76 // //{77 // // continue;78 // //}79 // Console.WriteLine(item.Attributes["Name"].Value);80 // Console.WriteLine(item.Attributes["Count"].Value);81 //}82 Console.ReadKey();83 }84 }85 }
7、为什么要使用委托
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _7_为什么要使用委托 8 { 9 public delegate string DelProStr(string name);10 class Program11 {12 static void Main(string[] args)13 {14 //三个需求15 //1、将一个字符串数组中每个元素都转换成大写16 //2、将一个字符串数组中每个元素都转换成小写17 //3、将一个字符串数组中每个元素两边都加上 双引号18 string[] names = { "abCDefG", "HIJKlmnOP", "QRsTuvW", "XyZ" };19 //ProStToUpper(names);20 //ProStrToLower(names);21 // ProStrSYH(names);22 23 ProStr(names, delegate(string name)24 {25 return "\"" + name + "\"";26 });27 for (int i = 0; i < names.Length; i++)28 {29 Console.WriteLine(names[i]);30 }31 Console.ReadKey();32 }33 34 35 public static void ProStr(string[] name, DelProStr del)36 {37 for (int i = 0; i < name.Length; i++)38 {39 name[i] = del(name[i]);40 }41 }42 43 //public static string StrToUpper(string name)44 //{45 // return name.ToUpper();46 //}47 48 //public static string StrToLower(string name)49 //{50 // return name.ToLower();51 //}52 53 //public static string StrSYH(string name)54 //{55 // return "\"" + name + "\"";56 //}57 58 59 //public static void ProStToUpper(string[] name)60 //{61 // for (int i = 0; i < name.Length; i++)62 // {63 // name[i] = name[i].ToUpper();64 // }65 //}66 //public static void ProStrToLower(string[] name)67 //{68 // for (int i = 0; i < name.Length; i++)69 // {70 // name[i] = name[i].ToLower();71 // }72 //}73 //public static void ProStrSYH(string[] names)74 //{75 // for (int i = 0; i < names.Length; i++)76 // {77 // names[i] = "\"" + names[i] + "\"";78 // }79 //}80 81 82 }83 }
8、委托概念
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading; 6 using System.Threading.Tasks; 7 8 namespace _8_委托概念 9 {10 //声明一个委托指向一个函数11 public delegate void DelSayHi(string name);12 class Program13 {14 static void Main(string[] args)15 {16 //DelSayHi del = SayHiEnglish;//new DelSayHi(SayHiEnglish);17 //del("张三");18 //Console.ReadKey();19 20 //Test("张三", SayHiChinese);21 //Test("李四", SayHiEnglish);22 //Console.ReadKey();23 }24 25 public static void Test(string name,DelSayHi del)26 { 27 //调用28 del(name);29 }30 31 public static void SayHiChinese(string name)32 {33 Console.WriteLine("吃了么?" + name);34 }35 public static void SayHiEnglish(string name)36 {37 Console.WriteLine("Nice to meet you" + name);38 }39 }40 }
9、匿名函数
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _9_匿名函数 8 { 9 public delegate void DelSayHi(string name);10 class Program11 {12 static void Main(string[] args)13 {14 //SayHi("张三", SayHiChinese);15 //DelSayHi del = delegate(string name)16 //{17 // Console.WriteLine("你好" + name);18 //};19 //del("张三");20 21 22 //lamda表达式 => goes to23 DelSayHi del = (string name) => { Console.WriteLine("你好" + name); };24 del("张三");25 Console.ReadKey();26 }27 28 //public static void SayHi(string name,DelSayHi del)29 //{30 // del(name);31 //}32 33 //public static void SayHiChinese(string name)34 //{35 // Console.WriteLine("你好"+name);36 //}37 //public static void SayHiEnglish(string name)38 //{39 // Console.WriteLine("Hello"+name);40 //}41 }42 }
10求数组的最大值
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _10求数组的最大值 8 { 9 public delegate int DelCompare(object o1, object o2);10 class Program11 {12 static void Main(string[] args)13 {14 object[] o = { "abc","fdsfdsds","fdsfdsfdsfdsfdsfds","fdsfds"};15 //object result = GetMax(o, Compare2);16 //object result = GetMax(o, delegate(object o1, object o2) {17 // string s1 = (string)o1;18 // string s2 = (string)o2;19 // return s1.Length - s2.Length;20 //});21 22 object result = GetMax(o, (object o1,object o2) => {23 string s1 = (string)o1;24 string s2 = (string)o2;25 return s1.Length - s2.Length;26 });27 Console.WriteLine(result);28 Console.ReadKey();29 }30 31 public static object GetMax(object[] nums, DelCompare del)32 {33 object max = nums[0];34 for (int i = 0; i < nums.Length; i++)35 {36 //要传一个比较的方法37 if (del(max, nums[i]) < 0)38 {39 max = nums[i];40 }41 }42 return max;43 }44 45 46 //public static int Compare1(object o1, object o2)47 //{48 // int n1 = (int)o1;49 // int n2 = (int)o2;50 // return n1 - n2;51 //}52 53 //public static int Compare2(object o1, object o2)54 //{55 // string s1 = (string)o1;56 // string s2 = (string)o2;57 // return s1.Length - s2.Length;58 //}59 60 //public static string GetMax(string[] names)61 //{62 // string max = names[0];63 // for (int i = 0; i < names.Length; i++)64 // {65 // if (max.Length < names[i].Length)66 // {67 // max = names[i];68 // }69 // }70 // return max;71 //}72 }73 }
11、泛型委托
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _11_泛型委托 8 { 9 public delegate int DelCompare(T t1, T t2);10 // public delegate int DelCompare(object o1, object o2);11 class Program12 {13 static void Main(string[] args)14 {15 int[] nums = { 1, 2, 3, 4, 5 };16 int max = GetMax (nums, Compare1);17 Console.WriteLine(max);18 19 string[] names = { "abcdefg", "fdsfds", "fdsfdsfdsfdsfdsfdsfdsfsd" };20 string max1 = GetMax (names, (string s1, string s2) =>21 {22 return s1.Length - s2.Length;23 });24 Console.WriteLine(max1);25 Console.ReadKey();26 }27 public static T GetMax (T[] nums, DelCompare del)28 {29 T max = nums[0];30 for (int i = 0; i < nums.Length; i++)31 {32 //要传一个比较的方法33 if (del(max, nums[i]) < 0)34 {35 max = nums[i];36 }37 }38 return max;39 }40 41 42 public static int Compare1(int n1, int n2)43 {44 return n1 - n2;45 }46 }47 }
12、lamda表达式
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _12_lamda表达式 8 { 9 10 public delegate void DelOne();11 public delegate void DelTwo(string name);12 public delegate string DelThree(string name);13 class Program14 {15 static void Main(string[] args)16 {17 DelOne del = () => { };// delegate() { };18 19 DelTwo del2 = (string name) => { };//delegate(string name) { };20 21 DelThree del3 = (string name) => { return name; };//delegate(string name) { return name; };22 23 24 List list = new List () { 1, 2, 3, 4, 5, 6, 7, 8, 9 };25 list.RemoveAll(n => n > 4);26 foreach (var item in list)27 {28 Console.WriteLine(item);29 }30 Console.ReadKey();31 }32 }33 }
13、使用委托来进行窗体传值
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _13_使用委托来进行窗体传值 8 { 9 class Program10 {11 static void Main(string[] args)12 {13 }14 }15 }
14、窗体传值
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms;10 11 namespace _14_窗体传值12 {13 public partial class Form1 : Form14 {15 public Form1()16 {17 InitializeComponent();18 }19 20 private void button1_Click(object sender, EventArgs e)21 {22 Form2 frm2 = new Form2(ShowMsg);23 frm2.Show();24 }25 26 27 void ShowMsg(string str)28 {29 label1.Text = str;30 }31 }32 }
1 namespace _14_窗体传值 2 { 3 partial class Form1 4 { 5 ///6 /// 必需的设计器变量。 7 /// 8 private System.ComponentModel.IContainer components = null; 9 10 ///11 /// 清理所有正在使用的资源。12 /// 13 /// 如果应释放托管资源,为 true;否则为 false。14 protected override void Dispose(bool disposing)15 {16 if (disposing && (components != null))17 {18 components.Dispose();19 }20 base.Dispose(disposing);21 }22 23 #region Windows 窗体设计器生成的代码24 25 ///26 /// 设计器支持所需的方法 - 不要27 /// 使用代码编辑器修改此方法的内容。28 /// 29 private void InitializeComponent()30 {31 this.button1 = new System.Windows.Forms.Button();32 this.label1 = new System.Windows.Forms.Label();33 this.SuspendLayout();34 // 35 // button136 // 37 this.button1.Location = new System.Drawing.Point(431, 85);38 this.button1.Name = "button1";39 this.button1.Size = new System.Drawing.Size(75, 23);40 this.button1.TabIndex = 0;41 this.button1.Text = "button1";42 this.button1.UseVisualStyleBackColor = true;43 this.button1.Click += new System.EventHandler(this.button1_Click);44 // 45 // label146 // 47 this.label1.AutoSize = true;48 this.label1.Location = new System.Drawing.Point(137, 95);49 this.label1.Name = "label1";50 this.label1.Size = new System.Drawing.Size(41, 12);51 this.label1.TabIndex = 1;52 this.label1.Text = "label1";53 // 54 // Form155 // 56 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);57 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;58 this.ClientSize = new System.Drawing.Size(628, 400);59 this.Controls.Add(this.label1);60 this.Controls.Add(this.button1);61 this.Name = "Form1";62 this.Text = "Form1";63 this.ResumeLayout(false);64 this.PerformLayout();65 66 }67 68 #endregion69 70 private System.Windows.Forms.Button button1;71 private System.Windows.Forms.Label label1;72 }73 }
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms;10 11 namespace _14_窗体传值12 {13 14 //声明一个委托15 public delegate void DelTest(string str);16 public partial class Form2 : Form17 {18 public DelTest _del;19 public Form2(DelTest del)20 {21 this._del = del;22 InitializeComponent();23 }24 25 private void button1_Click(object sender, EventArgs e)26 {27 _del(textBox1.Text);28 }29 }30 }
1 namespace _14_窗体传值 2 { 3 partial class Form2 4 { 5 ///6 /// Required designer variable. 7 /// 8 private System.ComponentModel.IContainer components = null; 9 10 ///11 /// Clean up any resources being used.12 /// 13 /// true if managed resources should be disposed; otherwise, false.14 protected override void Dispose(bool disposing)15 {16 if (disposing && (components != null))17 {18 components.Dispose();19 }20 base.Dispose(disposing);21 }22 23 #region Windows Form Designer generated code24 25 ///26 /// Required method for Designer support - do not modify27 /// the contents of this method with the code editor.28 /// 29 private void InitializeComponent()30 {31 this.button1 = new System.Windows.Forms.Button();32 this.textBox1 = new System.Windows.Forms.TextBox();33 this.SuspendLayout();34 // 35 // button136 // 37 this.button1.Location = new System.Drawing.Point(346, 140);38 this.button1.Name = "button1";39 this.button1.Size = new System.Drawing.Size(75, 23);40 this.button1.TabIndex = 0;41 this.button1.Text = "button1";42 this.button1.UseVisualStyleBackColor = true;43 this.button1.Click += new System.EventHandler(this.button1_Click);44 // 45 // textBox146 // 47 this.textBox1.Location = new System.Drawing.Point(189, 140);48 this.textBox1.Name = "textBox1";49 this.textBox1.Size = new System.Drawing.Size(100, 21);50 this.textBox1.TabIndex = 1;51 // 52 // Form253 // 54 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);55 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;56 this.ClientSize = new System.Drawing.Size(485, 388);57 this.Controls.Add(this.textBox1);58 this.Controls.Add(this.button1);59 this.Name = "Form2";60 this.Text = "Form2";61 this.ResumeLayout(false);62 this.PerformLayout();63 64 }65 66 #endregion67 68 private System.Windows.Forms.Button button1;69 private System.Windows.Forms.TextBox textBox1;70 }71 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Threading.Tasks; 5 using System.Windows.Forms; 6 7 namespace _14_窗体传值 8 { 9 static class Program10 {11 ///12 /// 应用程序的主入口点。13 /// 14 [STAThread]15 static void Main()16 {17 Application.EnableVisualStyles();18 Application.SetCompatibleTextRenderingDefault(false);19 Application.Run(new Form1());20 }21 }22 }
15多播委托
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _15多播委托 8 { 9 public delegate void DelTest();10 class Program11 {12 static void Main(string[] args)13 {14 DelTest del = T1;15 del += T2;16 del += T3;17 del+= T4;18 del -= T3;19 del -= T1;20 del();21 Console.ReadKey();22 }23 24 public static void T1()25 {26 Console.WriteLine("我是T1");27 }28 public static void T2()29 {30 Console.WriteLine("我是T2");31 }32 33 public static void T3()34 {35 Console.WriteLine("我是T3");36 }37 public static void T4()38 {39 Console.WriteLine("我是T4");40 }41 }42 }