2

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?

Glory Raj
  • 17,397
  • 27
  • 100
  • 203
happygilmore
  • 3,008
  • 4
  • 23
  • 37

2 Answers2

13

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

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • +1 The key is that given `'1','2',' ','3'` you don't want a sequence like `'1','2','','3'`. You don't want the `' '` replaced with anything at all, so just don't `yield` anything. – George Duckett Apr 03 '13 at 13:36
  • `There is no such thing as "no character".` Well, actually, there is. `'\0'`. The "null character". That said, it shouldn't be used in this context. – Servy Apr 03 '13 at 13:58
  • 2
    The nul character is a valid character. It has a defined value. It has a special meaning for many programming languages, but it's still a character. "No character" cannot exist the same way that "no int" cannot exist, although we frequently speak of "no money" when we mean zero :) – nvoigt Apr 03 '13 at 15:04
  • @nvoigt No character (or no int) can certainly exist. You would just need to change the type from `char` to `char?`. – svick Apr 03 '13 at 18:17
  • @svick Even a Nullable char always has a value. It only has an *additional* bool value telling you if the value of the char is useful to you or just random garbage. But it has a value, even a char? cannot have "no value". You can test it, check the value of the char in the nullable char if the HasValue bool is false. It's still a char with a value. – nvoigt Apr 04 '13 at 06:00
  • @nvoigt Well, that depends on what exactly does “has a value” mean. If you look at it technically, then yeah, it's a `struct` that has a fields set to `false` (so it has a value). But logically, this value represents a state without a value, just like real `null` does for reference types. And most of the time, you care about the logical meaning of code, not about its implementation. – svick Apr 04 '13 at 10:44
  • @nvoigt, what if I want to do a replace with: Replace('*', '') so that I replace the asterisk with no space. How can I do that? – Kala J Jan 05 '15 at 20:37
  • @KalaJ That seems to be a different question. You may want to do a search for your question here and if you don't find an answer, post a new question. – nvoigt Jan 06 '15 at 07:55
2
  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.

svick
  • 236,525
  • 50
  • 385
  • 514
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276