LINQ tip to use proper for loop

Dear Friends,

As a programmer I would like to share some of the best tips which I found so far and this is one of the tip which I am going to share right away

suppose you have following  piece of code

string[] Names= namecollection.Split(GetDelimeter());

           List<string> NameList = new List<string>();

foreach (string name in Names)
              if (!string.IsNullOrEmpty(name ))
                  sb.Append(name .Trim() + ",");
                  NameList .Add(name .Trim());

Now you can write above  for each code  & if condition in following way

foreach (string Nam in Names.Where(Nam=> !string.IsNullOrEmpty(Nam)))
               sb.Append(Nam .Trim() + ",");
             NameList .Add(Nam.Trim());

It is much faster then a earlier code which shown

Hope you like this. Enjoy  Reading


Rajat Jaiswal

String Vs StringBuilder know it – in more Detail(curious)

Dear All,

We all know the difference between String  and StringBuilder. Like Once the String Object is define its length and content can not be modified. When we do concatenate the string a new object is created always.

While in StringBuilder  length and content can be modified even after object is created.It is faster than string etc.

So lets understand it what exactly happened inside this story.

for this I have created a windows application with 2 button and text box. The first button will handle String operation, second button handle String Builder operation and we show result in textbox. just simple one.

On string button I wrote following code.

private void btnString_Click(object sender, EventArgs e)
          string strResult = string.Empty;
          for (int intI = 0; intI < 4000; intI++)
              strResult = strResult + intI.ToString();
              strResult  = strResult + ",";
          this.txtResult.Text = strResult;

On string builder button  I wrote follow code.

private void btnStringBuilder_Click(object sender, EventArgs e)
StringBuilder strTest = new StringBuilder();
for (int intI = 0; intI < 4000; intI++)


this.txtResult.Text = strTest.ToString();


Simple enough to understand nothing new . So now what lets start a CLR Profiler application

If you don’t have CLR Profiler application then you can download it from here.

Now start CLR Profiler Application by double click the exe

You will get following screen


Now as we have created the windows application so we hit  “Start Application” button

As you click the Start Application button it will prompt for EXE file so  I provided the  exe of above project. as I  provided the path it started the exe of my project. So Now I clicked the string button as shown in below fig.


As you see the output is printed  in textbox after the print I closed the “Form1”  windows as I closed it.

Wow  I got following screen


Which basically  showing the memory allocation of string type  operation.

same above step  I have repeated for String Builder  button. I open the String Operation EXE again then hit String Builder Button and then closed the “Form1” window and as I closed the window I got following screen which is memory allocation of String Builder


Now just compare above result 



If you see above screen the memory allocation of string object is more than 250 times of StringBuilder object.  and there are more object to finalize in string operation but not in String builder. there is 91 Items in Generation 0 and 1 item in Generation 1 of String object while there is no item in String builder.

So from above comparison we can say that yes string object take more memory it makes GC busy a lot so possibility is higher that you may get “Out Of Memory exception” , your application is slow . So its better to use StringBuilder is such cases where you need to concatenate strings dynamically.

I hope you enjoyed the difference in detail . Will take care such situation and use proper string  or stringBuilder.

So enjoy the Weekend.

Thanks & Best Regards,

Rajat Jaiswal

Learn, Share, Grow knowledge