C#_C#6简单使用

using System;
using System.Collections.Generic;
namespace Learn
{
    using Learn5;
    using Learn6;
    class Program
    {
        static void Main(string[] args)
        {
            Learn5.Test t5 = new Learn5.Test();
            Learn6.Test t6 = new Learn6.Test();
            t5.Print("C#5");
            t6.Print("C#6");
            Learn5.Rectangle r5 = new Learn5.Rectangle();
            Learn6.Rectangle r6 = new Learn6.Rectangle();
            //t5不为空时调用
            if (t5 != null)
            {
                string r5IsSquare = string.Format("r5是不是正方形:{0}.创建时间是{1}", t5.IsSquare(r5), r5.CreateTime);//C#5
                t5.Print(r5IsSquare);
                foreach (var p in r5.Point.Keys)
                {
                    t5.Print(string.Format("第{0}个点为({1},{2})",p+1,r5.Point[p][0], r5.Point[p][1]));
                }
            }
            //t6不为空时调用
            string r6IsSquare = $"r6是不是正方形:{t6?.IsSquare(r6)}.创建时间是{r6?.CreateTime}";//C#6
            t6?.Print(r6IsSquare);
            foreach (var p in r6.Point.Keys)
            {
                t6?.Print($"第{p + 1}个点为({r6.Point[p][0]},{r6.Point[p][1]})");
            }
            //r5.Id = 5; 只读
            //r6.Id = 5; 只读
        }
    }
}
//C#5
namespace Learn5
{
    public class Test
    {
        public void Print(string context)
        {
            Console.WriteLine(context);
        }
        public bool IsSquare(Rectangle rect)
        {
            if (rect == null) return false;
            return rect.Height == rect.Width;
        }
    }

    public class Rectangle
    {
        //实例化类是属性初始值
        public Rectangle()
        {
            Height = 1;
            Width = 1;
        }
        public double Height { get; set; }
        public double Width { get; set; }
        //只读属性
        private readonly int id = 1;
        public int Id
        {
            get { return id; }
        }
        //属性
        public string CreateTime
        {
            get { return DateTime.Now.ToString(); }
        }
        //字典赋值
        public Dictionary<int, int[]> Point = new Dictionary<int, int[]>()
        {
            { 0,new []{0,0}},
            { 1,new []{0,1}},
            { 2,new []{1,0}},
            { 3,new []{1,1}},
        };
    }
}
//C#6
namespace Learn6
{
    using static System.Console;//引用静态类
    public class Test
    {
        public void Print(string context)
        {
            WriteLine(context);//这里可以不用加Console
        }

        public bool IsSquare(Rectangle rect) => rect?.Height == rect?.Width && rect != null;//如果只有一行的话可以使用lambda表达式去写
    }

    public class Rectangle
    {
        //属性初始化
        public double Height { get; set; } = 1;
        public double Width { get; set; } = 1;
        //只读属性
        public int Id { get; } = 2;
        //属性lambda
        public string CreateTime => DateTime.Now.ToString();//属性单行一样也可以使用
        //字典赋值
        public Dictionary<int, int[]> Point = new Dictionary<int, int[]>()
        {
            [0] = new[] { 0, 0 },
            [1] = new[] { 0, 1 },
            [2] = new[] { 1, 0 },
            [3] = new[] { 1, 1 },
        };
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

林一怂儿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值