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}";
}
}
}
posted on 2017-04-18 22:27 HelloHongfu 阅读(234) 评论(0) 收藏 举报
浙公网安备 33010602011771号