Sep 22 2007

StringBuilder in ASP.NET 2.0 AJAX 1.0 Client Library

Category: AJAX-ATLASBil@l @ 09:59

I was surprised to know that in the new ASP.NET 2.0 AJAX 1.0 Client Library, a new data type has been added which is the StringBuilder. As you know either in C# or VB.NET, when you are concatinating a large number of strings, each time you concat a string to another string, a new memory location is being assigned to the resulting string. However, when using a StringBuilder the same instance of the StringBuilder is used to append more and more strings.

This also applies on the extended JavaScript provided by the AJAX Client Library, so now you can write somethnig as this:

<script language="javascript" type="text/javascript">
    var sb = new Sys.StringBuilder();
    sb.appendLine("Hello World");
    sb.appendLine("ASP.NET 2.0 AJAX 1.0 Extensions are great");
    sb.append("A new line added without a break line using append!!");
   
    alert(sb.toString());
</script>

In the code above a new instance of Sys.StringBuilder class has been instantiated!! You can use the append and appendLine method to add text to the StringBuilder object. Also, you can use the toString() method to get the text out of the StringBuilder just as you do in C# or VB.NET!!

Hope this helps,
regards

 

Tags:

Comments are closed