Closed michael-damatov closed 6 years ago
Suggest to use Array.Empty<T>() method to reuse the immutable instance.
Array.Empty<T>()
Applies to:
new T[] { }
new T[0]
T[] variable = { }
T[] Property { get; } = { };
T[] Property { get; set; } = { };
Note: the analyzer is only active if the used framework has the Array.Empty<T>() method.
Suggest to use T[n] to improve code readability, where n is the item count.
T[n]
new T[] { d, default, default(T) }
T
null
0
false
'\0'
new T()
new[] { d, default, default(T) }
new[] { null as T, default }
T[] variable = { d }
T[] Property { get; } = { d };
T[] Property { get; set; } = { d };
Empty array initialization
Suggest to use
Array.Empty<T>()
method to reuse the immutable instance.Applies to:
new T[] { }
expressionsnew T[0]
expressionsT[] variable = { }
variable or type field declaration with initializationT[] Property { get; } = { };
auto property declaration with initializationT[] Property { get; set; } = { };
auto property declaration with initializationNote: the analyzer is only active if the used framework has the
Array.Empty<T>()
method.Initialization of arrays with default values
Suggest to use
T[n]
to improve code readability, where n is the item count.Applies to:
new T[] { d, default, default(T) }
where d is the default value for theT
(null
,0
,false
,'\0'
, ornew T()
whereT
is known to be value type)new[] { d, default, default(T) }
where d is the default value for theT
new[] { null as T, default }
whereT
is a reference typeT[] variable = { d }
variable or type field declaration with initialization where d is the default value for theT
(or the default expression)T[] Property { get; } = { d };
auto property declaration with initialization where d is the default value for theT
(or the default expression)T[] Property { get; set; } = { d };
auto property declaration with initialization where d is the default value for theT
(or the default expression)Current Limitations