Apr 01 2024
// Using string concatenation
string result = "";
for (int i = 0; i < 1000; i++)
{
result += "a"; // Creates a new string object in each iteration
}
// Using StringBuilder
var builder = new StringBuilder();
for (int i = 0; i < 1000; i++)
{
builder.Append("a"); // Appends to the existing character array
}
string result = builder.ToString(); // Converts to string once at the end
string string1 = "hello world";
string string2 = "Hello World";
bool areEqual = string.Equals(string1, string2, StringComparison.OrdinalIgnoreCase);
// areEqual is true because the comparison is case-insensitive.
public class SpanVsSubstring
{
private const string testString = "This is a longer test string for demonstration.";
[Benchmark]
public string UseSubstring()
{
return testString.Substring(10, 5); // Extracts "longer"
}
[Benchmark]
public ReadOnlySpan<char> UseSpan()
{
ReadOnlySpan<char> span = testString.AsSpan(10, 5);
return span; // "longer"
}
}
Join 13,250+ subscribers to improve your .NET Knowledge.
Go-to resource for understanding the core concepts of design patterns without the overwhelming complexity. In this concise and affordable ebook, I've distilled the essence of design patterns into an easy-to-digest format. It is a Beginner level. Check out it here.
Every Monday morning, I share 1 actionable tip on C#, .NET & Arcitecture topic, that you can use right away.
Subscribe to the TheCodeMan.net and be among the 13,250+ subscribers gaining practical tips and resources to enhance your .NET expertise.