Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argument type null is not assignable to parameter type

Tags:

c#

null

generics

I'm trying to develop a generic BoundedList class, for which I've created a generic BoundedListNode class. My BoundedList class is as follows:

class BoundedList<TE>
{
    private BoundedListNode<TE> _startNode;
    private BoundedListNode<TE> _lastUsedNode;
    protected Dictionary<TE, BoundedListNode<TE>> Pointer;

    public BoundedList(int capacity)
    {
        Pointer = new Dictionary<TE, BoundedListNode<TE>>(capacity);
        _startNode = new BoundedListNode<TE>(null);
        _lastUsedNode = _startNode;
    }
}

In the constructor at the _startNode I'm getting the error "Argument type null is not assignable to parameter type TE".

How can I assign null in this case?


1 Answers

You need to tell the compiler TE is a class, meaning a reference type. With an unbounded type, TE can also be a value type, which isn't assignable to null:

public class BoundedListNode<TE> where TE : class

Then, you'll be able to assign null as an argument in your constructor:

like image 96
Yuval Itzchakov Avatar answered May 12 '26 05:05

Yuval Itzchakov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!