List is a collection object and helpful for performing programming task.
Example:
- You can use as datasource.
- 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
- 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.
- List<string> : To create string object list.
- List<string> _listOfFriend = new List<string>();
- _listOfFriend.Add("Rajesh");
- _listOfFriend.Add("Mahesh");
- _listOfFriend.Add("Ramesh");
- _listOfFriend.Add("Jayesh");
- _listOfFriend.Add("Mitesh");
- _listOfFriend.Add("Pritesh");
- _listOfFriend.Add("Suresh");
- _listOfFriend.Add("Dahnesh");
Figure 2: Add method detail
Figure 3: ASPX page code
After that you can attach to GridView as datasource.
- GridView1.DataSource = _listOfFriend;
- GridView1.DataBind();
Normal Output
Sort(): - List<string> _listOfFriend = new List<string>();
- _listOfFriend.Add("Rajesh");
- _listOfFriend.Add("Mahesh");
- _listOfFriend.Add("Ramesh");
- _listOfFriend.Add("Jayesh");
- _listOfFriend.Add("Mitesh");
- _listOfFriend.Add("Pritesh");
- _listOfFriend.Add("Suresh");
- _listOfFriend.Add("Dahnesh");
_listOfFriend.Sort();- GridView1.DataSource = _listOfFriend;
- 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.
- List<string> _listOfFriend = new List<string>();
- _listOfFriend.Add("Rajesh");
- _listOfFriend.Add("Mahesh");
- _listOfFriend.Add("Ramesh");
- _listOfFriend.Add("Jayesh");
- _listOfFriend.Add("Mitesh");
- _listOfFriend.Add("Pritesh");
- _listOfFriend.Add("Suresh");
- _listOfFriend.Add("Dahnesh");
_listOfFriend.RemoveAt(1); - GridView1.DataSource = _listOfFriend;
- GridView1.DataBind();
_listOfFriend.RemoveAt(1); Above command will erase second number of item that is Mahesh from list.
Update:
Update value of list item as per index number.
- List<string> _listOfFriend = new List<string>();
- _listOfFriend.Add("Rajesh");
- _listOfFriend.Add("Mahesh");
- _listOfFriend.Add("Ramesh");
- _listOfFriend.Add("Jayesh");
- _listOfFriend.Add("Mitesh");
- _listOfFriend.Add("Pritesh");
- _listOfFriend.Add("Suresh");
- _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.
- string[] FriendArray = _listOfFriend.ToArray();
Above will convert your List object into Array.
Convert To String
Convert List object into string.
- string[] FriendArray = _listOfFriend.ToArray();
- string result = string.Join(",",FriendArray);
- return result;
Lab Practice and Exercises - 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.