Collection

Collection In C#– by Sagar Jaybhay

Arrays:

                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[3];

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.

List: –

                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.

. List list = new List();

String and StringBuilder: –

String: –

                                 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.

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

Sagar Jaybhay

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *