1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 namespace System
.Diagnostics
.CodeAnalysis
7 /// <summary>Specifies that null is allowed as an input even if the corresponding type disallows it.</summary>
8 [AttributeUsage(AttributeTargets
.Field
| AttributeTargets
.Parameter
| AttributeTargets
.Property
, Inherited
= false)]
9 #if INTERNAL_NULLABLE_ATTRIBUTES
14 sealed class AllowNullAttribute
: Attribute { }
16 /// <summary>Specifies that null is disallowed as an input even if the corresponding type allows it.</summary>
17 [AttributeUsage(AttributeTargets
.Field
| AttributeTargets
.Parameter
| AttributeTargets
.Property
, Inherited
= false)]
18 #if INTERNAL_NULLABLE_ATTRIBUTES
23 sealed class DisallowNullAttribute
: Attribute { }
25 /// <summary>Specifies that an output may be null even if the corresponding type disallows it.</summary>
26 [AttributeUsage(AttributeTargets
.Field
| AttributeTargets
.Parameter
| AttributeTargets
.Property
| AttributeTargets
.ReturnValue
, Inherited
= false)]
27 #if INTERNAL_NULLABLE_ATTRIBUTES
32 sealed class MaybeNullAttribute
: Attribute { }
34 /// <summary>Specifies that an output will not be null even if the corresponding type allows it.</summary>
35 [AttributeUsage(AttributeTargets
.Field
| AttributeTargets
.Parameter
| AttributeTargets
.Property
| AttributeTargets
.ReturnValue
, Inherited
= false)]
36 #if INTERNAL_NULLABLE_ATTRIBUTES
41 sealed class NotNullAttribute
: Attribute { }
43 /// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter may be null even if the corresponding type disallows it.</summary>
44 [AttributeUsage(AttributeTargets
.Parameter
, Inherited
= false)]
45 #if INTERNAL_NULLABLE_ATTRIBUTES
50 sealed class MaybeNullWhenAttribute
: Attribute
52 /// <summary>Initializes the attribute with the specified return value condition.</summary>
53 /// <param name="returnValue">
54 /// The return value condition. If the method returns this value, the associated parameter may be null.
56 public MaybeNullWhenAttribute(bool returnValue
) => ReturnValue
= returnValue
;
58 /// <summary>Gets the return value condition.</summary>
59 public bool ReturnValue { get; }
62 /// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter will not be null even if the corresponding type allows it.</summary>
63 [AttributeUsage(AttributeTargets
.Parameter
, Inherited
= false)]
64 #if INTERNAL_NULLABLE_ATTRIBUTES
69 sealed class NotNullWhenAttribute
: Attribute
71 /// <summary>Initializes the attribute with the specified return value condition.</summary>
72 /// <param name="returnValue">
73 /// The return value condition. If the method returns this value, the associated parameter will not be null.
75 public NotNullWhenAttribute(bool returnValue
) => ReturnValue
= returnValue
;
77 /// <summary>Gets the return value condition.</summary>
78 public bool ReturnValue { get; }
81 /// <summary>Specifies that the output will be non-null if the named parameter is non-null.</summary>
82 [AttributeUsage(AttributeTargets
.Parameter
| AttributeTargets
.Property
| AttributeTargets
.ReturnValue
, AllowMultiple
= true, Inherited
= false)]
83 #if INTERNAL_NULLABLE_ATTRIBUTES
88 sealed class NotNullIfNotNullAttribute
: Attribute
90 /// <summary>Initializes the attribute with the associated parameter name.</summary>
91 /// <param name="parameterName">
92 /// The associated parameter name. The output will be non-null if the argument to the parameter specified is non-null.
94 public NotNullIfNotNullAttribute(string parameterName
) => ParameterName
= parameterName
;
96 /// <summary>Gets the associated parameter name.</summary>
97 public string ParameterName { get; }
100 /// <summary>Applied to a method that will never return under any circumstance.</summary>
101 [AttributeUsage(AttributeTargets
.Method
, Inherited
= false)]
102 #if INTERNAL_NULLABLE_ATTRIBUTES
107 sealed class DoesNotReturnAttribute
: Attribute { }
109 /// <summary>Specifies that the method will not return if the associated Boolean parameter is passed the specified value.</summary>
110 [AttributeUsage(AttributeTargets
.Parameter
, Inherited
= false)]
111 #if INTERNAL_NULLABLE_ATTRIBUTES
116 sealed class DoesNotReturnIfAttribute
: Attribute
118 /// <summary>Initializes the attribute with the specified parameter value.</summary>
119 /// <param name="parameterValue">
120 /// The condition parameter value. Code after the method will be considered unreachable by diagnostics if the argument to
121 /// the associated parameter matches this value.
123 public DoesNotReturnIfAttribute(bool parameterValue
) => ParameterValue
= parameterValue
;
125 /// <summary>Gets the condition parameter value.</summary>
126 public bool ParameterValue { get; }