What is the difference between a Field and a Property in C#?支柱和場?
我已閱讀上面的這個話題,但它充滿了混亂的答案與等等等等。
我想知道,在普通英語中,下面的代碼是字段或屬性? 。如果它是一個領域,什麼是財產?如果它是一個財產,什麼是領域?
class Door
{
public int width { get; set; }
}
非常感謝。
What is the difference between a Field and a Property in C#?支柱和場?
我已閱讀上面的這個話題,但它充滿了混亂的答案與等等等等。
我想知道,在普通英語中,下面的代碼是字段或屬性? 。如果它是一個領域,什麼是財產?如果它是一個財產,什麼是領域?
class Door
{
public int width { get; set; }
}
非常感謝。
這就是屬性。這是使用getter,setter和後備變量創建屬性的簡寫。
class Door
{
public int width { get; set; }
}
背襯變量是匿名的,但基本上,編譯器生成的代碼是相同的:
class Door {
private int _width;
public int width {
get {
return _width;
}
set {
_width = value;
}
}
}
甲字段只是在一個類中的公共變量或結構,看起來像這樣:
class Door {
public int width;
}
在這個c ase編譯器不會創建任何代碼來處理該字段,它只是一個普通變量。
沒有看到太多的C#代碼與同線大括號。我喜歡。 – Jonesopolis
實際問題:您是否在實際項目中使用公共領域?現在我可以看到他們禁止的相當宗教感情。 –
@IlyaIvanov我的老師說田地是幼兒園的水平。在現實生活中,你只使用屬性。 –
屬性只是爲字段定義getter和setter的語法。
class Door
{
public int width { get; set; }
}
類似於
class Door
{
private int width;
public int getWidth()
{
return width;
}
public void setWidth(int i)
{
width = i;
}
}
這是一個性質。如果它是'public int width;',它將是一個字段。 – Ryan
接受答案中的註釋是否對您有幫助? –
你看過除了SO以外的任何資源嗎? – pamphlet