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 8600799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:41:48+00:00 2026-06-12T01:41:48+00:00

Given a generic type T in C#, I wonder how to acquire type Q

  • 0

Given a generic type T in C#, I wonder how to acquire type Q, which is equal to T? for non-nullable T, and T for already nullable T.

The question arose from real code. I want to unify access to parameters passed through query string in my ASP.NET application. And I want to specify a default value of the same type, but ensure null can be passed as a default value.

public static T FetchValue<T>(
   string name, 
   <T? for non-nullable, T otherwise> default_value = null)  // How to write this?
{
  var page = HttpContext.Current.Handler as Page;

  string str = page.Request.QueryString[name];

  if (str == null)
  {
    if (default_value == null)
    {
      throw new HttpRequestValidationException("A " + name + " must be specified.");
    }
    else
    {
      return default_value;
    }
  }

  return (T)Convert.ChangeType(str, typeof(T));
}

Currently I’m forced having two overloads of the FetchValue – one without default value, and one with it:

public static T FetchValue<T>(string name);
public static T FetchValue<T>(string name, T default_value);

It works fine, but I wonder whether it is possible to merge both functions like this.

In C++ I would use type-traits, like PromoteNullable<T>::type with two specializations of PromoteNullable for both nullable and non-nullable types. But what about C#?

  • 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-06-12T01:41:50+00:00Added an answer on June 12, 2026 at 1:41 am

    Doesn’t directly answer the question as posed, but I’d write this:

        public static T FetchValue<T>(string name)
        {
            T value;
            if (TryFetchValue(name, out value))
                return value;
            throw new HttpRequestValidationException("A " + name + " must be specified.");
        }
    
        public static T FetchValue<T>(string name, T default_value)
        {
            T value;
            if (TryFetchValue(name, out value))
                return value;
            return default_value;
        }
    
        private static bool TryFetchValue<T>(
             string name,
             out T value)
        {
            var page = HttpContext.Current.Handler as Page;
    
            string str = page.Request.QueryString[name];
    
            if (str == null)
            {
                value = default(T);
                return false;
            }
    
            value = (T)Convert.ChangeType(str, typeof(T));
            return true;
        }
    

    So the bulk of the code exists only once – and you can even now actually have the calling code choose to have null as a default value, if it so chooses.


    Even if you could create the parameter declaration you wanted, this line would still be an issue:

    return default_value;
    

    If it turned out that default_value was a T? rather than a T, then the above code doesn’t work. Even if you do a cast:

    return (T)default_value;
    

    there’s still an issue – that to cast from T? to T, the compiler actually has to insert a call to obtain the Value property of the nullable. But that call wouldn’t be valid if the type of default_value was just T.

    In C# Generics, the compiler has to create one piece of IL for the method. There’s no way to insert an optional piece of code that may access Value.

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

Sidebar

Related Questions

Given a Generic IList of some type, which contains a number of items, is
I have a class which has a property of generic type as given below.
This question is partly about delegates, and partly about generics. Given the simplified code:
Trying to find out if a provided Type is of a given generic type
Given a generic type with constraints: class MyClass<T> where T: Alpha { } and
Is there a way to declare a vaiable for an open generic type? Given:
Given a generic type TMyClass <T> = class ... end; is there a way
Given the following code from RssToolkit in RssXmlHelper.cs: /// <summary> /// Returns XML of
In C#, given a generic type such as this: interface IGenericType<T> where T :
My professor has given me this assignment. Implement a generic function called Max, which

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.