using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSGDemo.CSG021
{
//https://docs.microsoft.com/zh-cn/dotnet/csharp/properties
class CSG021Property
{
//#csg021-01,声明方式一,最传统方式
private string _property1;
public string Property1
{
get { return _property1; }
set { _property1 = value; }
}
//#csg021-02,声明方式二,自动属性
public string Property2 { get; set; }
//#csg021-03,声明方式三,默认属性
public string Property3 { get; set; } = "default value";
//#csg021-04,声明方式四,带修饰符的访问器
public string Property4 { get; private set; }
//#csg021-05,声明方式五,使用init关键字
//init 关键字的含义为 该属性只能在构造函数中被赋值。
public string Property5 { get; init; }
//#csg021-06,声明方式五,带 expression-bodied 语法访问器
private string _property6;
public string Property6
{
get => _property6;
set => _property6 = value;
}
}
//#csg021-07,属性改变事件 INotifyPropertyChanged
//该接口用于通知数据绑定客户端值已更改
public class Person : INotifyPropertyChanged
{
private string _firstName;
public string FirstName
{
get => _firstName;
set
{
if (string.IsNullOrWhiteSpace(value))
throw new ArgumentException("First name must not be blank");
if (value != _firstName)
{
PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(nameof(FirstName)));
}
_firstName = value;
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}