is this a nice thing to do

T

Tony Johansson

Hi

I just want your opinion about using the feature to add "" to make it a
string.
So is it bad and more unreadable to use the "" feature to make it a string ?
In example #1 below I use the feature to add "" to make it a string but
in #2 I use the standard method to use the ToString to it a string

static void Main(string[] args)
{
string letters = "ABCD";
Random random = new Random();
string letterCode;

#1 letterCode = "" + letters[random.Next(0,4)] +
letters[random.Next(0,4)];
#2 letterCode = letters[random.Next(0,4)].ToString() +
letters[random.Next(0,4)].ToString();
}

//Tony
 
A

Alexey Smirnov

Hi

I just want your opinion about using the feature to add "" to make it a
string.
So is it bad and more unreadable to use the "" feature to make it a string ?
In example #1 below I use the feature to add "" to make it a string but
in #2 I use the standard method to use the ToString to it a string

static void Main(string[] args)
  {
         string letters = "ABCD";
         Random random = new Random();
         string letterCode;

#1      letterCode = "" + letters[random.Next(0,4)] +
letters[random.Next(0,4)];
#2      letterCode = letters[random.Next(0,4)].ToString() +
letters[random.Next(0,4)].ToString();

}

//Tony

Using ToString is more clear and correct. However, you can check if
char array could give you better performance

char[] arr = new char[2];
arr[0] = letters[random.Next(0, 4)];
arr[1] = letters[random.Next(0, 4)];
letterCode = new string(arr);

http://dotnetperls.com/char-combine

....another way is to use string.Concat

letterCode = string.Concat(letters[random.Next(0, 4)], letters
[random.Next(0, 4)]);
 
R

Rick Lones

Tony said:
Hi

I just want your opinion about using the feature to add "" to make it a
string.
So is it bad and more unreadable to use the "" feature to make it a string ?
In example #1 below I use the feature to add "" to make it a string but
in #2 I use the standard method to use the ToString to it a string

static void Main(string[] args)
{
string letters = "ABCD";
Random random = new Random();
string letterCode;

#1 letterCode = "" + letters[random.Next(0,4)] +
letters[random.Next(0,4)];
#2 letterCode = letters[random.Next(0,4)].ToString() +
letters[random.Next(0,4)].ToString();
}

Re #1: If you index strings rather than characters you don't need either:

string[] letters = {"A","B","C","D"};
Random random = new Random();
string letterCode = letters[random.Next(0,4)] +
letters[random.Next(0,4)];

Re #2: You only need the first ToString():

letterCode = letters[random.Next(0,4)].ToString() +
letters[random.Next(0,4)]; // this works

It works because successive concatenations implicitly invoke ToString() for you.

-rick-
 
R

Rick Lones

Roger said:
That extra concatenation is absolutely nothing except slowing you down.

Actually it is enabling the expression to compile. The result of the indexing
operation as presented is a character, not a string. Leaving out "" in Tony's
method #1 raises a compiler error.
Tony Johansson said:
Hi

I just want your opinion about using the feature to add "" to make it
a string.
So is it bad and more unreadable to use the "" feature to make it a
string ?
In example #1 below I use the feature to add "" to make it a string but
in #2 I use the standard method to use the ToString to it a string

static void Main(string[] args)
{
string letters = "ABCD";
Random random = new Random();
string letterCode;

#1 letterCode = "" + letters[random.Next(0,4)] +
letters[random.Next(0,4)];
#2 letterCode = letters[random.Next(0,4)].ToString() +
letters[random.Next(0,4)].ToString();
}

//Tony
 
R

Roger Frost

No kidding, so you prefer #1 huh? Myself, I wouldn't cloud the logic with
an empty concatenation, it doesn't improve readability or performance, but
to each his own I suppose. And really both methods could use improvement,
but all things being equal I believe #2 is more desirable. I would hate to
try to explain to a superior what I was thinking if I used "" + in anything,
let alone something as simple as this.



Rick Lones said:
Roger said:
That extra concatenation is absolutely nothing except slowing you down.

Actually it is enabling the expression to compile. The result of the
indexing operation as presented is a character, not a string. Leaving out
"" in Tony's method #1 raises a compiler error.
Tony Johansson said:
Hi

I just want your opinion about using the feature to add "" to make it a
string.
So is it bad and more unreadable to use the "" feature to make it a
string ?
In example #1 below I use the feature to add "" to make it a string but
in #2 I use the standard method to use the ToString to it a string

static void Main(string[] args)
{
string letters = "ABCD";
Random random = new Random();
string letterCode;

#1 letterCode = "" + letters[random.Next(0,4)] +
letters[random.Next(0,4)];
#2 letterCode = letters[random.Next(0,4)].ToString() +
letters[random.Next(0,4)].ToString();
}

//Tony
 
T

Tony Johansson

Rick Lones said:
Tony said:
Hi

I just want your opinion about using the feature to add "" to make it a
string.
So is it bad and more unreadable to use the "" feature to make it a
string ?
In example #1 below I use the feature to add "" to make it a string but
in #2 I use the standard method to use the ToString to it a string

static void Main(string[] args)
{
string letters = "ABCD";
Random random = new Random();
string letterCode;

#1 letterCode = "" + letters[random.Next(0,4)] +
letters[random.Next(0,4)];
#2 letterCode = letters[random.Next(0,4)].ToString() +
letters[random.Next(0,4)].ToString();
}

Re #1: If you index strings rather than characters you don't need either:

string[] letters = {"A","B","C","D"};
Random random = new Random();
string letterCode = letters[random.Next(0,4)] +
letters[random.Next(0,4)];

Re #2: You only need the first ToString():

letterCode = letters[random.Next(0,4)].ToString() +
letters[random.Next(0,4)]; // this works

It works because successive concatenations implicitly invoke ToString()
for you.

-rick-

I used cut and paste one to many ToString()

//Tony
 
G

Göran Andersson

Tony said:
Hi

I just want your opinion about using the feature to add "" to make it a
string.
So is it bad and more unreadable to use the "" feature to make it a string ?
In example #1 below I use the feature to add "" to make it a string but
in #2 I use the standard method to use the ToString to it a string

static void Main(string[] args)
{
string letters = "ABCD";
Random random = new Random();
string letterCode;

#1 letterCode = "" + letters[random.Next(0,4)] +
letters[random.Next(0,4)];
#2 letterCode = letters[random.Next(0,4)].ToString() +
letters[random.Next(0,4)].ToString();
}

//Tony

When you add an extra empty string instead of using ToString, you are
causing the code to use the overload of String.Concat that takes object
parameters instead of string parameters.

The #1 code compiles into:

letterCode = String.Concat(
(object)"",
(object)letters[random.Next(0,4)],
(object)letters[random.Next(0,4)])
);

While the #2 code compiles into:

letterCode = String.Concat(
letters[random.Next(0,4)].ToString(),
letters[random.Next(0,4)].ToString()
);

What happens with the #1 code is that the characters are boxed before
they are sent to the method, and ToString is called on each parameter to
create the strings to concatenate. The end result is the same

There are other effective alternatives to put the characters into a
string without the need to turn each of them into a string first. For
example:

letterCode = new String(new char[] {
letters[random.Next(0,4)],
letters[random.Next(0,4)]

});

or:

letterCode =
new StringBuilder(2)
.Append(letters[random.Next(0,4)])
.Append(letters[random.Next(0,4)])
.ToString();

Both alternatives creates a throw-away object. The first alternative
creates an array to create the string from, and the second alternative
creates a StringBuilder. Both are however still better than code #1
(creating five extra objects) and slightly better than code #2 (creating
two extra objects).
 
G

Göran Andersson

Rick said:
Tony said:
Hi

I just want your opinion about using the feature to add "" to make it
a string.
So is it bad and more unreadable to use the "" feature to make it a
string ?
In example #1 below I use the feature to add "" to make it a string but
in #2 I use the standard method to use the ToString to it a string

static void Main(string[] args)
{
string letters = "ABCD";
Random random = new Random();
string letterCode;

#1 letterCode = "" + letters[random.Next(0,4)] +
letters[random.Next(0,4)];
#2 letterCode = letters[random.Next(0,4)].ToString() +
letters[random.Next(0,4)].ToString();
}

Re #1: If you index strings rather than characters you don't need either:

string[] letters = {"A","B","C","D"};
Random random = new Random();
string letterCode = letters[random.Next(0,4)] +
letters[random.Next(0,4)];

Re #2: You only need the first ToString():

letterCode = letters[random.Next(0,4)].ToString() +
letters[random.Next(0,4)]; // this works

It works because successive concatenations implicitly invoke ToString()
for you.

-rick-

It does invoke ToString, but not directly. The overload of String.Concat
that takes objects is called, so the character is boxed. You should call
ToString explicitly on both characters so that the overload that takes
strings is used.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top