Attribute: Difference between revisions
From MXMS Wiki
Jump to navigationJump to search
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
In the C# programming language, attributes are metadata attached to a field or a block of code like assemblies, members and types, and are equivalent to annotations in Java. Attributes are accessible to both the compiler and programmatically through reflection. In contrast, properties, in C# terminology, are members of a class which syntactically are used like instance (or class) variables, but are implemented as a pair of getter/setter functions. (In the absence of a setter, properties are read-only.) | In the C# programming language, attributes are metadata attached to a field or a block of code like assemblies, members and types, and are equivalent to annotations in Java. Attributes are accessible to both the compiler and programmatically through reflection. In contrast, properties, in C# terminology, are members of a class which syntactically are used like instance (or class) variables, but are implemented as a pair of getter/setter functions. (In the absence of a setter, properties are read-only.) | ||
<pre> | |||
[Obsolete("Use class C1 instead", IsError = true)] // causes compiler message saying | |||
public class C // that C is obsolete | |||
{...} | |||
// class name ends with "Attribute" | |||
// but can be used as "Obsolete" | |||
public class ObsoleteAttribute : Attribute | |||
{ | |||
public string Message { get; } | |||
public bool IsError { get; set; } | |||
public ObsoleteAttribute() {...} | |||
public ObsoleteAttribute(string msg) {...} | |||
public ObsoleteAttribute(string msg, bool error) {...} | |||
} | |||
[Obsolete] | |||
[Obsolete("This is obsolete")] | |||
[Obsolete("This is obsolete", false)] | |||
[Obsolete("This is obsolete", IsError = false)] | |||
</pre> | |||
Revision as of 16:21, 16 March 2026
In the C# programming language, attributes are metadata attached to a field or a block of code like assemblies, members and types, and are equivalent to annotations in Java. Attributes are accessible to both the compiler and programmatically through reflection. In contrast, properties, in C# terminology, are members of a class which syntactically are used like instance (or class) variables, but are implemented as a pair of getter/setter functions. (In the absence of a setter, properties are read-only.)
[Obsolete("Use class C1 instead", IsError = true)] // causes compiler message saying
public class C // that C is obsolete
{...}
// class name ends with "Attribute"
// but can be used as "Obsolete"
public class ObsoleteAttribute : Attribute
{
public string Message { get; }
public bool IsError { get; set; }
public ObsoleteAttribute() {...}
public ObsoleteAttribute(string msg) {...}
public ObsoleteAttribute(string msg, bool error) {...}
}
[Obsolete]
[Obsolete("This is obsolete")]
[Obsolete("This is obsolete", false)]
[Obsolete("This is obsolete", IsError = false)]