//设置只读的属性public string FirstName { get; }public string LastName { get; }
public Person(string firstName, string lastName){ FirstName = firstName; LastName = lastName;}
public string FirstName { get; } = "";
//方法public override string ToString() => $"{FirstName},{LastName}";//只读属性public string FullName => $"{FirstName},{LastName}";
比如我们输出一个Hello World的时候
Console.WriteLine("hello world");
有了using static
我们可以
using System;using static System.Console;namespace test{ public class Program { static void Main(string[] args) { WriteLine("hello world"); } }}
貌似没鸡毛暖用,就是看起来清爽一丢丢
Person p =null;var R = p?.FirstName;//R=null
当p为null的时候返回null,当p不为null的时候返回FirstName,还可以用在方法的调用上,this.SomethingHappened?.Invoke(this, eventArgs);
Person p = null;var R = p?.FirstName ?? "Unspecified";
当p为null的时候返回后边字符串,不为空的时候返回FirstName
使用 $
作为字符串的开头,并使用 {
和 }
之间的表达式代替序号:
public string GetGradePointPercentage() => $"Name: {LastName}, {FirstName}. G.P.A: {Grades.Average():F2}";