Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6591917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:29:14+00:00 2026-05-25T17:29:14+00:00

I have an interesting problem. I’d like to create a generic class that can

  • 0

I have an interesting problem. I’d like to create a generic class that can deal with both Reference types as well as Nullable<T> types. Basically I want something like:

public class ClassWithNull<T>
{
    public T varName = null;
}

Now this, of course, does not compile because not all types can be assigned null, namely non-nullable value types. But the problem is Nullable<T> is a value type, so simply adding where T : class doesn’t help me. My generics-foo is not too strong, but I haven’t been able to find any way to say that T must either be a reference type or a nullable value type.

The idea I have to solve this is to make ClassWithNull<T> an abstract class. I could then add two sub-classes, one to deal with reference types and one to deal with nullable value types. Then, a static factory method in the base class could use reflection to determine which sub-class ought to be constructed. Something like:

public static ClassWithNull<T> CreateClassWithNull<T>()
{
    StackTrace st = new StackTrace();
    Type type = st.GetFrame(1).GetMethod().GetGenericArguments()[0];
    if (!type.IsValueType)
    {
        return new ClassWithReferenceType<T>();
    }
    else if (type == typeof(Nullable))
    {
        return new ClassWithNullableValueType<T>();
    }
    else
    {
        throw new Exception("Must provide nullable type.");
    }
}

The problem here is that generics are resolved statically. If ClassWithReferenceType<U> expects U to be a reference type, then calling new ClassWithReferenceType<T>() in the factory method is a compilation error since T is not required to be a reference type. The compiler does not know about the run time check.

Any ideas about how to implement such a thing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:29:15+00:00Added an answer on May 25, 2026 at 5:29 pm

    How about:

    public class ClassWithNull<T>
    {
        public T varName = default(T);
    }
    

    (Actually, you don’t even need the assignment – you can just leave it to be the default value on construction. But you might want default(T) for local variables.)

    That won’t stop you from using it incorrectly with a non-nullable value type – but is that enough?

    If that doesn’t help you, I would suggest writing two static methods, like this:

    public static ClassWithNull<T> CreateClassWithNullForClass<T> where T : class
    {
        return new ClassWithReferenceType<T>();
    }
    
    public static ClassWithNull<T> CreateClassWithNullForNullable<T> where T : struct
    {
        return new ClassWithNullableValueType<T>();
    }
    

    The field in ClassWithNullableValueType would be Nullable<T> – T would be the underlying type.

    Now if you want overloads of the same method, that gets a little harder, particularly if you don’t want to pass any parameters. It’s possible, but really, really horrible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

He're an interesting problem that looks for the most Pythonic solution. Suppose I have
I have an interesting problem. Consider this class hierachy: class Base { public: virtual
I have an interesting problem, and I can't seem to figure out the lambda
I have an interesting problem with XSL. We have an XML Input file that
I have an interesting problem.. I have a Form that launches another Form (2)
I have an interesting problem. I currently have a basic template library that renders
I have an interesting problem to solve here that may require some creative direction.
I have an interesting problem that I'm trying to solve and was wondering if
I have an interesting problem and would appreciate your thoughts for the best solution.
I have one interesting problem. I must parse mail body (regular expression), get some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.