create a password with at least one uppercase letter in csharp

You can create a password with at least one uppercase letter in C# by using the following code:

main.cs
string password = "mypassword1";
bool hasUpperCase = password.Any(char.IsUpper);

if (!hasUpperCase)
{
    // password does not contain an uppercase letter
    // add code to handle validation error
}
201 chars
9 lines

The Any method is a LINQ extension method that checks if any character in the string satisfies a specified condition. In this case, the condition is whether the character is uppercase. If hasUpperCase is false, then the password does not contain an uppercase letter and you can handle the validation error accordingly.

gistlibby LogSnag