C# 類中的可選屬性


如果可能並且允許為其提供 null,則屬性為可選。CLR 型別不能為 null 的屬性不能配置為可選。

可選屬性使用示例 −

示例

[AttributeUsage(AttributeTargets.Property,
Inherited = false,
AllowMultiple = false)]
internal sealed class OptionalAttribute : Attribute { }

public class Employee {
   public string EmpName { get; set; }

   [Optional]
   public string AlternativeName { get; set; }
}

更新日期:21-6-2020

5K+ 瀏覽

開啟你的 職業生涯

完成課程並獲得認證

開始
廣告