Unsuccessful and generics

I am trying to create a generic node class that can contain any number of child nodes, a string for the node key and node data, which may or may not be null. However, I am having trouble getting the syntax of the right to accept a generic value as a generic parameter for Nullable.

internal class TrieNode<E>
{
    Nullable<E> Data;
    string Key;
    List<TrieNode<E>> Children;

    public TrieNode(E? data, string key)
    {
        Data = data;
        Key = key;
        Children = new List<TrieNode<E>>();
    }
}

At compile time, I get the following error message:

Type "E" must be an unimaginable value type in order to use it as the "T" parameter in a generic type or "System.Nullable" method

Is there a way to guarantee that E is a type that does not contain NULL, or in any other way?

+3
source share
3 answers

You just need to:

internal class TrieNode<E> where E : struct

: struct E Nullable<>, Nullable<E> E?.

+9

, TrieNode, E: struct

, Nullable , ( )

0

Why not just declare node to use object? An object can refer to any data type and can also be null.

-2
source

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


All Articles