3

Would like to implement a method like the following by using the C#8.0 Nullable Check feature:

public static T Min<T>(IEnumerable<T> objects) where T : IComparable<T>
{
    bool first = true;
    T result = default; // <---- CS8653

    foreach (var obj in objects)
    {
        // Bla Bla
    }

    return result;
}

I get an error at the marked line (fourth line):

MathHelper.cs(129,24,129,31): warning CS8653: Ein Standardausdruck führt einen NULL-Wert ein, wenn "T" ein Verweistyp ist, der keine NULL-Werte zulässt.

English translation: A default value introduces a null value, if T is a reference type which does not allow null values...

But now the challenge:

1) I'd like to have this function for 'structs' and for 'classes', so introducing "where class" or "where struct" is not an option.

2) T? result = default givers an error:

CS8627 Ein Nullable-Typparameter muss als Werttyp oder als Nicht-Nullable-Verweistyp bekannt sein. Sie sollten eine class-, struct- oder eine Typeinschränkung hinzufügen. BurnSystems

3) T! result = default also does not work..,..

Anybody having an idea how to get the nullable check feature running for generic functions which allow a reference and value type while the reference value may be null....


EDIT: The project file:

<PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <LangVersion>8</LangVersion>
    <Nullable>enable</Nullable>
</PropertyGroup>
DevEstacion
  • 1,947
  • 1
  • 14
  • 28
UllaDieTrulla
  • 573
  • 1
  • 5
  • 18
  • with the given code, I don't get this warning (.NET Core 3 / C# 8 ) – Keith Nicholas Oct 15 '19 at 20:23
  • I think you can do `T? result = default` if you add the `notnull` constraint. – juharr Oct 15 '19 at 20:25
  • https://ibb.co/gTpq4xb - Here, the screenshot... @juharr: > public static T Min(IEnumerable objects) where T : IComparable, notnull gives an error at 'notnull' - "Cannot resolve symbol 'notnull'" – UllaDieTrulla Oct 15 '19 at 20:31
  • also, if the result of this is going to be a "Min" getting default is not going to be useful – Keith Nicholas Oct 15 '19 at 20:31
  • my project targets ```netcoreapp3.0``` so maybe something about targeting std2.0 is the problem – Keith Nicholas Oct 15 '19 at 20:37
  • What does `CS8627 Ein Nullable-Typparameter muss als Werttyp oder als Nicht-Nullable-Verweistyp bekannt sein. Sie sollten eine class-, struct- oder eine Typeinschränkung hinzufügen. BurnSystems` translate to in English? – mjwills Oct 15 '19 at 20:37
  • Hi, just trying netstandard2.1 since I would like to use the library also for .Net Framework legacy application using the library... Just a test baloon... But this also means that I have to switch to .Net Framework 4.8 for the referencing applications. – UllaDieTrulla Oct 15 '19 at 20:39
  • **default!** Was never aware of this. Looks a bit strange and creates struggle in the methods using the Min-Function (since they can't see that the return value might be null) Thanks... Using .Net Framework 2.1 did not help a lot. `notnull` does not work. @mjwills: The translation means: That the nullable type parameter shall be known as value or as non-nullable reference value... I should add a where struct or where class. – UllaDieTrulla Oct 15 '19 at 20:45
  • 1
    Do you really need to initialize that temporary result variable with the default value ? Why don't you initialize it with the first item of the collection ? (And when the collection does not contain any items, I think it is appropriate to throw an exception). – Frederik Gheysels Oct 15 '19 at 20:49
  • Hi, this was just an example. I could find another method, where your proposal might not be appropriate... The method should have been resilient against empty enumerations. It was more about understanding nullables and reference/value generics... I'm a bit surprised that Microsoft does not have a clearer definition for default. – UllaDieTrulla Oct 15 '19 at 20:52
  • Can you use the `class?` constraint: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/nullable-reference-types#generics – Frederik Gheysels Oct 15 '19 at 20:55

0 Answers0