C#:4种cool的语言功能

个人感觉C#的语言特性要比java好很多,C#语言要简单、易用。如下C#6.0语言的几个比较酷的写法:

在开始coding之前我们需要在VS中把我们的C#版本调至6.0.流程如下:

1>在项目上右键属性:

2>更改语言版本

1.变量自动赋值

class Program
    {
        public static string Id { get; } = "assignment";  //自动变量赋值

        static void Main(string[] args)
        {
            Console.WriteLine("Hi there !" + Id);
            Console.ReadKey();
        }
    }

2.字典初始化

 static Dictionary<string, string> _value = new Dictionary<string, string>()
        {
            ["key1"] = "value1",
            ["key2"] = "value2"
        };

        static void Main(string[] args)
        {
            foreach (KeyValuePair<string, string> kvp in _value)
            {
                Console.WriteLine("取到的key:{0},值:{1}", kvp, kvp.Value);
            }
            Console.ReadKey();
        }

 

3.在命名空间中申明静态类:如:using static System.Console;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static System.Console;

namespace newnamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            WriteLine("hellow world");
            ReadKey();
        }
    }
}

4.null值的判断

  class Program
    {
        static void Main(string[] args)
        {
            string value1 = "1233";
            if (true)
            {
                value1 = null;
            }
            WriteLine("value1值是否为null:"+(value1==null));
            WriteLine(value1?.Substring(0, 2));
            ReadKey();
        }
    }

在开发的过程中使用这些简易的操作会减少我们的代码量,方便代码的阅读。本篇暂未完结,后期新特性我会在实践中使用之后再补充进来。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值