views:

267

answers:

4

Is this possible?

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

}

A nullable List<> that is also an out parameter?

+15  A: 

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

Anton Gogolev
+7  A: 

As Anton said, you don't need to use Nullable<T> - but it could certainly be an out parameter:

private void Test(out List<ExampleClass> foo)

It's possible you're confusing a nullable List<T> with a List<T?> which would be valid for value types... for example, you could use:

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

which would be an out parameter which is a list of nullable guids.

On the other hand, it's not generally nice to have out parameters in void methods - you should usually use it as the return type instead.

Jon Skeet
thanks Jon. the method shown was an example only and thanks for providing an explanation.
Trent
+1  A: 

Use ? just for nullable ValueTypes.

odiseh
A: 

Being an out parameter or not is irrelevant here. But you cannot make a Nullable<T> with a class; T must be a struct. Otherwise the compiler will complain.

In addition to this, it is considered bad style to capitalise the name of a parameter (use exClass instead of ExClass). Your programs will work the same, but anybody reading your code might be misled.

Gorpik