Thursday, April 12, 2012
c# interview question :- Why is stringbuilder concatenation more efficient than simple string concatenation?
This question is one of the favorites question which comes during c# interviews and the interviewer is not only expecting which one of them is more efficient. But he is also expecting the reason for the same.
In order to understand the same better lets consider the below scenario where we have two lines of code which does string concantenation.
For a simple string concantenation code shown below it will create 3 copies of string in memory.
//The below line of code Creates one copy of the string
string str ="anup";
/* The below line of code creates three copies of string object one for the concatenation at right hand side and the other for new value at the left hand side. The first old allocated memory is sent for garbage collection.*/
str = str + "anup";
When you use the string builder for concatenation it will create only one copy of the object.
/* The below code uses string builder and only one object is created as compared to normal string where we have 3 copies created*/
StringBuilder
objBuilder = new StringBuilder();
objBuilder.Append("anup");
Ok now summarizing what should we say to the interviewer.
String is immutable. Immutable means once assigned it can not be changed. Thats why it creates more copies of the object as it can not use the same instance.String builder is mutable , in other words the same object will changed rather than creating new objects.
So string builder is more efficient as compared to simple string concatenation.
Wednesday, April 4, 2012
Understanding Predicate Delegates in C#
c# - String to Generic [Type conversion]
Monday, March 26, 2012
Q: What is XBAP?
Q: What are possible ways to implement distributed applications in .NET?
Thursday, March 15, 2012
Q: Where’s the entry point in WPF application?
gives a compilation error telling you that it is already defined.
Application is special-cased when it is compiled from XAML, because Visual Studio assigns
the XAML file a Build Action of ApplicationDefinition. This causes a Main method to be
autogenerated.
Q: What unit of measurement is used by WPF?
are specified in device-independent pixels. These “logical pixels” are meant to represent
1/96th of an inch, regardless of the screen’s DPI setting. Note that device-independent
pixels are always specified as double values, so they can be fractional.
The exact measurement of 1/96th of an inch isn’t important, although it was chosen
because on a typical 96 DPI display, one device-independent pixel is identical to one physical
pixel. Of course, the notion of a true “inch” depends on the physical display device. If an
application draws a one-inch line on my laptop screen, that line will certainly be longer than
one inch if I hook up my laptop to a projector!