C
C#•9mo ago
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?
8 Replies
Pobiega
Pobiega•9mo ago
make condition be nullable and default it to null then in the if, just check if its null before invoking it, or default that value to true thats what I do in my very similar method
public static T GetFromConsole<T>(string prompt, Func<T, bool>? validator = null,
string errorMessage = "Invalid value, try again.") where T : IParsable<T>
{
while (true)
{
Console.Write(prompt);
if (T.TryParse(Console.ReadLine(), null, out var value))
{
if (validator?.Invoke(value) ?? true)
{
return value;
}

Console.WriteLine(errorMessage);
continue;
}

Console.WriteLine("Invalid format, try again.");
}
}
public static T GetFromConsole<T>(string prompt, Func<T, bool>? validator = null,
string errorMessage = "Invalid value, try again.") where T : IParsable<T>
{
while (true)
{
Console.Write(prompt);
if (T.TryParse(Console.ReadLine(), null, out var value))
{
if (validator?.Invoke(value) ?? true)
{
return value;
}

Console.WriteLine(errorMessage);
continue;
}

Console.WriteLine("Invalid format, try again.");
}
}
Mass
Mass•9mo ago
that's perfect, thanks!
Pobiega
Pobiega•9mo ago
feel free to borrow the generic nature of this method btw 😛 it works for anything parsable, instead of just for ints you use it like var age = GetFromConsole<int>("How old are you?"); or specify int on the left, and it will be inferred on the right
Mass
Mass•9mo ago
Something like that was actually gonna be my next step, so thanks a lot! After rewriting my own code to look be more similar to yours, I have this:
T GetInput<T>(string prompt = "> ", Func<T, bool>? condition = null, string errMsg = "Please enter a valid value.") where T : IParsable<T> {
while (true) {
Console.Write(prompt);
bool valid = T.TryParse(Console.ReadLine(), null, out var value) && (condition?.Invoke(value) ?? true);
if (valid) return value;
else Console.WriteLine(errMsg);
}
}
T GetInput<T>(string prompt = "> ", Func<T, bool>? condition = null, string errMsg = "Please enter a valid value.") where T : IParsable<T> {
while (true) {
Console.Write(prompt);
bool valid = T.TryParse(Console.ReadLine(), null, out var value) && (condition?.Invoke(value) ?? true);
if (valid) return value;
else Console.WriteLine(errMsg);
}
}
My only concern now is that the value being returned might be null.
Angius
Angius•9mo ago
Now name the method properly and we're good
Mass
Mass•9mo ago
oh yeah I'm not used to C# naming conventions yet 😛
Angius
Angius•9mo ago
$structure for reference
MODiX
MODiX•9mo ago
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;
}
}
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;
}
}
Want results from more Discord servers?
Add your server