In .NET 4.0, you can use LINQ:
if (yourText.All(char.IsLetterOrDigit)) {
yourText.All will stop execution and return false the first time that char.IsLetterOrDigit reports false char.IsLetterOrDigit since then the All contract cannot be executed.
The note! this answer does not check alphanumeric characters (usually AZ, az and 0-9). This answer allows local characters like Γ₯Àâ .
Update 2018-01-29
The syntax above only works when you use a single method that has one argument of the correct type (in this case, char ).
To use several conditions, you need to write like this:
if (yourText.All(x => char.IsLetterOrDigit(x) || char.IsWhiteSpace(x))) { }
jgauffin May 20 '13 at 1:04 pm 2013-05-20 13:04
source share