Nullable List <> as an out parameter

Is it possible?

 private void Test(out List<ExampleClass>? ExClass) { } 

A null list <>, which is also an output parameter?

+4
source share
4 answers

List<T> is a reference type (class), so not required ? . Just assign null the ExClass parameter in the body of the method.

+20
source

As Anton said, you do not need to use Nullable<T> , but it can be an out parameter:

 private void Test(out List<ExampleClass> foo) 

Perhaps you are misleading the nullable List<T> value with List<T?> , Which would be valid for value types ... for example, you could use:

 private void Test(out List<Guid?> foo) 

which will be the output parameter, which is a list of valid values.

On the other hand, it usually does not have out parameters in void methods - you usually should use it instead of the return type.

+9
source

Use ? only for ValueTypes with a zero value.

+2
source

Whether out or not is irrelevant here. But you cannot make Nullable<T> with a class; T must be a structure. Otherwise, the compiler will complain.

Additionally, it is considered bad style to use a parameter name (use exClass instead of exClass ). Your programs will work the same way, but anyone who reads your code may be misled.

0
source

Source: https://habr.com/ru/post/1300616/


All Articles