public class SharpSixNewInfo { public static void Show() { //条件运算 { Student stu = null; //获取属性对象为空返回null string lastName = stu?.LastName; Console.WriteLine(lastName); //对象为空属性也为空返回其他字符串 string result = stu?.FirstName ?? "1"; Console.WriteLine(result); } //异常捕获,过滤异常符合进入 { //StaticClass.ExceptionShow(); } //事件回调 { NotifyPropertyChanged notifyPropertyChanged = new NotifyPropertyChanged(); notifyPropertyChanged.PropertyChanged += (object o, PropertyChangedEventArgs b) => { Console.WriteLine(o); }; notifyPropertyChanged.LastName = "test"; } } } /// <summary> /// 赋值后调用事件,发布订阅模式 /// </summary> public class NotifyPropertyChanged : INotifyPropertyChanged { public string LastName { get { return lastName; } set { if (value != lastName) { lastName = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(LastName))); } } } private string lastName; public event PropertyChangedEventHandler PropertyChanged; } /// <summary> /// 异常捕获 /// </summary> public class StaticClass { public static void ExceptionShow() { try { throw new Exception("测试"); } catch (Exception ex) when(ex.Message.Contains("测试")) //过滤异常信息,条件符合进入异常 { throw; } } } public class Student { public int? Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } }
原文:https://www.cnblogs.com/lemonzwt/p/14617823.html