© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
57 replies
__dil__

❔ Generics and nullable types

This snippet below results in an error

class Foo<T>
{
    T? _foo;

    Foo()
    {
        _foo = null; // Error here
    }
}
class Foo<T>
{
    T? _foo;

    Foo()
    {
        _foo = null; // Error here
    }
}

Cannot convert null to type parameter 'T' because it could be a value type. Consider using 'default(T)' instead.

I don't really understand the error. I thought the type of
_foo
_foo
was
T?
T?
, not
T
T
.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Default literal and non nullable reference types
C#CC# / help
11mo ago
Have nullable types been changed?
C#CC# / help
4mo ago
Don’t understand nullable reference types
C#CC# / help
4y ago
✅ Enum and generics
C#CC# / help
3y ago
Next page