Dear, all
I am new to programming as well as new to C# .
Can somebody tell me how to concatanate strings in C#?
Currently , I am building a calculator in VS 2005 and I am stuckup in
decimal point button.
Reagards,
TR :)
Printable View
Dear, all
I am new to programming as well as new to C# .
Can somebody tell me how to concatanate strings in C#?
Currently , I am building a calculator in VS 2005 and I am stuckup in
decimal point button.
Reagards,
TR :)
Welcome to the forums. :wave:
There are a couple of ways to do that but I believe that using the StringBuilder Class is the best.
Here is a pretty good article on using this class.
string msg = "hello " + "world";
This is adequate for concatting 2 strings but as hack said if you are creating complicated and long strings it is best to use the stringbuilder. The reason being that strings are immutable, which means that a new string object is created each time you concatenate, this is not an issue when you are simply joining 2 or 3 strings (approximately the time it takes to instantiate a SB object.
The rule of thumb is to use "x" + "y" for less than 3 or less strings and use a stringbuilder for 4 or more.
If you are concatenating several strings all at the one time I'd suggest using the String.Concat method. That way you're making a single method call rather than multiple calls to the Append method of a StringBuilder. If you're concatenating multiple strings in a loop or something like that, where you cannot do it all in a single line, then I'd go with the StringBuilder. Of course, if all you want to do is put a decimal point between two numbers then you can do something like:Code:string result = string.Format("{0}.{1}", wholePart, fractionalPart);