List Collection Sample & Lab Practice Questions

List is a collection object and helpful for performing programming task.

 Example:

  1. You can use as datasource.
  2. Manipulate data,

    • Add : Add method for new string add at the bottom of the list.
    • Sort : Sort list items.
    • Delete: Remove item from list.
    • Update: Update list object with index of item.
    • Convert To Array
    • Convert To String

  3. Lab Practice Questions

List<t>: t stand for any data type.

Example:

_listOfFriend.Add():
Add method for new string and add at the bottom of the list.

add
                                    Figure 1: Above screen shot of Codebehing file.

In image shown how to create list object and call list. Add method to insert new list item.

  1. List<string> : To create string object list.  
  2. List<string> _listOfFriend = new List<string>();  
  3. _listOfFriend.Add("Rajesh");  
  4. _listOfFriend.Add("Mahesh");  
  5. _listOfFriend.Add("Ramesh");  
  6. _listOfFriend.Add("Jayesh");  
  7. _listOfFriend.Add("Mitesh");  
  8. _listOfFriend.Add("Pritesh");  
  9. _listOfFriend.Add("Suresh");  
  10. _listOfFriend.Add("Dahnesh");  
 Add method
                                                Figure 2: Add method detail

ASPX page code 
                                                  Figure 3: ASPX page code

After that you can attach to GridView as datasource.
  1. GridView1.DataSource = _listOfFriend;  
  2. GridView1.DataBind();  
Normal Output

output

Sort():
  1. List<string> _listOfFriend = new List<string>();  
  2. _listOfFriend.Add("Rajesh");  
  3. _listOfFriend.Add("Mahesh");  
  4. _listOfFriend.Add("Ramesh");  
  5. _listOfFriend.Add("Jayesh");  
  6. _listOfFriend.Add("Mitesh");  
  7. _listOfFriend.Add("Pritesh");  
  8. _listOfFriend.Add("Suresh");  
  9. _listOfFriend.Add("Dahnesh");  
_listOfFriend.Sort();
  1. GridView1.DataSource = _listOfFriend;  
  2. GridView1.DataBind();  
By just adding SORT() method to list object, output will be visible as in the following,

SORT()

You can see sorted output as mentioned above.

Remove():

Remove a list object with boolean return value

Remove
If searched item found it will return TRUE value otherwise FALSE.

searched item

In above code you can see Rajesh found then ItemFound variable value come as TRUE.

RemoveAt():

This will remove particular item from list as per zero index pattern. First item of list is 0 index item then 1, 2,3,4.
If total 5 item in list then index of list will be 4 because its start from 0.
  1. List<string> _listOfFriend = new List<string>();  
  2. _listOfFriend.Add("Rajesh");  
  3. _listOfFriend.Add("Mahesh");  
  4. _listOfFriend.Add("Ramesh");  
  5. _listOfFriend.Add("Jayesh");  
  6. _listOfFriend.Add("Mitesh");  
  7. _listOfFriend.Add("Pritesh");  
  8. _listOfFriend.Add("Suresh");  
  9. _listOfFriend.Add("Dahnesh");  
_listOfFriend.RemoveAt(1);
  1. GridView1.DataSource = _listOfFriend;  
  2. GridView1.DataBind();  
_listOfFriend.RemoveAt(1);
_listOfFriend.RemoveAt(1); 

Above command will erase second number of item that is Mahesh from list.

mahesh

Update:

Update value of list item as per index number.
  1. List<string> _listOfFriend = new List<string>();  
  2. _listOfFriend.Add("Rajesh");  
  3. _listOfFriend.Add("Mahesh");  
  4. _listOfFriend.Add("Ramesh");  
  5. _listOfFriend.Add("Jayesh");  
  6. _listOfFriend.Add("Mitesh");  
  7. _listOfFriend.Add("Pritesh");  
  8. _listOfFriend.Add("Suresh");  
  9. _listOfFriend.Add("Dahnesh");  
Now, the following code will change Mahesh value that is 1 index into Nagesh.

_listOfFriend[1] = "Nagesh";

update

Convert To Array

Convert List object in to Array.

  1. string[] FriendArray = _listOfFriend.ToArray();  
Above will convert your List object into Array.

Convert To Array

Convert To String

Convert List object into string.
  1. string[] FriendArray = _listOfFriend.ToArray();  
  2. string result = string.Join(",",FriendArray);  
  3. return result;  
Convert To String

Lab Practice and Exercises
  1. Write table from 1 to 100 and display in dropdown list, checkbox list and bulleted list.

    Sample code having article code and lab practice test code attached.


Similar Articles