Code Inspection: Redundant type specification in default expression
In C#, we have been able to use default(T)
when we did not know whether T
would be a value type or reference type. Using default(T)
would return null
for reference types, or a zero-initialized value for value types. It’s pretty tedious to write though, as T
could be a very long class name.
Starting from C# 7.1, we have a default
literal that can be used instead and infers the type based on the context in which it’s used. It can be used in most places where we would normally use null
, as it will work perfectly for both value and reference types.
JetBrains Rider recognizes opportunities to use the default
literal syntax where default(T)
is being used. A quick-fix allows you to remove the redundant type specification:
public void Foo()
{
List<string> someList = default(List<string>);
}
public void Foo()
{
List<string> someList = default;
}
Last modified: 07 April 2022