Collection In C#– by Sagar Jaybhay
When you use int i=10; you can store only one value in that variable but when you need to enter multiple items/value of the same type in a variable then arrays comes to picture. But you can store the same type of element in that array.
int i = new int;
Arrays are strongly typed. One disadvantage of the array is once you initialized that array then it not grow.
For accessing the items inside array you need to use indices in the array.
Is a collection like an array of the same data type or user-defined data type. But the only difference is that it is dynamic in nature. When we use this we don’t know how many objects are added to the collection.
String and StringBuilder: –
Is a data type. When you entered text in a textbox or console, automatically it converts into a string. The string is immutable. Means if you change the value of string variable rather than changing the value it will create a new copy of that data. The string is enclosed in double quotes and in c# it means string staring and string ends. Immutable means once the value is assign or object is created you can not change that value. Microsoft is doing string as immutable for thread safety reason.
string name = “sagar”;
name = “jaybhay”;
StringBuilder: – Is immutable and is used when heavy concatenation required. In this when you assign a new value to a variable it will not create new reference rather than it used the previous reference and update the previous value. Both string and StringBuilder use Heap to allocate memory. Performance is increased when you used stringBuilder.
Clr profiler link:
Use CLR profiler to show memory allocation.
When to use string and StringBuilder?
When your string not going to change in the program then use string but if it changed you can use StringBuilder.