1

Since C# 8.0 nullable reference types are possible. If they are enabled you have to append the ? to the reference type to make it nullable.

My question now is how can I force a method parameter that uses the params keyword, to be not-nullable.

Example:

#nullable enable
public void MyMethod(params int[] foo)
{
    // foo can still be null
    // How can I ensure that foo is not null at compile-time
}

MyMethod(null); // valid, but why?

Another question which is similar, would be: Why is params int[] foo even nullable in the first place? I would expect something like params int[]? foo to make it nullable. But with params this seems to be different.

I tried NotNullAttribute but this only gives a warning. I want to disallow that completely by the compiler.

Does anybody know a way to make it non-nullable and maybe can tell me why params allows nullable even with #nullable enable?

Robert S.
  • 1,942
  • 16
  • 22
  • 3
    " I want to disallow that completely by the compiler." -> FYI, the nullable context only provides compile-time **warnings** (not errors), anything can still be null at run-time. There is no way to do this besides doing `if (x is not null)` – Camilo Terevinto Nov 18 '21 at 10:38
  • As above, just check for an error and throw an exception with a message. The caller of the method will receive this message and can then decide to check before calling the method or pass on the message to their caller. However, this will happen at run time only. – falopsy Nov 18 '21 at 11:30

3 Answers3

1

this will be valid too

MyMethod();

it has nothing to do with nullables. You use nullable a wrong way here. It is because you are using the params keyword.

From MSDN

"Using the params keyword, you can specify a method parameter that takes a variable number of arguments. The parameter type must be a single-dimensional array.

When you call a method with a params parameter, you can pass in:

....

No arguments. If you send no arguments, the length of the params list is zero. "

When you pass null, you are assigning null to arguments explicitly.

but if you want to use nullable and compiler wornings, make your code like this

#nullable enable
public void MyAnotherMethod()
{

MyMethod(null); // Compiller will issue a warning

MyMethod(); // No warning
}
Serge
  • 40,935
  • 4
  • 18
  • 45
1
  • valid, but why?

Parameter arrays where introduced before the nullable reference types and from the documentation it is valid to pass null as a value for such parameter:

A caller can then invoke the method in either of four ways:

  • By passing an array of the appropriate type that contains the desired number of elements.
  • By passing a comma-separated list of individual arguments of the appropriate type to the method.
  • By passing null.
  • By not providing an argument to the parameter array.
  • Does anybody know a way to make it non-nullable and maybe can tell me why params allows nullable even with #nullable enable?

If the call is made inside nullable enabled context compiler should issue a warning. You can change this warning into by changing compiler settings. See this question.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
0

Referencing to the MSDN concerning Nullable reference types

Known pitfalls

Arrays and structs that contain reference types are known pitfalls in nullable references and the static analysis that determines null safety. In both situations, a non-nullable reference may be initialized to null, without generating warnings.

So you can't make these types non-nullable because params is based on array.

Possible solution is to create your own non-nullable type or pass existing (which implements IEnumerable, for example). I think you should reach the same effect as with params.

Vadim
  • 96
  • 1
  • 8