Products
Database Search Solution (New Version) Search Control SEO Pager Highlighter Shortcut Controls Crypt Package Free ASP.NET Controls
Geotargeting Component ASP.NET Media Player Control Flash Video Player Control Services
ASP.NET Telecommute Jobs Free IP Location Lookup Test .Net Regular Expressions CSS/Table/DIV Page Layouts Custom Programming Article Sites Master List |
Strings Performance In ASP.NETStrings are very important, you probably started to learn programming by using a strings, to build famous "Hello World!" application. After that, you probably noticed that string manipulation is one of the most used programming techniques in real world applications, especially in web programming. Optimizing string concatenationString concatenation is often used by ASP.NET developer to build XML, HTML or SQL code dynamically. There are two common ways to concatenate strings in .NET Framework, by using operators & or + and by using StringBuilder class. String Concatenation by using an operator + or &You can concatenate strings on standard way by using + or & operator, with code like this: [ C# ] private
string CreateContactsXML(int
ContactsTotal) [ VB.NET ] Private
Function ContactsXML(ByVal
ContactsTotal As
Integer) As
String
Note that code above is not doing anything smart, it just creates long string. String Concatenation by using a StringBuilder classAnother way to build strings is by using a specialized StringBuilder class, with code like this: [ C# ] private
string ContactsXMLWithStringBuilder(int
ContactsTotal) [ VB.NET ] Private
Function ContactsXMLWithStringBuilder(ByVal
ContactsTotal As
Integer) As
String
Note: To use StringBuilder class you need to import System.Text namespace. So, should I use operators + and & or StringBuilder class?StringBuilder is generally faster because string is immutable and every action on string actually create new string. That uses a lot of memory. But StringBuilder is complex class and instantiate of StringBuilder type will take some time too. Because of that, you can use standard way with + and & operators if you concatenate just a few strings. If number of strings is higher (e.g. few hundreds), then you should consider StringBuilder way. In general, if you work with some long while or for loop you better use StringBuilder's Append method since it is faster, with less memory requirements and less CPU usage. Of course, the fastest solution is to not use concatenation at all :). If you can avoid it on some way that is usually best solution. Difference between String.Empty and ""This difference is not big, but if you want to get maximum efficiency use String.Empty because String.Empty does not create an object. "" creates an object, so it is probably better to write code like this: [ C# ] if
(StringToCompare == String.Empty) [ VB.NET ] If
StringToCompare = String.Empty
Then
There is another way, even better than String.Empty. Instead of any comparing of strings, you can just check string length. This will be fastest solution, like this: [ C# ] if
(StringToCompare.Length == 0) [ VB.NET ] If
StringToCompare.Length = 0 Then Comparing strings (case insensitive)Easiest way for case insensitive comparation of two strings is by using ToLower() or ToUpper() method, and many developers do so. The code with ToUpper() method would look like this: [ C# ] if
(String1.ToUpper() == String2.ToUpper()) [ VB.NET ] If
String1.ToUpper = String2.ToUpper Then
However, there is much more efficient way for case insensitive strings comparing by using Compare() method: [ C# ] if
(String.Compare(String1, String2,
true) == 0) [ VB.NET ] If
String.Compare(String1, String2,
True) = 0 Then
Second approach with String.Compare method much more faster than ToLower() or ToUpper() method and without high demands to garbage collector. Comparing strings (case sensitive)When comparing strings, developers usually use = operator in VB.NET or == operator in C#. There is significant difference between C# operator == and VB.NET operator =. == is much faster than =. If you are C# programmer, you can continue to use = operator and performance will be ok. Of course, if you are VB.NET programmer, you can't use == operator. But, you can use String.Equals method to get better results. Code could look like this: [ VB.NET ] If
String.Equals(String1, String2)
Then SecureStringSecureString class is located in System.Secure namespace. Common classes String and StringBuilder stores data in memory as plain text. If your string contains sensitive data this could be security problem. In that case, you should consider SecureString class. This class encrypts data automatically by using DPAPI (Data Protection API). Unfortunately, we can't assign a string to SecureString directly, but we can use its AppendCharacter method to add character by character. For example, to assign word "hello" code would be: [ C# ] System.Security.SecureString
SecretString = [ VB.NET ] Dim
SecretString As System.Security.SecureString = _
There is an interesting method MakeReadOnly(). After calling this method it is not possible to add new characters. If you try to add another character SecureString will throw exception. To return a string from SecureString when you need it, use this code: [ C# ] using
System.Runtime.InteropServices; [ VB.NET ] Imports
System.Runtime.InteropServices Tutorial toolbar: Tell A Friend | Add to favorites | Feedback | Google |