© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
14 replies
Mass

How to set a default method for an optional parameter in a method.

I've been trying to google this frantically because I'm sure this question has been asked by others before, but all the results are just for optional parameters in general (and none of the examples are passing methods).
Here's my method example:
int getIntegerInput(Func<int, bool> condition) {
  bool valid;
  while (true) {
      Console.Write("> ");
      string input = Console.ReadLine() ?? "";
      valid = int.TryParse(input, out int value) && condition(value);
      if (valid) return value;
      else Console.WriteLine(errMsg);
  }
}
int getIntegerInput(Func<int, bool> condition) {
  bool valid;
  while (true) {
      Console.Write("> ");
      string input = Console.ReadLine() ?? "";
      valid = int.TryParse(input, out int value) && condition(value);
      if (valid) return value;
      else Console.WriteLine(errMsg);
  }
}

All I want is to allow the method to be called without any parameters, in which case condition would just always return true.
Right now I have to call the method like this:
getIntegerInput((input) => { return true; });
getIntegerInput((input) => { return true; });

Basically I want the lambda above to be the default value for "condition" in the method. But simply assigning it as the default isn't allowed.
What is the proper syntax for setting a method as a default parameter?
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

set default value of parameter to other parameter [Answered]
C#CC# / help
4y ago
❔ Optional function parameter
C#CC# / help
3y ago