Optional property in a C# class


A property is optional if it is possible and valid for it to have null. A property whose CLR type cannot have null cannot be configured optional.

An example optional attribute usage −

Example

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

Updated on: 21-Jun-2020

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements