Question

I'm writing a Linq extension method, to make a p455w0rd from a string input.

public static IEnumerable<char> ToPasswordFormat(this IEnumerable<char> source)
{
        var enumerator = source.GetEnumerator();
        while (enumerator.MoveNext())
        {
            switch((char)enumerator.Current)
            {
                case 'a':
                    yield return '4';
                    break;
                case 'e':
                    yield return '3';
                    break;
                case 'l':
                    yield return '7';
                    break;
                case 'i':
                    yield return '!';
                    break;
                case ' ':
                    yield return '';
                    break;
                default:
                    yield return (char)enumerator.Current;
                    break;
            }
        }
}

as you can see I want to remove spaces, but when I use yield return ''; it gives me error Empty character literal.

What is '' and how can I return yield return nothing?

Was it helpful?

Solution

You cannot yield return nothing. The solution would be to not yield return if it's a whitespace. Comment out that line and you should be good.

'' is meaningless. The compiler will complain. There is no such thing as "no character".

OTHER TIPS

  1. '' is meaningless. There is no concept of "no character" - there is the nul character, but it is not the same as "no character". Compare this with strings, which can be a null reference (to a string) or a zero-length string.

  2. Already answered in a comment to your question by @George Duckett: Just remove the yield from the ' ' case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top