Home  >  Article  >  Backend Development  >  Optional properties in C# classes

Optional properties in C# classes

王林
王林forward
2023-09-22 10:13:07763browse

C# 类中的可选属性

The attribute is optional if it is possible and valid to be null. Properties of CLR types that are not null cannot be configured as optional.

An example usage of optional attributes:

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

The above is the detailed content of Optional properties in C# classes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete