The as operator must be used with a reference type or nullablepe or nullable type ('long' is a non-n

public void InformNetwork(Vetr eventName, params object[] adder) {
    switch (eventName)
    {
        case Vetr.CLOSED:
            long connId = adder[0] as long ?? throw new Exception("Empty or invalid connId (Connection ID)");

I’m looking for a single liner to grab a variable, cast it to a type (in this case long) and throw if it’s either null or cast/assignment is not successful. I thought that was a way to do it but for some reason it fails. It won’t even compile with following errors in the console:
error CS0077: The as operator must be used with a reference type or nullable type ('long' is a non-nullable value type)

Welp, I’m an idiot, forgot ? after long. So now it’s:
long connId = adder[0] as long? ?? throw and works.

1 Like