Skip to main content

Posts

Showing posts with the label String

Formatting strings in binding

Recently I get a chance for a code walk through of an WPF application in order to achieve better performance. So, during my review process, I came across this below code snippet:   <WrapPanel>         <TextBlock Text="{Binding Path=FirstName,Mode=OneWay}"/>         <TextBlock Text=" "/>         <TextBlock Text="{Binding Path=LastName,Mode=OneWay}"/>     </WrapPanel> If you will closely analyze this given snippet, you will definitely get a way to optimize it. Well, I am talking  about formatting the string as well as binding part. As most of you are aware that we have a property named StringFormat since .Net 3.5 SP1. So, why can't we use this StringFormat for our binding too. If you want to change the above code to incorporate StringFormat , then it will something look like: <WrapPanel>         <TextBlock>                 <TextBlock.Text>                     <MultiBinding String

Converting a given string to Title Case

To convert string in title case is bit tedious and may require lot of code as there is no direct method available in C#.Net  in String class. So, here I am sharing a method, which will be the extension method for String class and can be used to convert given string to title case. Here I am using CultureInfo from Globalization and ToTitleCase method of TextInfo class. public static class StringExtensions {     public static string ConvertToTitleCase(this string input)     {         System.Globalization.CultureInfo cultureInfo =         System.Threading.Thread.CurrentThread.CurrentCulture;         System.Globalization.TextInfo textInfo = cultureInfo.TextInfo;         return textInfo.ToTitleCase(input.ToLower());     } } Hope reusing this will save lot of your time :)

Performance analysis for String and StringBuilder

Sometimes small-small changes in our code really makes a huge difference to a performance. There are many tips and tricks available and among those, one I am going to discuss over here. I'll be talking about  String vs StringBuilder. One needs to be very careful while playing with strings because memory wise there is a huge impact of strings. I  know, there are lots and lots of articles available on net on String and StringBuilder, but still I am going to show this, using some statistics. Here I am taking Fx 4.0 C# console application with different static methods to showcase my analysis.  Basically what I am doing here is, I am having a String variable named  outputString  and just looping that for 1000 times and concating the string to variable outputString.  Please note, concatenation is done using + symbol. So, what happens internally is, whenever concatenation is done using + symbol, every time, new String object is created. So, as with my snippet. Here I am  lo