|
|
|
Hi!
I’m very interesting in when to use exactly the StringBuilder?
For example for something like this?:
String strTest1 = “This”;
String strTest2 = “Test”;
StringBuilder stbTest = new StringBuilder();
stbTest.Append(strTest1). Append(“is a ”). Append(stbTest);
can someone provide some sample codes when to use a StringBuilder?
And can someone tell me his experience about the performance of a StringBuilder?
Regards,
gicio
|
|
|
|
|
StringBuilders should be used where you need to concatenate lots of strings together. The StringBuilder allocates a chunk of memory bigger than the size of the actual string, and copies the new strings into the extra space at the end. This way, you aren't repeatedly re-allocating the memory and copying both the old and the new string into it.
I've found that the StringBuilder takes at least 30x less time than it would take without it. My benchmark test involved loading a rich text file with a picture in it line-by-line. Without the StringBuilder, it took 3 minutes; with the StringBuilder, it took 35ms.
|
|
|
|
|
|
I read somewhere (I don't recall right now) that you should use string.Concat for up to 5 strings and StringBuilder if you need more than 5.
Also, if you really require that absolute last clock-cycle out of string comparisons then the static version of string.Equals yields the quickest returns. But it's horible to read so I just stick with if(string1==string2) ... unless I really need those clock cycles.
Regards,
Colin.
--Colin Mackay--
"In the confrontation between the stream and the rock, the stream always wins - not through strength but perseverance." (H. Jackson Brown)
|
|
|
|
|
Here's one small fragment:
using System;
using System.Text;
namespace StringVsStringBuilder
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Playing with String");
string sName = "Jackarse";
sName = "Paul III"; //You actually get a new string (new memory slot).
Console.WriteLine("Before replace (string): " + sName);
sName.Replace("III", "IX");
Console.WriteLine("After replace (string): " + sName);
Console.WriteLine("String.Replace(..) " + sName.Replace("III", "V"));
Console.WriteLine();
Console.WriteLine("Playing with StringBuilder");
StringBuilder sbName = new StringBuilder("Timothy III");
Console.WriteLine("Before replace (StringBuilder): " + sbName);
sbName.Replace("III", "IX");
Console.WriteLine("After replace (StringBuilder): " + sbName);
return;
}
}
}
There're also other things you need to know about String Vs StringBuilder when you do PInvoke. But, one step at a time.
|
|
|
|
|
One of the standards that you need to apply to this decision of string vs. StringBuilder comes when you are looking at how frequently the code gets executed.
If a user clicks a button and that puts out the full name by doing firstName+" "+lastName then you can do the concats quite easily and not even think of a StringBuilder.
If you are constructing a large process that may be executed 10-1000 times per object processed AND you are doing concats in it, then you might get a performance benefit by using StringBuilders. However, you must keep in mind that you can still create process overhead even with a StringBuilder. For example:
StringBuild workArea;
for (int i=0; i<1000; i++)
{
... process
workArea.Append(someString);
...
string answer = workArea.ToString();
}
Once you have set a string to a value of a StringBuilder.ToString() value, you should create a new StringBuilder and cannot use it multiple times.
Another item to consider is if you are doing a looped process and doing alot of checks if either stringA == stringB or stringA == "" then you get the best performance by doing stringA.Equals(stringB) AND stringA.Length == 0.
_____________________________________________
The world is a dangerous place. Not because of those that do evil, but because of those who look on and do nothing.
|
|
|
|
|
how would i retrieve the temperature from my abit ic7-g motherboard? i want to be able to view what the temperature is inside my computer.
thanks,
Rob Tomson
--
There are 10 kinds of people. Those who understand binary and those who don't.
|
|
|
|
|
Rob Tomson wrote:
how would i retrieve the temperature from my abit ic7-g motherboard? i want to be able to view what the temperature is inside my computer.
You might want to check the Windows Management Instrumentation[^] documentation.
-Nick Parker
DeveloperNotes.com
|
|
|
|
|
i don't see how i can use this to get the temperature. is there an example somewhere? or maybe just point me in the right direction?
thanks,
Rob Tomson
--
There are 10 kinds of people. Those who understand binary and those who don't.
|
|
|
|
|
How can i use the data source control in my own aplication like we do it in visual studio .NET Actually i am talking of the control through which we select the database driver what is the name of that control and how can we use that. please explain it briefly
Thanks
Inam
|
|
|
|
|
how can I adda a value to registry keys and modify taht value(I know Microsoft.Win32.registrykey has some functions but i can not find any method for adding value)
|
|
|
|
|
Microsoft.Win32.Registry.CurrentUser.CreateSubKey(name);
CreateSubKey opens an existing subkey, or creates it.
|
|
|
|
|
this method create a new subkey not a value
|
|
|
|
|
Sorry, I thougt he meant adding keys. For setting or creating values there is SetValue - doesn't it create the value, if it's not there?
|
|
|
|
|
Hi,
You can find a nice article about C# and the registry here in Codeprojec,t I think Nish was the one who wrote it.
HTH
Braulio
|
|
|
|
|
|
I wanted to start getting into C# controls. At least that's what I think I want...
Ok, so I want to start making my own button, and/or Dialogs... for example, instead of using the typical .net buttons, I want to design my own, with their own look; and i want to make my own forms too, instead of the windows xp look on all my apps, i would like to design my own look, replacing the xp title bar with my own and everything...
What kind of a book would I need to learn this? controls?
/\ |_ E X E GG
|
|
|
|
|
Has any body ever read this??
Windows Forms Programming in C#
by Chris Sells (Author)
and/or
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald
/\ |_ E X E GG
|
|
|
|
|
eggie5 wrote:
What kind of a book would I need to learn this? controls?
There are many books out there that discuss making your own controls as a chapter they cover however there are also good references available online. I would suggest you start at CodeProject's C# Controls[^] section and take a look there. Chris Sells did a nice article on Building Windows Controls[^], however I would always stress it is a good idea to read through the .NET Framework Documentation[^]
-Nick Parker
DeveloperNotes.com
|
|
|
|
|
|
Is there an API that a process or a service can call to determine if a computer is signed on?
thanks
|
|
|
|
|
Maybe the Environment class can help you.
Environment.UserName - Name of the user account under which you process is running. The documentation says, it is the user who started the thread. But in reality it returns the user your process is loggend as.
Environment.UserDomainName - network domain name of the active user
|
|
|
|
|
Thank you, that was very helpful.
|
|
|
|