Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Thuộc tính tùy chọn (Optional property) trong lớp C#


Một thuộc tính là tùy chọn nếu nó có thể và hợp lệ khi mang giá trị null. Thuộc tính có kiểu CLR không thể là null thì không thể cấu hình thành optional.

Ví dụ sử dụng attribute Optional:

Ví dụ

[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; }
}