c#中自定义属性有无get、set 的区别

2023-07-14  本文已影响0人  大龙10

一、c#中自定义属性有无get、set 的区别

public string Name;//一般属性,可写、可读

Public string Email{get;set;}//可写、可读

Public string Age{get;}//只读

public string Height{set;}//只写
//字段
private string email;
//读取邮箱
public string GetEmail(){
    return this.email;  
}
//设置邮箱
public void SetEmail(string value){
    this.email=value;
}

private string age;
public string GetAge(){
    return this.age;
}    
private string height;
public string SetHeight(string value){
    this.height=value;
}

  总之,一个属性,就是用来给内部或者外部访问的,读写权限总要有一个,get,set 就是让你知道属性是否可读可写。

资料:https://q.cnblogs.com/q/77170/
上一篇 下一篇

猜你喜欢

热点阅读