zoukankan      html  css  js  c++  java
  • C#6.0 新功能

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    
    
    
    namespace ConsoleApp2
    {
        class Program
        {
          
            static void Main(string[] args)
            {
    
                Console.WriteLine("输入姓:");
                var lastName = Console.ReadLine();
                Console.WriteLine("输入名:");
    
                var firstName = Console.ReadLine();
                //string format 新写法
                var student = new Student(firstName,lastName);
    
                Console.WriteLine($"student.ToString(){ student.ToString()}");
                Console.WriteLine($"student.FullName{ student.FullName}");
    
                Console.WriteLine("C#6.0新特性");
    
                student = new Student(null,null);
    
    
                Console.WriteLine($"student.ToString(){ student.ToString()}");
                Console.WriteLine($"student.FullName{ student?.FullName}");
                Console.WriteLine($"student.Grades{ student.Grades.Count}");
    
    
    
    
               //异常过滤
                try
                {
                    throw new Exception("测试异常filter");
    
                }
                catch (Exception e) when( e.Message.Contains("filter"))
                {
    
                    Console.WriteLine(e.Message);
                    throw;
                }
                //catch (Exception e) when (e.LogException())
                //{
                //    // This is never reached!
                //}
    
    
                
    
    
                Console.WriteLine("C#6.0新特性");
    
                Console.ReadKey();
    
    
            }
    
            public class Student
            {
                public string LastName {get;}
                public string FirstName { get; }
    
                public Student(string firstName, string lastName)
                {
                    LastName = lastName;
                    FirstName = firstName;
                }
    
                public void SetName(string firstName, string lastName)
                {
                   //this. LastName = lastName;
                   //this.FirstName = firstName;
                }
                //方法表达式
                public ICollection<double> Grades { get; } = new List<double>();
    
                public string GetFormattedGradePoint() =>
    $"Name: {LastName}, {FirstName}. G.P.A: {Grades.Average():F2}";
                public override string ToString() => $"{LastName},{FirstName}";
              
                public string  FullName=>  $"{LastName},{FirstName}";
            }
        }
    }
    
  • 相关阅读:
    玩耍redis遇到的问题之记录
    哈勃望远镜--星柱图
    用js将从后台得到的时间戳(毫秒数)转换为想要的日期格式
    div水平居中
    hibernate和spring下载网址
    intellj idea 如何设置类头注释和方法注释(转载)
    转载:IT人高效的休息方式
    什么是REST?以及RESTful的实现
    easyui datagrid 获取记录数 页数 当前页
    font字体文件跨域
  • 原文地址:https://www.cnblogs.com/hellohongfu/p/6730909.html
Copyright © 2011-2022 走看看