-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d547eed
commit 2f1965d
Showing
2 changed files
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
namespace Hussy.Net; | ||
|
||
/// <summary> | ||
/// Defines static methods for common programming tasks. | ||
/// </summary> | ||
public static partial class Hussy | ||
{ | ||
/// <summary> | ||
/// Prompts the user with a message and reads the user's response, parsing it into the specified type. | ||
/// </summary> | ||
/// <typeparam name="T">The type to parse the user's response into.</typeparam> | ||
/// <param name="message">The message to display to the user.</param> | ||
/// <param name="formatProvider">The format provider to use for parsing the response. If null, the default format provider is used.</param> | ||
/// <returns>The parsed user's response of type T.</returns> | ||
public static T P<T>(string message, IFormatProvider? formatProvider = null) | ||
where T : IParsable<T> | ||
{ | ||
Console.Write(message); | ||
var response = Console.ReadLine(); | ||
if (!T.TryParse(response, formatProvider, out var parsedResponse)) | ||
throw new FormatException("Unable to parse response."); | ||
|
||
return parsedResponse; | ||
} | ||
} |