C#学习

1.区分类、接口、类的引用、接口的引用

static void Main(string[] args)
        {
            
            #region 2
            //对象qqq
            myclass qqq = new myclass();
            Console.WriteLine(qqq.GetHashCode().ToString());
            Console.WriteLine("*****************");
            //接口myInterface的引用
            myInterface ia = qqq;
            Console.WriteLine(ia.GetHashCode().ToString());
            Console.WriteLine(ia.a);
            Console.WriteLine(ia.b);
            Console.WriteLine("*****************");
            //接口myInterface2的引用
            myInterface2 ia2 = qqq;
            Console.WriteLine(ia2.GetHashCode().ToString());
            Console.WriteLine(ia2.c);
            Console.WriteLine(ia2.d);
            Console.WriteLine("*****************");
            //引用qqq
            myclass aaa = qqq;
            Console.WriteLine(aaa.GetHashCode().ToString());
            Console.WriteLine("*****************");

            Console.WriteLine(qqq.Equals(aaa).ToString());
            Console.WriteLine(qqq.Equals(ia).ToString());
            Console.WriteLine(qqq.Equals(ia2).ToString());

            Console.Read();

            
            #endregion

        }
        delegate long mydelegate(int q, int b);
        //AsyncResult
        interface myInterface
        {
            int a { get; set; }
            int b { get; set; }
        }

        interface myInterface2
        {
            int c { get; set; }
            int d { get; set; }
        }
        class myclass:myInterface,myInterface2
        {
            public myclass()
            {
                a = 10;
                b = 11;
                c = 12;
                d = 13;
            }

            public int a { get; set; }
            public int b { get; set; }
            public int c { get; set; }
            public int d { 

运行结果:

2.异步编程

 protected static long Sum(int x, int y)
        {
            Thread.Sleep(1000);
            Console.WriteLine("Inside sum");

            return (x + y);
        }
        delegate long mydelegate(int q, int b);
        static void Main(string[] args)
        {
            #region 1
            mydelegate myDel = Sum;
            Console.WriteLine("before ");
            IAsyncResult iar = myDel.BeginInvoke(3, 5, mycallback, "haha");
            Console.WriteLine("After ");
            Console.WriteLine("Do");
            Console.ReadLine();
            #endregion
        }
         static void mycallback(IAsyncResult iar)
        {
            AsyncResult ar = (AsyncResult)iar;
            mydelegate newdelegate = (mydelegate)ar.AsyncDelegate; //获取委托对象
            long temp = newdelegate.EndInvoke(iar);
            string strtemp = iar.AsyncState as string;
            Console.WriteLine("{0}",temp);
            Console.WriteLine(strtemp);

        }

 AsyncResult实现了IA sync Result接口, AsyncResult和AsyncState分别保存了委托对象和BeginInvoke 方法调用的最后一个参数对象

运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值