首頁  >  文章  >  後端開發  >  C# 自動實現的屬性

C# 自動實現的屬性

WBOY
WBOY原創
2024-09-03 15:21:49671瀏覽

在類別的 get 方法和 set 方法中使用時不需要任何程式碼的屬性在 C# 中稱為自動實作屬性。使用它,給定的程式碼變得更加可讀和簡潔,並且當在程式碼中使用這些屬性時,編譯器將建立私有字段,該字段對應於這些屬性,並且只能使用get 方法和set 方法進行訪問。此類自動實作的屬性不能在介面中聲明,因為介面不允許編譯器建立與自動實作的屬性對應的私有欄位。這些在 C# 3.0 版及更高版本中可用。

文法

C# 自動實作屬性的語法如下:

Public data_type property_name{ get; set; }

其中 data_type 是屬性的資料類型,

property_name 是屬性的名稱。

C# 自動實作屬性的工作

  • 當需要在 get 方法和 set 方法內部使用一些沒有任何程式碼的屬性時,這些屬性在 C# 中稱為自動實作的屬性。
  • 透過使用 C# 中自動實現的屬性,程式碼變得更加簡潔和可讀。
  • 當程式中使用 Auto 實作的屬性時,編譯器會建立與這些屬性對應的私有字段,只能使用 set 方法和 get 方法存取這些私有欄位。
  • 自動實作的屬性不能在介面中聲明,因為這些屬性對應的編譯器所建立的私有欄位(只能使用 set 方法和 get 方法存取)是介面不允許的。
  • 自動實現的屬性是在 C# 3.0 版本以及更高版本的 C# 中引入的。

C# 自動實作的屬性範例

下面提到了不同的例子:

範例#1

C# 程序,演示程序中自動實現的屬性,通過將某些變量設置為自動實現的屬性來獲取書籍的詳細信息,使其只能使用 get 和 set 方法訪問。

代碼:

using System;
using System.Collections.Generic;
//a namespace called check is defined
namespace Check
{
//a class called books is defined
class Books
{
// three auto implemented properties are defined which can be accessed using set and get method
public int BookID { get; set; }
public string BookName { get; set; }
public string BookAuthor { get; set; }
}
//a class called property is defined
class property
{
//main method is called
public static void Main(string[] args)
{
//an instance of the books class is created
Books books = new Books();
//the auto implemented properties defined before are set with certain values
books.BookID    = 10;
books.BookName  = "To Kill a mocking bird";
books.BookAuthor = "Harper Lee";
// the auto implemented properties defined before are obtained using get method
Console.WriteLine("The BookID of the given book is: {0} ", books.BookID);
Console.WriteLine("The name of the given book is: {0} ", books.BookName);
Console.WriteLine("The Author of the given book is: {0} ", books.BookAuthor);
}
}
}

輸出:

C# 自動實現的屬性

在上面的程式中,定義了一個名為 check 的命名空間。然後定義一個名為 books 的類別。然後定義了三個自動實作的屬性,可以使用 set 和 get 方法存取它們。然後定義一個名為property的類別。然後建立該書類別的一個實例。然後將先前定義的自動實現的屬性設定為某些值。然後使用get方法取得先前定義的自動實作的屬性。

範例#2

C# 程序,演示程序中自動實現的屬性,通過將某些變量設置為自動實現的屬性來獲取書籍的詳細信息,使其只能使用 get 和 set 方法訪問。

代碼:

using System;
using System.Collections.Generic;
//a namespace called check is defined
namespace Check
{
//a class called players is defined
class players
{
// three auto implemented properties are defined which can be accessed using set and get method
public int playerposition { get; set; }
public string playerName { get; set; }
public string playerteam { get; set; }
}
//a class called property is defined
class property
{
//main method is called
public static void Main(string[] args)
{
//an instance of the books class is created
players play = new players();
//the auto implemented properties defined before are set with certain values
play.playerposition    = 1;
play.playerName  = "Sachin Tendulkar";
play.playerteam = "India";
// the auto implemented properties defined before are obtained using get method
Console.WriteLine("The position  of the given player is: {0} ", play.playerposition);
Console.WriteLine("The name of the given player is: {0} ", play.playerName);
Console.WriteLine("The team for which the player plays is: {0} ", play.playerteam);
}
}
}

輸出:

C# 自動實現的屬性

在上面的程式中,定義了一個名為 check 的命名空間。然後定義一個名為players的類別。然後定義了三個自動實作的屬性,可以使用 set 和 get 方法存取它們。然後定義一個名為property的類別。然後創建玩家類別的實例。然後將先前定義的自動實現的屬性設定為某些值。然後使用get方法取得先前定義的自動實作的屬性。最後,輸出如上面的快照所示。

優點

在 C# 中使用自動實現的屬性有幾個優點。他們是:

  • 透過使用自動實現的屬性,應用程式介面可以面向未來。因此,如果我們想稍後在 getter 或 setter 中使用邏輯,應用程式介面不會改變。
  • 資料綁定只能透過使用自動實現的屬性來實現。這是因為資料綁定框架僅適用於屬性,不適用於欄位。

結論

在本教程中,我們透過定義、語法和程式設計範例及其輸出和優點來了解 C# 中自動實現屬性的概念。

以上是C# 自動實現的屬性的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn