c#、vb 自动属性

时间:2021-02-13 19:26:13
vb示例:
Public Property Name() As String = "Bob"
 等效于
    Private _name As String = "Bob"
Property Name As String
Get
Return _name
End Get
Set(value As String)
_name = value
End Set
End Property
c# 示例,在c#中不允许开发者在定义自动属性时,赋值给该属性;不过clr会为属性设定安全的默认值,比如为int赋值0,引用类型赋值null。开发者可以在类的构造函数中为自动属性赋值。
    public class Book
{
public string BookName { get; private set; }
public Book()
{
BookName = "c# 自动属性";
}
}