首頁  >  文章  >  後端開發  >  如何在 C# 中使用屬性

如何在 C# 中使用屬性

王林
王林轉載
2023-09-03 14:13:131282瀏覽

如何在 C# 中使用属性

屬性是一種聲明性標記,用於向執行時傳達有關程式中各種元素(如類別、方法、結構、枚舉器、組件等)行為的資訊.您可以透過使用屬性為程式新增聲明性資訊。聲明性標記由放置在其所用於的元素上方的方括號([ ])表示。

以下是屬性的語法:

[attribute(positional_parameters, name_parameter = value, ...)]
Element

.Net Framework 提供兩種類型的屬性:預先定義屬性和自訂建置屬性。

讓我們看看如何宣告自訂屬性 -

//a custom attribute BugFix to be assigned to a class and its members
[AttributeUsage(
AttributeTargets.Class |
AttributeTargets.Constructor |
AttributeTargets.Field |
AttributeTargets.Method |
AttributeTargets.Property,
AllowMultiple = true)]
public class DeBugInfo : System.Attribute
#

以上是如何在 C# 中使用屬性的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除