Monthly Archives: June 2012

Character test utility

Every now and then I have to decide if a character satisfies a predicate (char.IsPunctuation, char.IsSymbol and so on) found on the System.Char class. The MSDN pages are of limited help plus I need to browse each predicate. Therefore I wrote a small piece of code that, aside from the fact that it’s quite simple and has nothing spectacular, tests a set of characters through all these predicates.

Basically this is a reference for me, in order to avoid rewriting this over and over again, but since it could be helpful to others too I decided to publish it here.


using System;
using System.Linq;

namespace CharTest
{
    internal class Program
    {
        static void Main()
        {
            var charPredicates = new Predicate<char>[]
            {
                char.IsControl, 
                char.IsDigit, 
                char.IsHighSurrogate, 
                char.IsLetter,
                char.IsLetterOrDigit,
                char.IsLowSurrogate, 
                char.IsLower, 
                char.IsNumber, 
                char.IsPunctuation, 
                char.IsSeparator, 
                char.IsSurrogate, 
                char.IsSymbol, 
                char.IsUpper, 
                char.IsWhiteSpace
            };

            Console.WriteLine("Enter an empty line to exit");
            Console.WriteLine();
            Console.WriteLine();

            string line;
            do
            {
                line = Console.ReadLine();
                var chars = line.Distinct().ToArray();
                for (var i = 0; i < chars.Count(); i++)
                {
                    var ch = chars[i];
                    Console.WriteLine();
                    Console.WriteLine("Distinct character " + (i + 1) + " : '" + ch + "'");
                    Console.WriteLine("-----------------------------------------");
                    foreach (var predicate in charPredicates)
                    {

                        Console.Write(predicate.Method.Name);
                        Console.SetCursorPosition(20, Console.CursorTop);
                        Console.WriteLine(predicate(ch));
                    }
                }
            }
            while (line != "");
        }
    }
}

The monthly WTF : You will execute

As I wander free upon the large fields of code in this world I sometimes come upon interesting things. The wonder, the joy, the mysteries that I encounter I must share with all of you, otherwise it would be selfish of me.

I will, therefore, start a series (it surely will be a series) of such posts called “The monthly WTF”.

Today I present you :

        private static void CanWhatever(object sender, CanExecuteRoutedEventArgs e)
        {
            if (SomeString.IsNotNullOrEmpty())
            {
                if (Repo.AllowSingleSignOn)
                {
                    e.CanExecute = false;
                }
            }
            e.CanExecute = true;
        }

Notice anything special?

WP Like Button Plugin by Free WordPress Templates