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

  • Home
  • SEARCH
  • 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 7421541
In Process

The Archive Base Latest Questions

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

I want to be able to call the generic foo method which has a

  • 0

I want to be able to call the generic foo method which has a Nullable param. When the body of the Foo method calls the Foo(), it goes recursive, resulting in a stack overflow (without the .com). Is there a way to have the call in the if statement call the correct overload with the runtime type?

I know changing the names would do it and I understand why the recursion is happening, but don’t know of another way to write this.

Thanks for looking.

class Program {
    static void Main(string[] args) {
        int? x = 5;
        int y = 10;
        Foo(x, y);
    }

    static void Foo<T>(Nullable<T> arg, T defaultValue) where T : struct {
        if (arg.HasValue)
            Foo(arg.Value, defaultValue);
    }

    static void Foo(int arg, int defaultValue) {
        Console.WriteLine(string.Format("I'm an int arg={0}, default={1]}", arg, defaultValue));
    }

    static void Foo(string arg, int defaultValue) {
        Console.WriteLine(string.Format("I'm an string arg={0}, default={1]}", arg, defaultValue));
    }

    static void Foo(bool arg, int defaultValue) {
        Console.WriteLine(string.Format("I'm an double arg={0}, default={1]}", arg, defaultValue));
    }
}
  • 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-29T08:25:42+00:00Added an answer on May 29, 2026 at 8:25 am

    Generics are a compile time feature, overload resolution has been decided before the program ever runs. If you want to, as you say, force a runtime decision, you could employ dynamic to achieve that goal (in C# 4+). Short of that, you simply must cast to the applicable type between int and bool otherwise.

      if (arg.HasValue) 
            Foo((dynamic)arg.Value, defaultValue); 
    

    But beware! This only solves your problem for int and bool. Other types could be passed to the generic method, and those will continue to resolve to the generic method on the subsequent call.

     Foo(5.0, 10.0); // would still overflow with your given methods
    

    Follow your initial judgment, if you want to support int, string, and bool, simply have those overloads. If you want to support any given T, create an overload for T.

    static void Foo<T>(T arg, T defaultValue)
    {
        Console.WriteLine(string.Format("I'm a T arg={0}, default={1}", arg, defaultValue));
    }
    

    Overload resolution will still pick int, bool, string, or T? overloads if they are applicable based upon compile time information. All other calls, and the call with arg.Value, will go to this new overload.

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

Sidebar

Related Questions

Summary I want to be able to call a JavaScript function from a Flex
I want to be able to make an HTTP call updating some select boxes
I have a generic method that has some parameter of a generic Type. What
I have this restfull service that I want to be able to call for
I want to be able to call Interfaces in my class, but have spring
I want to be able to call a function within an if statement. For
i just want to be able to call a javascritp subroutine on the client,
I'm trying to make a list of function calls. I want to be able
I want to call a generic handler function for all textBox on GotFocus and
I'm using LaTeX and BibTeX for an article, and I want to able to

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.