Well, on the one hand, you can make the code simpler:
foreach (char c in value)
{
if (c < 32 || c > 126)
{
...
}
}
Or using LINQ if you just need to know if any non-ASCII characters are:
bool bad = value.Any(c => c < 32 || c > 126);
... but in principle, you cannot detect non-ASCII characters without repeating each character in the string ...
source
share