Working with Lists.

In this tutorial you’ll be working with Lists<>, C# sexy sexy new way of implementing arrays with a much wider gamma of uses.

We’ll be managing Lists<> of different types and calling some of the most common functions.

Read the complete tutorial after the break.

Hi there, thanks for reading the blog.


Ok first off let’s create a new Console Application.

Add the following code in the Main() method:

List<string> MyStringList = new List<string>();

List<int> MyIntList = new List<int>();

List<object> MyObjectList = new List<object>();

With this code we’re declaring three lists, one of type string, one of type int  and one of type object.

In the object type list we can store all sorts of information from chars, to bytes, to strings, to ints. It’s a generic type of list. Useful for passing parameter to a SQL Stored Procedure. But that’s a bit ahead of our scope. 😀


Let’s add some items to our lists.

MyStringList.Add("Hello, this is the first item.");
MyStringList.Add("Second item.");
MyStringList.Add("Third item.");

We’ve added three strings to our list. Simple enough right?

There is a neat function in the Lists class that Inserts an item to any zero-based index we want. For instance:

MyStringList.Insert(1, "Modified second item."); 

We’ll insert "Modified second item.” to the second position in our list displacing every item after it by one.

Write down this code to see it in action:

foreach (string X in MyStringList)

For each string we find in MyStringList, write down the string X to the console.

That concludes the string based tutorial for lists. Stay tuned for int based, and object based.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s