Last active
November 19, 2024 21:04
-
-
Save karenpayneoregon/65c5bea1db0f306bd300b5bbc609781e to your computer and use it in GitHub Desktop.
C# OverloadResolutionPriority Attribute
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
public partial class Demo | |
{ | |
[OverloadResolutionPriority(2)] | |
public static void DisplayInvoice(string invoice ) | |
{ | |
Console.WriteLine($"invoice: {NextValue(invoice)}"); | |
} | |
[OverloadResolutionPriority(-1)] | |
public static void DisplayInvoice(string invoice, int count = 3) | |
{ | |
var value = invoice; | |
for (var index = 0; index < count; index++) | |
{ | |
value = NextValue(value); | |
Console.WriteLine($"invoice: {value}"); | |
} | |
} | |
public static string NextValue(string sender, int incrementBy = 1) | |
{ | |
var value = NumbersPattern().Match(sender).Value; | |
return sender[..^value.Length] + (long.Parse(value) + incrementBy) | |
.ToString().PadLeft(value.Length, '0'); | |
} | |
[GeneratedRegex("[0-9]+$")] | |
private static partial Regex NumbersPattern(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment