문제

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?

도움이 되었습니까?

해결책

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".

다른 팁

  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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top