3

I just saw in one source code something like this:

char[] letters = { 'k', 'n', 'p', 's' };

    for (int d1 = 0; d1 < letters.Length; d1++)
    {
        for (int d2 = 0; d2 < letters.Length; d2++)
        {
            for (int d3 = 0; d3 < letters.Length; d3++)
            {
                for (int d4 = 0; d4 < letters.Length; d4++)
                {
                    string left = ""+ letters[d1] + letters[d2] + letters[d3] + letters[d4];
                }
           }
        }
    }

What I am realy wondering is how exactly is that string strName = "" +.... working. What are these "" for? I looked wherever I could, but didnt find answer. Sorry if answer is very easy, I am new in programming, so I will appreciate your understanding.

Zied R.
  • 4,964
  • 2
  • 36
  • 67
Kadiyski
  • 115
  • 6

2 Answers2

5

Somewhere in the C# Language Specifications (7.8.4 I think) it is written:

String concatenation:

string operator +(string x, string y);

string operator +(string x, object y);

string operator +(object x, string y);

These overloads of the binary + operator perform string concatenation. If an operand of string concatenation is null, an empty string is substituted. Otherwise, any non-string argument is converted to its string representation by invoking the virtual ToString method inherited from type object. If ToString returns null, an empty string is substituted.

So the + operator between a string and a char converts the char to a string (through the .ToString() method of the char) and concatenates the two.

So for example:

char ch = 'x';

string str = "" + ch;

is "converted" to

string str = "" + ch.ToString();

where ch.ToString() is "x" (with double quotes, so a string)

In the end "" + something is a (safe) way to call .ToString() for something, safe because it handles the case that something == null, by not calling .ToString() and returning an empty string.

In your specific case, you can see that line of code as something like:

string left = "" + letters[d1].ToString();
left = left + letters[d2].ToString();
left = left + letters[d3].ToString();
left = left + letters[d4].ToString();

(in truth it is a little more complex if I remember correctly, because the C# compiler optimizes multiple string concatenations through the use of a single call to string.Concat, but this is an implementation detail, see for example https://stackoverflow.com/a/288913/613130)

Community
  • 1
  • 1
xanatos
  • 109,618
  • 12
  • 197
  • 280
  • Great anwer, appreciate it! – Kadiyski Apr 19 '15 at 12:59
  • 1
    For the one that suggested the edit, no, it is wrong. There is an explicit addition between `""` and the first `letters[d1]`. Then the compiler can clearly be clever and remove it, but it is present. – xanatos Apr 19 '15 at 13:20
3

Simple : letters[i] is a char and to cast it to a string "" is added to the expression.

e.g. int x = 5;

  string str = ""+x;  // "5" as string.

Same goes for char to string :)

Zied R.
  • 4,964
  • 2
  • 36
  • 67
Sachin Sharma
  • 1,446
  • 4
  • 18
  • 37
  • 2
    You have got it, but perhaps you should try to expand a little explaining why you cannot build a string just concatenating characters – Steve Apr 19 '15 at 12:46
  • Just joined SO. Will keep that in Mind. :) – Sachin Sharma Apr 19 '15 at 12:47
  • Yes, I made the math for that :D but it looks very.. strange. Isn't there more official way to say "Hey, you- character! Go in that string!" I found something like `string someStr = String.Join("",someCh[0],someCh[1],someCh[2]);)` – Kadiyski Apr 19 '15 at 12:48
  • or you can use String.Concat(letters[d1] + letters[d2] + letters[d3] + letters[d4]); – Sachin Sharma Apr 19 '15 at 12:57