-1

I'm trying to write a generic method which takes in a list of XLCellValue objects and return a list of nullable generic types. First I tried this:

T? ConvertColumnToNullableType<T?>(List<XLCellValue> inputList)
{
    List<T?> outputList = new List<T?>();
    
    return outputList;
}

Which threw the error below:

error CS1003: Syntax error, ',' expected

Then I looked around a bit and tried this:

List<Nullable<T>> ConvertColumnToNullableType<T>(List<XLCellValue> inputList) where T : class
{
    List<T> outputList = new List<T>();
    
    return outputList;
}

Which didn't work either:

(1,19): error CS0453: The type 'T' must be a non-nullable value type in order to use it as > parameter 'T' in the generic type or method 'Nullable'

(5,12): error CS0029: Cannot implicitly convert type 'System.Collections.Generic.List' > to 'System.Collections.Generic.List<T?>'

What am I doing wrong here and how can I fix it?

user32882
  • 5,094
  • 5
  • 43
  • 82
  • Not sure what your method is supposed to do, but the original syntax error can be fixed by removing the `?` in the type parameters list: `T? ConvertColumnToNullableType(List inputList)` – Sweeper Apr 20 '23 at 11:30
  • Didn't work for me. Could you flesh out a full answer? – user32882 Apr 20 '23 at 11:32
  • Which version of C# you are using? with C# that supports nullable I got `CS1003 Syntax error, ',' expected` with first code ... and with C# which doesn't I've got your error – Selvin Apr 20 '23 at 11:34
  • @Selvin I'm using C# from a jupyter notebook using .NET interactive. How can I determine that? – user32882 Apr 20 '23 at 11:36
  • In short it is not possible for method not accepting `T` as a parameter unless you restrict `T` to be `class` or `struct`. Check out [this answer](https://stackoverflow.com/a/72911969/2501279) – Guru Stron Apr 20 '23 at 11:38
  • Do you want nullable structs or nullable reference types? – Charlieface Apr 20 '23 at 11:39
  • *How can I determine that?* `#error version` ... it will not work for Framework ... only for .net x – Selvin Apr 20 '23 at 11:41
  • @Selvin `Compiler version: '4.4.0-6.22561.11 (1ce8866c)'. Language version: 11.0.` – user32882 Apr 20 '23 at 11:42

1 Answers1

1

I think what you want is

List<T?> ConvertColumnToNullableType<T>(List<XLCellValue> inputList)
    where T : class
{
    List<T?> outputList = new List<T?>();

    return outputList;
}

this will allow the method to return a list of generic type that is a class but your list can contain null values.

To fix the second example, in order to use Nullable you would have to require T to not be a class - a struct:

List<Nullable<T>> ConvertColumnToNullableType<T>(List<XLCellValue> inputList) 
    where T : struct
{
    List<T?> outputList = new List<T?>();

    return outputList;
}
Rafal
  • 12,391
  • 32
  • 54
  • Your first proposition fails with `The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.`. The second one constrains to `struct` where my OP explicitly asks about reference types. – user32882 Apr 20 '23 at 11:35
  • @user32882 you need to enable nullable reference types. For example `#nullable enable` – Guru Stron Apr 20 '23 at 11:35
  • @GuruStron I think that might be the problem, but now I need to figure out how to do that for a .NET interactive session... – user32882 Apr 20 '23 at 11:37
  • @user32882 try `#nullable enable` – Guru Stron Apr 20 '23 at 11:39
  • @user32882 for first example you can drop the question mark entirely it will not change the code behavior. Question mark next to reference types is rather compilation time validation feature than entitling that you would care about on interactive session – Rafal Apr 20 '23 at 11:39
  • @GuruStron that command doesn't fail but then I still can't do something as simple as `string? test = "fsdafds";` I still get the `'#nullable' annotations context` error – user32882 Apr 20 '23 at 11:41
  • @user32882 just do not use question marks next to reference types :) – Rafal Apr 20 '23 at 11:42