Strings
-
Usually when I'm joining a bunch of things to make a string, I'll just do like
string a = pt.X.ToString() + ", " + pt.Y.ToString();
. I was just wondering if this was considered bad code or something, because any example I see where they are joining things together like this, they use something else besides this. Should I be using something else to join things like this? (Wow that was a lot of "this")
I wish I could drive...
-
Usually when I'm joining a bunch of things to make a string, I'll just do like
string a = pt.X.ToString() + ", " + pt.Y.ToString();
. I was just wondering if this was considered bad code or something, because any example I see where they are joining things together like this, they use something else besides this. Should I be using something else to join things like this? (Wow that was a lot of "this")
I wish I could drive...
I sometimes just use String.Format(....) ...
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
-
Usually when I'm joining a bunch of things to make a string, I'll just do like
string a = pt.X.ToString() + ", " + pt.Y.ToString();
. I was just wondering if this was considered bad code or something, because any example I see where they are joining things together like this, they use something else besides this. Should I be using something else to join things like this? (Wow that was a lot of "this")
I wish I could drive...
Because strings are immutable if there are several concatenations involved it is better to use StringBuilder[^] For small efforts I might consider String.Format for readability
only two letters away from being an asset
-
Usually when I'm joining a bunch of things to make a string, I'll just do like
string a = pt.X.ToString() + ", " + pt.Y.ToString();
. I was just wondering if this was considered bad code or something, because any example I see where they are joining things together like this, they use something else besides this. Should I be using something else to join things like this? (Wow that was a lot of "this")
I wish I could drive...
For a single statement like that, there is no better method. The compiler turns that statement into: string a = String.Concat(pt.X.ToString(), ", ", pt.Y.ToString()); This will allocate a string buffer with the correct size and copies the data from each string to it, so it causes a minimum of overhead.
Experience is the sum of all the mistakes you have done.
-
Usually when I'm joining a bunch of things to make a string, I'll just do like
string a = pt.X.ToString() + ", " + pt.Y.ToString();
. I was just wondering if this was considered bad code or something, because any example I see where they are joining things together like this, they use something else besides this. Should I be using something else to join things like this? (Wow that was a lot of "this")
I wish I could drive...
You should use a string builder
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
Usually when I'm joining a bunch of things to make a string, I'll just do like
string a = pt.X.ToString() + ", " + pt.Y.ToString();
. I was just wondering if this was considered bad code or something, because any example I see where they are joining things together like this, they use something else besides this. Should I be using something else to join things like this? (Wow that was a lot of "this")
I wish I could drive...
I prefer string.Format for that. There was a thread on this a few months back.