C
C#ā€¢9mo ago
Mekasu0124

āœ… Learning Regex

string errorText = "Username Must Be Created Using The Letters A-Z, a-z, 0-9, or special characters !@#$%^&*";
string regex = // something here
string username = ColorCon.GetStringFromConsole(
prompt: "Create A Username:",
color: ConsoleColor.White,
validator: x => x.IsMatch(regex),
errorMessage: errorText);
string errorText = "Username Must Be Created Using The Letters A-Z, a-z, 0-9, or special characters !@#$%^&*";
string regex = // something here
string username = ColorCon.GetStringFromConsole(
prompt: "Create A Username:",
color: ConsoleColor.White,
validator: x => x.IsMatch(regex),
errorMessage: errorText);
I've never learned to create regex strings for validation, and I'm wanting to learn how to use/create them. I've used them before, but it was just a mere "<something_here> regex validation" on google, copy/paste and go. 1. What is a regex string 2. How do I write them to get the format that I'm wanting? 3. For this specific purpose, the username should be able to be something like someUsername, some$Username, or SomEU123sername!$ Thanks
76 Replies
Mekasu0124
Mekasu0124ā€¢9mo ago
public static string ValidateUsernameInput(string input)
{
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
string numbers = "0123456789";
string special = "!@#$%^&*";

foreach (char letter in input)
{
if (!alphabet.Contains(letter) || !numbers.Contains(letter) || !special.Contains(letter))
{
Console.WriteLine("[Error] Invalid Input. Input Should Only Be English Letters" +
"A-Z or 0-9");
Console.Write("Your Input: ");
input = Console.ReadLine();
}
}

return input;
}
public static string ValidateUsernameInput(string input)
{
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
string numbers = "0123456789";
string special = "!@#$%^&*";

foreach (char letter in input)
{
if (!alphabet.Contains(letter) || !numbers.Contains(letter) || !special.Contains(letter))
{
Console.WriteLine("[Error] Invalid Input. Input Should Only Be English Letters" +
"A-Z or 0-9");
Console.Write("Your Input: ");
input = Console.ReadLine();
}
}

return input;
}
for reference, this is how I currently validate usernames, and I just want to become more efficient with it
Pobiega
Pobiegaā€¢9mo ago
1: regex is a pattern matching "language"/system that can be used in most if not all programming languages 2: You'll need to learn regex to do that šŸ™‚ I highly recommend https://regex101.com/
regex101
regex101: build, test, and debug regex
Regular expression tester with syntax highlighting, explanation, cheat sheet for PHP/PCRE, Python, GO, JavaScript, Java, C#/.NET, Rust.
Pobiega
Pobiegaā€¢9mo ago
it lets you see what your regex matches as you write it
Mekasu0124
Mekasu0124ā€¢9mo ago
ty šŸ™‚
Pobiega
Pobiegaā€¢9mo ago
you'll need to be more specific about what you want your regex to validate thou. Just the characters being used? or "at least one special char, one number, one capital and one small letter" etc? granted, imho thats not a good usecase for a regex
Mayor McCheese
Mayor McCheeseā€¢9mo ago
It's nice they support .net regex now, that wasn't the case until recently I think.
Pobiega
Pobiegaā€¢9mo ago
yup, they added it recently together with rust and java 8
Mekasu0124
Mekasu0124ā€¢9mo ago
so playing with the site for a few seconds, I put in this username and got this
No description
Mekasu0124
Mekasu0124ā€¢9mo ago
I do want it to be at least 1 char, 1 int, and 1 special
Pobiega
Pobiegaā€¢9mo ago
yeah you can see on the right in the "match information" that its matching each character as its own group thats probably not what you want I'd probably just use normal C# for that tbh
Mekasu0124
Mekasu0124ā€¢9mo ago
however, with this, I'm still not fully understanding so I want to try and see if I do understand 1. [A-Z] => matches case sensitive letters that are uppercase only 2. [a-z] => matches case sensitive letters that are lowercase only 3. [0-9] => matches numbers zero through nine 4. [!@#$%^&*] => matches these special characters case sensitive right?
Pobiega
Pobiegaā€¢9mo ago
regex is good at finding and extracting patterns from strings
Mayor McCheese
Mayor McCheeseā€¢9mo ago
regular expressions are great, they're good to know, but good to know when to use them is good too.
Mekasu0124
Mekasu0124ā€¢9mo ago
so apart from emails, when are they good to use?
Pobiega
Pobiegaā€¢9mo ago
the first 3, yes. some of those special chars might have special meanings in regex, depending on flavor and position finding and extracting patterns from strings
Mekasu0124
Mekasu0124ā€¢9mo ago
would you mind to give an example of that? ^ so it would be better for me to use how I currently do it already, but given I were to do that, how would I modify that function to account for at least 1 char, 1 int, and 1 special? use int variables for counts and return if each count is at least 1?
Pobiega
Pobiegaā€¢9mo ago
I'd probably just loop over each char in the string and set some bools to true when I find the right conditions
Mayor McCheese
Mayor McCheeseā€¢9mo ago
Regex is actually pretty bad for checking emails tbh, it can be a part of validating an email; but you can't rely on regex for many forms of validation alone.
Pobiega
Pobiegaā€¢9mo ago
public static bool IsValidPassword(string s)
{
var hadSymbol = false;
var hadNumber = false;
var hadUppercase = false;

foreach (var c in s)
{
if (char.IsNumber(c))
{
hadNumber = true;
}
...
}

return hadNumber && hadSymbol && hadUppercase;
}
public static bool IsValidPassword(string s)
{
var hadSymbol = false;
var hadNumber = false;
var hadUppercase = false;

foreach (var c in s)
{
if (char.IsNumber(c))
{
hadNumber = true;
}
...
}

return hadNumber && hadSymbol && hadUppercase;
}
Mekasu0124
Mekasu0124ā€¢9mo ago
oh ok so what I was imagining. An if/elif/else
Pobiega
Pobiegaā€¢9mo ago
as for a valid usecase for regex... https://github.com/logpai/loghub/blob/master/Linux/Linux_2k.log look at this file just a sample log file if we wanted to quickly extract the user and the id from each row, regex would be a decent option
Pobiega
Pobiegaā€¢9mo ago
No description
Pobiega
Pobiegaā€¢9mo ago
for example
Murten
Murtenā€¢9mo ago
Microsoft also has a documentation page on regex if that's more your style.
Mekasu0124
Mekasu0124ā€¢9mo ago
this is definitely a lot
public static bool ValidateUsernameInput(string input)
{
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
string numbers = "0123456789";
string special = "!@#$%^&*";
bool hasLet = false;
bool hasNum = false;
bool hasSpec = false;

foreach (char letter in input)
{
if (alphabet.Contains(letter))
{
hasLet = true;
}
if (numbers.Contains(letter))
{
hasNum = true;
}
if (special.Contains(letter))
{
hasSpec = true;
}
if (hasLet && hasNum && hasSpec) => return;
}
}
public static bool ValidateUsernameInput(string input)
{
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
string numbers = "0123456789";
string special = "!@#$%^&*";
bool hasLet = false;
bool hasNum = false;
bool hasSpec = false;

foreach (char letter in input)
{
if (alphabet.Contains(letter))
{
hasLet = true;
}
if (numbers.Contains(letter))
{
hasNum = true;
}
if (special.Contains(letter))
{
hasSpec = true;
}
if (hasLet && hasNum && hasSpec) => return;
}
}
I feel like I'm way off with this
Pobiega
Pobiegaā€¢9mo ago
no that seems fine, except maybe that return statement at the end
Mekasu0124
Mekasu0124ā€¢9mo ago
so it just needs to return a bool
Pobiega
Pobiegaā€¢9mo ago
and for the love of god remove the recursion >_> a validation method should only validate its not responsible for also taking input and retrying until its valid if you want that, make that its own method that uses the validation you've seen how I do that with ColorCon etc
Mekasu0124
Mekasu0124ā€¢9mo ago
I fixed it
Pobiega
Pobiegaā€¢9mo ago
nah that entire
else
{
Console.WriteLine("[Error] Invalid Input. Input Should Be A-Z, 0-9, or \"!@#$%^&*\" ");
Console.Write("Your Input: ");
input = Console.ReadLine();
}
else
{
Console.WriteLine("[Error] Invalid Input. Input Should Be A-Z, 0-9, or \"!@#$%^&*\" ");
Console.Write("Your Input: ");
input = Console.ReadLine();
}
block needs to go
Mekasu0124
Mekasu0124ā€¢9mo ago
it's just there for example usage. I have the ColorCon in the program
Mayor McCheese
Mayor McCheeseā€¢9mo ago
Whenever ( hasLet && hasNum && hasSpec) is true you can stop btw
Mekasu0124
Mekasu0124ā€¢9mo ago
what about this I removed it
Pobiega
Pobiegaā€¢9mo ago
yep a lot better, slap some continue; in each of the matching blocks if it was a number, we dont need to check if it was also a special character (as McCheese said) alternatively use else ifs
Mekasu0124
Mekasu0124ā€¢9mo ago
public static bool ValidateUsernameInput(string input)
{
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
string numbers = "0123456789";
string special = "!@#$%^&*";
bool hasLet = false;
bool hasNum = false;
bool hasSpec = false;

foreach (char letter in input)
{
if (alphabet.Contains(letter))
{
hasLet = true;
continue;
}
if (numbers.Contains(letter))
{
hasNum = true;
continue;
}
if (special.Contains(letter))
{
hasSpec = true;
continue;
}
}
return hasLet && hasNum && hasSpec;
}
public static bool ValidateUsernameInput(string input)
{
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
string numbers = "0123456789";
string special = "!@#$%^&*";
bool hasLet = false;
bool hasNum = false;
bool hasSpec = false;

foreach (char letter in input)
{
if (alphabet.Contains(letter))
{
hasLet = true;
continue;
}
if (numbers.Contains(letter))
{
hasNum = true;
continue;
}
if (special.Contains(letter))
{
hasSpec = true;
continue;
}
}
return hasLet && hasNum && hasSpec;
}
Pobiega
Pobiegaā€¢9mo ago
uhm if (hasLet && hasNum && hasSpec) => return; ???? :d what the fuck is going on there lol
Mekasu0124
Mekasu0124ā€¢9mo ago
I'm a tad confused here. Why wouldn't I check if a special character if the requirements are at least 1 number, 1 letter, and 1 special character? it was an attempt at this https://discord.com/channels/143867839282020352/1162384486395281419/1162389881675333672
MODiX
MODiXā€¢9mo ago
Mayor McCheese
Whenever ( hasLet && hasNum && hasSpec) is true you can stop btw
Quoted by
<@1032644230432182302> from #Learning Regex (click here)
React with āŒ to remove this embed.
Pobiega
Pobiegaā€¢9mo ago
well, we check each character the return should be after the loop a single character cant be both a number and a letter so if it was a number, we say "yeah we found at least one number" and we carry on
Mekasu0124
Mekasu0124ā€¢9mo ago
doing this with the loop, right? fixed that
Pobiega
Pobiegaā€¢9mo ago
yes exactly
Mekasu0124
Mekasu0124ā€¢9mo ago
right which is why there are 3 ifs, correct?
Pobiega
Pobiegaā€¢9mo ago
the loop is for each char in the string right
Mekasu0124
Mekasu0124ā€¢9mo ago
which is the reason for the continues
Pobiega
Pobiegaā€¢9mo ago
yes continue here means we move to the next c in s
Mekasu0124
Mekasu0124ā€¢9mo ago
right
Pobiega
Pobiegaā€¢9mo ago
or letter in input using your names
Mekasu0124
Mekasu0124ā€¢9mo ago
sweet. well thank you again for your help. sorry about the recursion ^_^
Murten
Murtenā€¢9mo ago
You could also use input.Any(char.IsDigit).
Mekasu0124
Mekasu0124ā€¢9mo ago
I actually have one more question. I can't seem to find which built-in method checks if a (letter) is either capitol or lower-case
Pobiega
Pobiegaā€¢9mo ago
you could, but that would loop over the string 3 times (assuming you want to check for letter, number and special char)
Mekasu0124
Mekasu0124ā€¢9mo ago
no lie, I typed in google IsDigit c# and couldn't find anything lol
Pobiega
Pobiegaā€¢9mo ago
if (char.IsUpper(c))
{
hadUppercase = true;
}
if (char.IsUpper(c))
{
hadUppercase = true;
}
šŸ™‚
Murten
Murtenā€¢9mo ago
Oof, usually yields more results if you search for your problem instead of a possible solution.
Mekasu0124
Mekasu0124ā€¢9mo ago
ok bet because I want to shorten string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; so that the validation can check whether the letter matches regardless to it's upper/lower case standpoint
Murten
Murtenā€¢9mo ago
char.IsLetter
arion
arionā€¢9mo ago
const string USERNAME = "Spiderman123#";


Console.WriteLine($"Username '{USERNAME}'is {(Matches(USERNAME.AsSpan(),
char.IsLetter,
char.IsDigit,
char.IsPunctuation,
char.IsUpper)
? "Valid" : "Invalid")}");



return;


static bool Matches<T>(ReadOnlySpan<T> array, params Predicate<T>[] predicates)
{
var solved = new List<Predicate<T>>();
foreach (var arrayValue in array)
{
foreach (var predicate in predicates)
{
if (solved.Contains(predicate))
continue;

if (predicate(arrayValue))
solved.Add(predicate);
}
}

return solved.Count == predicates.Length;
}
const string USERNAME = "Spiderman123#";


Console.WriteLine($"Username '{USERNAME}'is {(Matches(USERNAME.AsSpan(),
char.IsLetter,
char.IsDigit,
char.IsPunctuation,
char.IsUpper)
? "Valid" : "Invalid")}");



return;


static bool Matches<T>(ReadOnlySpan<T> array, params Predicate<T>[] predicates)
{
var solved = new List<Predicate<T>>();
foreach (var arrayValue in array)
{
foreach (var predicate in predicates)
{
if (solved.Contains(predicate))
continue;

if (predicate(arrayValue))
solved.Add(predicate);
}
}

return solved.Count == predicates.Length;
}
Username 'Spiderman123#'is Valid
Generics are nice
MODiX
MODiXā€¢9mo ago
Mekasu0124
REPL Result: Success
string username = "mekasu0124!";

foreach (char letter in username)
{
if (char.IsLetter(letter))
{
Console.WriteLine(letter + " is a letter");
}
else if (char.IsNumber(letter))
{
Console.WriteLine(letter + " is a number");
}
else if (char.IsSymbol(letter))
{
Console.WriteLine(letter + " is a symbol");
}
}
string username = "mekasu0124!";

foreach (char letter in username)
{
if (char.IsLetter(letter))
{
Console.WriteLine(letter + " is a letter");
}
else if (char.IsNumber(letter))
{
Console.WriteLine(letter + " is a number");
}
else if (char.IsSymbol(letter))
{
Console.WriteLine(letter + " is a symbol");
}
}
Console Output
m is a letter
e is a letter
k is a letter
a is a letter
s is a letter
u is a letter
0 is a number
1 is a number
2 is a number
4 is a number
m is a letter
e is a letter
k is a letter
a is a letter
s is a letter
u is a letter
0 is a number
1 is a number
2 is a number
4 is a number
Compile: 650.267ms | Execution: 60.476ms | React with āŒ to remove this embed.
Mekasu0124
Mekasu0124ā€¢9mo ago
why doesn't it recognize that an exclamation point is a symbol?
arion
arionā€¢9mo ago
char.IsPunctuation is what you should use it doesnt recognize many like @ # $ or %
MODiX
MODiXā€¢9mo ago
arion
REPL Result: Success
char.IsPunctuation('&') && char.IsPunctuation('#')
char.IsPunctuation('&') && char.IsPunctuation('#')
Result: bool
True
True
Compile: 429.229ms | Execution: 31.244ms | React with āŒ to remove this embed.
Mekasu0124
Mekasu0124ā€¢9mo ago
oh ok. I got you. thanks šŸ™‚
arion
arionā€¢9mo ago
For symbols they mean Unicode symbols like Ā© (Copyright sign)
Mekasu0124
Mekasu0124ā€¢9mo ago
oh ok that makes sense
arion
arionā€¢9mo ago
Though there is an issue of $ not being punctuation and being a symbol, so you could do char.IsPunctuation(c) || char.IsSymbol(c)
Mekasu0124
Mekasu0124ā€¢9mo ago
easy enough. makes sense. ty šŸ™‚
public static bool IsValidUsername(string s)
{
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
string numbers = "0123456789";

bool hasCapLet = false;
bool hasNum = false;

foreach (char letter in s)
{
if (char.IsUpper(letter))
{
hasCapLet = true;
}
else if (char.IsNumber(letter))
{
hasNum = true;
}
}

return hasCapLet && hasNum;
}

string username = ColorCon.GetStringFromConsole(
prompt: "Enter A Username: ",
color: ConsoleColor.Cyan,
validator: x => Helpers.IsValidUsername(x),
errorMessage: "Invalid Input. Username Must Be At Least 1 Capitol Letter and 1 Number");
public static bool IsValidUsername(string s)
{
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
string numbers = "0123456789";

bool hasCapLet = false;
bool hasNum = false;

foreach (char letter in s)
{
if (char.IsUpper(letter))
{
hasCapLet = true;
}
else if (char.IsNumber(letter))
{
hasNum = true;
}
}

return hasCapLet && hasNum;
}

string username = ColorCon.GetStringFromConsole(
prompt: "Enter A Username: ",
color: ConsoleColor.Cyan,
validator: x => Helpers.IsValidUsername(x),
errorMessage: "Invalid Input. Username Must Be At Least 1 Capitol Letter and 1 Number");
could I combine the two for efficiency?
arion
arionā€¢9mo ago
hmm, @Mekasu0124 are u a student by any chance? if so you can get Jetbrains Rider or Resharper for free via student plans A typical thing resharper would recommend is called "Replace with method group"
validator: x => Helpers.IsValidUsername(x),
validator: x => Helpers.IsValidUsername(x),
becomes
validator: Helpers.IsValidUsername,
validator: Helpers.IsValidUsername,
alphabet is unused, numbers too, also what do you mean by "combine the two"? the two if checks? not really since they check for distinct things, you could do something like
if (hasCapLet && hasNum)
return true;
if (hasCapLet && hasNum)
return true;
Mekasu0124
Mekasu0124ā€¢9mo ago
hmm, <ping> are u a student by any chance?
I am self-taught. I am a student going for my Bachelor's in Computer Science with a focus in Software Development and Cyber Security.
If so you can get Jetbrains Rider or Resharper for free via student plans
I don't like jetbrains anything and I don't know what resharper is
A typical thing resharper would recommend is called "Replace with method group" validator: x => Helpers.IsValidUsername(x), becomes validator: Helpers.IsValidUsername,
public static string GetStringFromConsole(
string prompt,
ConsoleColor color,
Func<string, bool>? validator = null,
string? errorMessage = null,
ConsoleColor errorColor = ConsoleColor.Red)
{
while (true)
{
Write(prompt, color);
var input = Console.ReadLine();

if (input != null && (validator?.Invoke(input) ?? true))
{
return input;
}

WriteLine(errorMessage ?? "Bad Input, Try Again", errorColor);
}
}
public static string GetStringFromConsole(
string prompt,
ConsoleColor color,
Func<string, bool>? validator = null,
string? errorMessage = null,
ConsoleColor errorColor = ConsoleColor.Red)
{
while (true)
{
Write(prompt, color);
var input = Console.ReadLine();

if (input != null && (validator?.Invoke(input) ?? true))
{
return input;
}

WriteLine(errorMessage ?? "Bad Input, Try Again", errorColor);
}
}
I didn't write the function which is why I replied to pobiega. I know he's probably asleep but he's been helping me make what I've already done more efficient which is why I asked if it would be efficient to call a method within the validator.
alphabet is unused, numbers too,
yea I know. I removed them, just didn't remove them here.
also what do you mean by "combin the two"? the two if checks? not really since they check for distinct things, you could something like if (hasCapLet && hasNum) => return true;
the validator is a true/false check so if I call a function that returns true/false then I should be able to call it. I ran it in a repl.it (see screenshot) and it worked as expected with returning a single true or a single false.
No description
arion
arionā€¢9mo ago
like this?
public class Program
{
public static void Main(string[] args)
{
string u1 = "makasu@124";
string u2 = "mEkasu@124";

Console.WriteLine(IsValidUsername(u1));
Console.WriteLine(IsValidUsername(u2));

string stringFromConsole = GetStringFromConsole("Please provide a username: ", ConsoleColor.Blue, IsValidUsername,
"Invalid username, please include at least 1 number and 1 upper-case letter.");

Console.WriteLine(IsValidUsername(stringFromConsole));
}

static bool IsValidUsername(string s)
{
bool hasCapLet = false;
bool hasNum = false;

foreach (char letter in s)
{
if (char.IsUpper(letter))
{
hasCapLet = true;
if (hasNum)
{
return true;
}
}
else if (char.IsNumber(letter))
{
hasNum = true;
if (hasCapLet)
{
return true;
}
}
}

return false;
}

static string GetStringFromConsole(
string prompt,
ConsoleColor color,
Predicate<string>? validator = null,
string? errorMessage = null,
ConsoleColor errorColor = ConsoleColor.Red)
{
while (true)
{
Console.Write(prompt, color);
var input = Console.ReadLine();

if (input != null && (validator?.Invoke(input) ?? true))
{
return input;
}

Console.WriteLine(errorMessage ?? "Bad Input, Try Again", errorColor);
}
}
}
public class Program
{
public static void Main(string[] args)
{
string u1 = "makasu@124";
string u2 = "mEkasu@124";

Console.WriteLine(IsValidUsername(u1));
Console.WriteLine(IsValidUsername(u2));

string stringFromConsole = GetStringFromConsole("Please provide a username: ", ConsoleColor.Blue, IsValidUsername,
"Invalid username, please include at least 1 number and 1 upper-case letter.");

Console.WriteLine(IsValidUsername(stringFromConsole));
}

static bool IsValidUsername(string s)
{
bool hasCapLet = false;
bool hasNum = false;

foreach (char letter in s)
{
if (char.IsUpper(letter))
{
hasCapLet = true;
if (hasNum)
{
return true;
}
}
else if (char.IsNumber(letter))
{
hasNum = true;
if (hasCapLet)
{
return true;
}
}
}

return false;
}

static string GetStringFromConsole(
string prompt,
ConsoleColor color,
Predicate<string>? validator = null,
string? errorMessage = null,
ConsoleColor errorColor = ConsoleColor.Red)
{
while (true)
{
Console.Write(prompt, color);
var input = Console.ReadLine();

if (input != null && (validator?.Invoke(input) ?? true))
{
return input;
}

Console.WriteLine(errorMessage ?? "Bad Input, Try Again", errorColor);
}
}
}
i've modified some small things like the GetStringFromConsole is now a Predicate<string> (exactly the same thing as what the Func<string, bool> did, just more clear) and the early exit checks for IsValidUsername via
if (otherCond)
{
return true;
}
if (otherCond)
{
return true;
}
Also, regarding Jetbrains products, Resharper is available on Visual Studio, it basically provides code suggestions for certain things
Mekasu0124
Mekasu0124ā€¢9mo ago
string username = ColorCon.GetStringFromConsole(
prompt: "Enter A Username",
color: color,
validator: x => Helpers.IsValidUsername(x),
errorMessage: errorText);
string username = ColorCon.GetStringFromConsole(
prompt: "Enter A Username",
color: color,
validator: x => Helpers.IsValidUsername(x),
errorMessage: errorText);
I set the username equal to the GetStringFromConsole() function call. So how is changing it to a predicate and removing the x => going to pass the user's input to the validator? I know I didn't initially show that. That's why I'm asking
arion
arionā€¢9mo ago
Changing the type from a Func<string, bool> to a Predicate<string> has 0 actual change when it comes down to it, but rather it makes use of already established delegates. Removing the x => is a visual change too (kinda) I say kinda, since it technically doesnt allocate a new delegate for the validator (it uses less RAM) But do note, RAM optimizations doesnt rly need to happen until your code needs it. I started C# from Unity game modding so i've seen my fair share of notlikethis code A simple console app like this doesnt really need aggressive memory optimizations, but Its pretty much just a benefit to know such things are more optimized than other things An established delegate like Action is actually just
public delegate void Action();
public delegate void Action();
Action<T> is just
public delegate void Action<in T>(T obj);
public delegate void Action<in T>(T obj);
A Func<T> is just
public delegate TResult Func<out TResult>();
public delegate TResult Func<out TResult>();
C# has a few built-in delegates like this Most common ones are Action,Func<T>,Predicate<T> and EventHandler<T> A Predicate<T> is just
public delegate bool Predicate<in T>(T obj);
public delegate bool Predicate<in T>(T obj);
Mekasu0124
Mekasu0124ā€¢9mo ago
that was a whole bunch of french
arion
arionā€¢9mo ago
noooooo
Mekasu0124
Mekasu0124ā€¢9mo ago
šŸ˜‚ The only documentation I know how to read is Python all the rest of it comes from YouTube "How To's" and StackOverflow
arion
arionā€¢9mo ago
I uhh, might be a bit of a documentation freak eYeS C# documentation got a bit boring so I switched to learning more about winapis (touching windows from almost any programming language) C# and WinAPI documentation look nearly identical in terms of layout eg. Predicate<T> vs LocalFree
Mekasu0124
Mekasu0124ā€¢9mo ago
I'm glad you can read it šŸ˜‚ it hurts my brain thanks for your help. I'm off to bed
Want results from more Discord servers?
Add your server
More Posts