IntroductionIn this article, I am going to discuss some important facts which might be unknown to most developers. I am going to discuss about three different topics which are related to C# and are more helpful when building your application.
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace StringTest
{
class Program
{
static void Main(string[] args)
{
string s = "Pranay";
s += " rana";
s += " rana1";
s += " rana122"; StringBuilder sb = new StringBuilder();
sb.Append("pranay");
sb.Append(" rana");
}
}
}
- Why use StringBuilder over String to get better performance
- Structure initialization in C#
- Checked operator
- Go To with Switch... Case
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace StringTest
{
class Program
{
static void Main(string[] args)
{
string s = "Pranay";
s += " rana";
s += " rana1";
s += " rana122"; StringBuilder sb = new StringBuilder();
sb.Append("pranay");
sb.Append(" rana");
}
}
}
After the execution of the above code, we get:s= "pranay rana rana1 rana12"
sb = "pranay rana"
sb = "pranay rana"
To get in to the details of what happened when I appended the string, I used the RedGate .NET Reflector. The image below shows the IL of the code that I executed above. As you can see: The String calls the Concat(str0,str1) function to append the string.
The StringBuilder calls the Append(str) function to append the string.Read more: Codeproject
The StringBuilder calls the Append(str) function to append the string.Read more: Codeproject
0 comments:
Post a Comment