c#入门第十六课

线程

static void Test1()
        {
            Console.WriteLine("Test1");
        }
        static void Test2()
        {
            Console.WriteLine("Test2");
        }

        static void Main(string[] args)
        {
            
            //创建子线程
            ThreadStart ts1 = new ThreadStart(Test1);
            Thread thread1 = new Thread(ts1);
            ThreadStart ts2 = new ThreadStart(Test2);
            Thread thread2 = new Thread(ts2);
            //开始线程
            thread1.Start();
            thread2.Start();
            Console.WriteLine("Main");
        }

委托

class DelegateTest
    {
        //委托 新的类型
        public delegate void CalcHandler(int a, int b);
        public DelegateTest()
        {
            /*
            CalcHandler calcHandler = new CalcHandler(Add);
            calcHandler(3, 4);
            calcHandler = new CalcHandler(Sub);
            calcHandler(4, 3);
            */
            //CalcHandler calcHandler = Add;
            //委托链
            CalcHandler calcHandler = Add;
            calcHandler += Sub;
            calcHandler(3, 4);
        }
        public void Add(int a,int b)
        {
            Console.WriteLine(a+b);
        }
        public void Sub(int a, int b)
        {
            Console.WriteLine(a - b);
        }      
    }
    class Timer
    {
        //委托
        public delegate void TimerHandler();
        public Timer(int time,TimerHandler timerHandler)
        {
            Thread.Sleep(time * 1000);
            if (timerHandler != null)
            {
                timerHandler();
            }           
        }
    }
    class Person
    {
        public Person()
        {
            //创建一个闹钟
            Timer.TimerHandler timerHandler = new Timer.TimerHandler(Eat);
            Timer timer = new Timer(3, timerHandler);
        }
        public void Eat()
        {
            Console.WriteLine("吃饭");
        }

    }
    class Game
    {
        static void Main(string[] args)
        {
            //new DelegateTest();
            new Person();
        }
    }

匿名函数

匿名函数是没有函数名的函数,仅限委托绑定

            Timer timer = new Timer(3,delegate(int  time){
                Console.WriteLine(time+"分钟过去了,该上学了");
            });
            //Lambda
            Timer timer = new Timer(3, (int time) => {
                Console.WriteLine(time + "分钟过去了,该上学了");
            });

事件

public TimerHandler timerHandler;
        public event TimerHandler TimerEvent;

timer.timerHandler = Eat;
timer.TimerEvent += Eat;
委托链可以等于,事件不可以等于

系统封装

//public Timer(int time,Action timerHandler)
//public Timer(int time,Func timerHandler)

正则表达式

static void Main(string[] args)
        {
            string input = "abcdef@163.com";
            string pattern = @"^\w{3,10}@\w+\.\w+$";
            input = "123-456789-123";
            pattern = @"\d{3}-\d{3}";
            Regex regex = new Regex(pattern);
            //Console.WriteLine(regex.IsMatch(input));
            //Match match = regex.Match(input);
            //Console.WriteLine(match.Value);
            //多匹配
            foreach(Match match in regex.Matches(input))
            {
                Console.WriteLine(match.Value);
            }
        }

IO

            /*
            StreamWriter sw = new StreamWriter("test.txt");
            sw.Write("asdasf");
            sw.Close();
            */
            //using (StreamWriter sw = new StreamWriter("test.txt"))
            //{
            //    sw.Write("asdasf");
            //}
            StreamReader sr = new StreamReader("test.txt");
            string str = sr.ReadToEnd();
            Console.WriteLine(str);
            sr.Close();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

葬月飘零

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值