12---Net基础加强

使用ShowDialog窗体之间的回传值:

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;



namespace _02使用ShowDialog窗体之间的回传值

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }



        private void button1_Click(object sender, EventArgs e)

        {

            Form2 f2 = new Form2();

            f2.ShowDialog();

            MessageBox.Show(f2.userMsg);

            MessageBox.Show("OK");

            

        }

    }

}
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;



namespace _02使用ShowDialog窗体之间的回传值

{

    public partial class Form2 : Form

    {

        public Form2()

        {

            InitializeComponent();

        }



        public string userMsg;



        private void button1_Click(object sender, EventArgs e)

        {

            this.userMsg = this.textBox1.Text;

            this.Close();                                                                                                                                                                                                                             

        }

    }

}

 

作业1:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace _01作业

{

    class Program

    {

        static void Main(string[] args)

        {

            //Delegate1 md = M1;

            //md(10,20,30);



            Delegate1 md = arr => Console.WriteLine(arr.Length);

            md(10, 20, 30);

            Console.ReadKey();

        }



        static void M1(params int[] arr)

        {

            Console.WriteLine(arr.Length);

        }

    }



    public delegate void Delegate1(params int[] array);

}

 

作业-多播委托

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace _03多播委托

{

    class Program

    {

        static void Main(string[] args)

        {

            ////一个委托同时指向了多个方法,这就是多播委托

            //Delegate1 md = M1;

            //md += M2;

            //md += M3;

            //md += M4;

            //md += M5;

            ////md = M6;//如果使用符号为委托赋值,最后会将前面的所有的绑定的委托都覆盖掉



            //md -= M3;

            //md -= M5;



            //md();//当调用委托变量的时候,绑定到该委托上的所有方法都会被调用

            //Console.ReadKey();



            //Delegate1 md = M1;

            //md = (Delegate1)Delegate.Combine(md,new Delegate1(M2));

            //md = (Delegate1)Delegate.Combine(md, new Delegate1(M3));

            //md = (Delegate1)Delegate.Remove(md, new Delegate1(M1));



            AddDelegate md = T1;

            md += T2;

            md += T3;

            //多播委托如果方法有返回值的化,这里只能获取最后一个方法的返回值.

            //int n=md(100,200);

            //Console.WriteLine(n);



            //获取每个方法被调用后的返回值

           Delegate[] mds = md.GetInvocationList();

            //循环遍历mds,获取每个委托对象并调用

           for (int i = 0; i < mds.Length; i++)

           {

               Delegate mm = mds[i];

               int n = ((AddDelegate)mm)(100,200);

               Console.WriteLine(n);

           }



            Console.ReadKey();



        }



        //static int T1(int x,int y)

        //{

        //    return x + y;

        //}



        //static int T2(int x, int y)

        //{

        //    return x + y;

        //}



        //static int T3(int x, int y)

        //{

        //    return x + y;

        //}



        static int T1(int x, int y)

        {

            return 100;

        }



        static int T2(int x, int y)

        {

            return 200;

        }



        static int T3(int x, int y)

        {

            return 300;

        }



        static void M1()

        {

            Console.WriteLine("M1");

        }



        static void M2()

        {

            Console.WriteLine("M2");

        }



        static void M3()

        {

            Console.WriteLine("M3");

        }



        static void M4()

        {

            Console.WriteLine("M4");

        }



        static void M5()

        {

            Console.WriteLine("M5");

        }



        static void M6()

        {

            Console.WriteLine("M6");

        }

    }



    public delegate int AddDelegate(int n1,int n2);

    //定义一个委托

    public delegate void Delegate1();

}

 

自定义泛型1

 泛型类:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace _04自定义泛型

{

    class Program

    {

        static void Main(string[] args)

        {

            //List<int> list = new List<int>();

            //泛型是代码重用和算法重用

            //泛型类

            //泛型接口

            //泛型方法

            //泛型委托



            //MyClass<string> mc = new MyClass<string>(new string[] { "A", "B", "C"});

            MyClass<int> mc = new MyClass<int>(new int[]{ 10,20,30});

            mc.Show();

            Console.Read();



        }

    }





    //T Type

    class MyClass<T>

    {

        public MyClass(T[] _names)

        {

            this.names = _names;

        }

        public T[] names = null;



        public void Show()

        {

            foreach (T item in names)

            {

                Console.WriteLine(item);

            }

        }

    }



}

 

泛型方法:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace _04自定义泛型

{

    class Program

    {

        static void Main(string[] args)

        {

            Person p = new Person();

            //p.show<string>("aaaaaaaaaaaa");

            //p.show("aaaaaaaaaaaa");//这两种都是正确的



            p.show<int>(123);

            Console.ReadKey();



        }

    }





    //泛型方法

    public class Person

    {

        //方法后直接写<T>就表示一个泛型方法

        public void show<T>(T msg)

        {

            Console.WriteLine(msg);

        }

    }



}

 

泛型接口:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace _04自定义泛型

{

    class Program

    {

        static void Main(string[] args)

        {

            SpiderMan<string> sm = new SpiderMan<string>();

            sm.Fly("gdsfgsdfgdsfgdsf");

            Console.ReadKey();



        }

    }



    //泛型接口

    public interface IFlyable<T>

    {

        void Fly(T msg);

    }



    //实现泛型接口的时候必须指定对应的泛型类型

    //编写一个类来实现泛型接口

    //一个普通类实现了泛型接口

    public class SupperMan:IFlyable<string>

    {

        #region MyRegion



        public void Fly(string msg)

        {

            Console.WriteLine(msg);

        }

        #endregion

    }



    //一个泛型类实现了泛型接口

    public class SpiderMan<YZK> : IFlyable<YZK>

    {



        public void Fly(YZK msg)

        {

            throw new NotImplementedException();

        }

    }



    //public class SpiderMan<YZK> : IFlyable<string>

    //{



    //    public void Fly(YZK msg)

    //    {

    //        throw new NotImplementedException();

    //    }

    //}

}

 

泛型委托:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace _04自定义泛型

{

    public delegate void M1Delegate(string msg);

    public delegate void MGenericDelegate<T>(T msg);

    class Program

    {

        static void Main(string[] args)

        {

            #region 泛型委托



            //M1Delegate md = M1;

            //md("aaaaaaaaaaaaa");

            //Console.ReadKey();



            //MGenericDelegate<string> md = M1;

            //md("aaaaaaaaaaaaa");

            //Console.ReadKey();



            //MGenericDelegate<int> md = M2;

            //md(123);

            //Console.ReadKey();



            MGenericDelegate<double> md = M3;

            md(123.45);

            Console.ReadKey();



            #endregion



        }



        static void M1(string msg)

        {

            Console.WriteLine(msg);

        }

        static void M2(int msg)

        {

            Console.WriteLine(msg);

        }

        static void M3(double msg)

        {

            Console.WriteLine(msg);

        }

        static void M4(float msg)

        {

            Console.WriteLine(msg);

        }

        static void M5(Person msg)

        {

            Console.WriteLine(msg);

        }

    }



    class Person

    { 

    

    }



}

 

泛型委托:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace _04自定义泛型

{

    public delegate void M1Delegate(string msg);

    public delegate void MGenericDelegate<T>(T msg);

    class Program

    {

        static void Main(string[] args)

        {

            #region 系统内置的泛型委托

            //只要是Action委托都是无返回值的



            ////存储无参数无返回值的方法

            //Action md = () => { Console.WriteLine("无参数无返回值。"); };

            //md();

            //Console.ReadKey();



            ////存储有几个参数的返回值

            //Action<string,int> md = (s,i) => { Console.WriteLine(s+"     "+i); };

            //md("aaaaaaaaaaaa",123);

            //Console.ReadKey();



            //带返回值的方法的时候,就需要使用另外一个泛型委托Func



            //Func<string> fn = T1;

            //string ss = fn();

            //Console.WriteLine(ss);

            //Console.ReadKey();



            ////返回值是string类型,参数是一个int类型

            //Func<int, string> fn = n => n.ToString();

            //Console.WriteLine(fn(10));

            //Console.ReadKey();



            ////返回值是string类型,参数是一个int类型

            //Func<int,int, string> fn =T2;

            //Console.WriteLine(fn(12,5));

            //Console.ReadKey();



            List<int> list = new List<int>{1,2,3,4,5,6,7,8,9};

            //List<int> listresult = list.FindAll(MyFilter);

            List<int> listresult = list.FindAll(element=>element>5);

            //list.Where(a => a > 5);

            for (int i = 0; i < listresult.Count; i++)

            {

                Console.WriteLine(listresult[i]);

            }

            Console.ReadKey();



            #endregion



        }

        static bool MyFilter(int element)

        { 

            return element>5;

        }

        static string T1()

        {

            return "aaaaaaaaaaaaa";

        }



        static string T2(int n1,int n2)

        {

            return (n1*n2).ToString();

        }

    }



}

 

泛型约束:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace _05泛型约束

{

    class Program

    {

        static void Main(string[] args)

        {

            //List<int>list=new List<int>();

            //list.Sort();



            //List<Person> list = new List<Person>();

            //list.Sort();



            //MyClass<string> mc = new MyClass<string>();

            //MyClass<int> mc = new MyClass<int>();



            //MyClass<MyTestClass> mc = new MyClass<MyTestClass>();



            MyClass<Person> mc = new MyClass<Person>();

            

        }

    }



    class MyTestClass : IComparable

    {



        public int CompareTo(object obj)

        {

            throw new NotImplementedException();

        }

    }

    //class AscByAge : IComparable<Person>

    //{



    //    public int CompareTo(Person x,Person y)

    //    {

    //        throw new NotImplementedException();

    //    }

    //}



    class Person:IComparable<Person>

    {

        //public Person(string  s)

        //{ 

        

        //}

        private int _age;



        public int Age

        {

            get { return _age; }

            set { _age = value; }

        }



        private string _name;



        public string Name

        {

            get { return _name; }

            set { _name = value; }

        }







        public int CompareTo(Person other)

        {

            throw new NotImplementedException();

        }

    }



    ////使用泛型约束,约束了T只能是值类型

    //class MyClass<T> where T:struct

    //{ 

    

    //}



    ////使用泛型约束,约束了T只能是引用类型不能是值类型

    //class MyClass<T> where T : class

    //{



    //}



    //限制T必须是实现了某个接口的类型,要求T必须是实现了IComparable接口的子类型对象或者就是该接口类型对象

    //class MyClass<T> where T : IComparable

    //{



    //}



    ////要求T必须是Person类型或者是Person类的子类型

    //class MyClass<T> where T : Person

    //{



    //}



    ////要求T必须是Person类型或者是Person类的子类型

    //class MyClass<T> 

    //    where T : Person 

    //    where T : new()//要求将来传递进来的类型必须具有一个无参数的构造函数

    //{



    //}



    //对T没有要求,但是V必须是T类型或者T类型的子类型

    class MyClass<T,V>

        where V :T

    {



    }

}

 

扩展方法:

先建一个静态类

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace _07扩展方法

{

    /// <summary>

    /// 1.增加扩展方法第一步,增加一个静态类,类名随便起

    /// 该静态类应该与将来要用扩展方法的地方在一个命名空间下。

    /// 即便命名空间不一样,用的时候也必须导入该命名空间否则不能使用

    /// </summary>

    static class MethodExt

    {

        //2.向静态类中增加一个静态方法

        //该静态方法的第一个参数就表示要给哪个类型增加该扩展方法

        //第一个修饰符this是必须的,这里的obj就表示将来调用该方法的那个

        public static void SayHello(this Person obj)

        {

            Console.WriteLine("Helllo"+obj.Name);

        }

    }

}

 

主程序:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace _07扩展方法

{

    class Program

    {

        static void Main(string[] args)

        {

            Person p = new Person();

            p.Name = "叶长种";

            p.SayHi();



            //扩展方法

            p.SayHello();

            Console.ReadKey();

        }

    }



    class Person

    {

        public string Name{get;set;}

        public void SayHi()

        {

            Console.WriteLine("Hi~~~~~~~~~~~~");

        }

    }

}

 

事件介绍:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace _08事件介绍

{

    class Program

    {

        static void Main(string[] args)

        {

            Mp3Palyer mp3 = new Mp3Palyer();

            mp3.BeforeMusicPlaying = () => { Console.WriteLine("音乐播放之前加载歌曲........."); };

            mp3.BeforeMusicStoping = () => { Console.WriteLine("音乐结束之前保存播放进度........."); };

            mp3.Start();

            mp3.PowerOff();

            Console.ReadKey();

        }

    }



    /// <summary>

    /// Mp3播放器

    /// </summary>

    public class Mp3Palyer

    {

        //希望在音乐开始播放之前插入一段代码,这段代码是由将来调用者来指定的

        public Action BeforeMusicPlaying;



        //希望在音乐停止播放之前插入一段代码,这段代码也是由将来调用者来指定的

        public Action BeforeMusicStoping;



        //启动Mp3

        public void Start()

        {

            Console.WriteLine("启动");

            if (BeforeMusicPlaying!=null)

            {

                //在音乐正式播放之前调用该委托

                BeforeMusicPlaying();               

            }

            //调用 PlayMusic()方法开始播放音乐

            PlayMusic();

        }



        private void PlayMusic()

        {

            Console.WriteLine("音乐开始播放。。。。。。。。。。");

        }





        //关机

        public void PowerOff()

        {

            if (BeforeMusicStoping != null)

            {

                //在音乐结束之前调用该委托

                BeforeMusicStoping();

            }

            //调用停止播放音乐的方法

            Stop();

        }



        //启动Mp3

        public void Stop()

        {

            Console.WriteLine("音乐停止播放");

        }

    }

}

 

 

事件案例:

创建控件

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Drawing;

using System.Data;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;



namespace _09事件案例

{

    public partial class UCTripleButton : UserControl

    {

        public UCTripleButton()

        {

            InitializeComponent();

        }



        //当在声明委托变量的前面加了event关键字自后,则委托变量就不是委托变量了,就立刻变成了事件

        public event Action TripleClick;

        int count = 0;

        //这个是按钮的单击事件

        private void button_Click(object sender, EventArgs e)

        {

            //每次单击一次统计一下

            count++;

            if (count>=3)

            {

                //MessageBox.Show("点了3次!");

                if (TripleClick!=null)

                {

                    //调用一下委托

                    TripleClick(); 

                }

            

                count = 0;

            }

        }

    }

}

窗体1:

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;



namespace _09事件案例

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }



        //这里是按钮的单击事件

        private void button1_Click(object sender, EventArgs e)

        {

            MessageBox.Show("你好!!!!!");

        }



        //委托可以直接赋值,但是事件不能直接赋值

        //事件只能使用+=或者-=来赋值,这样就避免了通过=将之前的注册的事件都覆盖掉

        private void Form1_Load(object sender, EventArgs e)

        {

            //ucTripleButton1.TripleClick = ClickThree;

            ucTripleButton1.TripleClick += new Action(ucTripleButton1_TripleClick);

        }



        void ucTripleButton1_TripleClick()

        {

            Console.WriteLine("事件处理");

        }

        private void ClickThree()

        {

            MessageBox.Show("被点击了三次!");

        }

    }

}

窗体2:

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;



namespace _09事件案例

{

    public partial class Form2 : Form

    {

        public Form2()

        {

            InitializeComponent();

        }



        private void Form2_Load(object sender, EventArgs e)

        {

            //ucTripleButton1.TripleClick = Click3;

            ucTripleButton1.TripleClick += new Action(ucTripleButton1_TripleClick);

        }



        void ucTripleButton1_TripleClick()

        {

            Console.WriteLine("事件处理");

        }

        public void Click3()

        {

            MessageBox.Show("哇塞!!!!被点击了三次哦!!!");

        }



  

    }

}

 

你可能感兴趣的:(net)