MrBool
You must be logged in to give feedback. Click here to login
[Close]

You must be logged to download.

Click here to login

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

A little more about Generic List using Csharp and ASP.NET

In this article I will cover some other features of List like Binary Search, Find Item, List to Array Copy, Insert Item and Get Range etc...

[close]

You didn't like the quality of this content?

Would you like to comment what you didn't like?

Introduction:

This Tutorial is second part of my previous Generic List Tutorial, In this tutorial I will cover some more features which are left in previous one. Generic List has some more attractive features like Find Item, Insert Item etc.

This article will cover the following topics:

  • List-Insert Item
  • List to Array Copy
  • List- Get Range
  • List- Find Item
List Items with Social Network Data

Figure 1: List Items with Social Network Data

ASP.NET Script:

ASP.NET is used here to display the List data and interact with the user. ASP.NET TextBox Controls, ASP.NET Label Controls, ASP.NET Buttons Controls and ListBox Control are added in ASP.NET page.

Listing 1: ASP.NET Script for List Demonstration

<head runat="server">
    <title>MrBool.com - List Tutorial Part 2</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        List Item:
        <asp:TextBox ID="txtListItem" runat="server"></asp:TextBox> 
        
        <br />
        <br />
        Message:
        <asp:Label ID="lblListDisplay" runat="server" Text=""></asp:Label>
        <br />
        <br />
        List State:<br />
        <asp:ListBox runat="server" ID="lstSocialNetwork" Width="200px" Height="150px" 
            > </asp:ListBox>
        <br />
        <br />
         <asp:Button ID="btnInsert" runat="server" Text="Insert" 
            onclick="btnInsert_Click" />
         <asp:Button ID="btnListtoArray" runat="server" Text="List to Array" 
            onclick="btnListtoArray_Click" />
         <asp:Button ID="btnGetRange" runat="server" Text="Get Range" onclick="btnGetRange_Click" 
            />
         <asp:Button ID="btnFind" runat="server" Text="Find" onclick="btnFind_Click" 
            />      
             </div>
    </form>
</body> 

C# Code:

Starting with C# code, First add the namespaces required for the List collection. Because list is a generic collection so add the following namespaces:

Listing 2: NameSpaces for List Collection

using System;
using System.Collections.Generic;

Now you have to create the List collection object and initialize it like below code.

Listing 3: List Object Declaration

static List<string> SocialNetworkList;
SocialNetworkList = new List<string>();

Now you have a List object of type string. Note that because this is string type, so you can also insert the int and other types(The types whose conversion is supported by string) into it but it will increase the conversion cost (Remember the concept of boxing and unboxing).

List-Insert Item

Similar to Add() method List provide a Insert() method to insert an item at a given location. Add() method add an item at the end only while using Insert() you can add an item at any given position. In given example it is inserting the “Digg” item at position “2” in the List. Note that List indexing start from “0”.

Insert Item - An item Inserted

Figure 2: Insert Item - An item Inserted (“Digg”)

Listing 4: List- Insert Item Source Code

protected void btnInsert_Click(object sender, EventArgs e)
    {
        if (txtListItem.Text.Trim() != "")
        {
            SocialNetworkList.Insert(2,txtListItem.Text.Trim());

            lblListDisplay.Text = "List Item Inserted at 2nd position";           
        }
        else
        {
            lblListDisplay.Text = "List - Please Enter an Item to Insert";
        }
        DisplayData();
    }
 

List to Array Copy:

Similar to some other collection classes List provide a method CopyTo() to copy the items into a given array . The below example explain the concept. First Define an array and then use CopyTo() method to copy items into Array. Note that the Array should be big enough to hold all the items getting copied.

List is Converted in Array and Printed on Screen

Figure 3: List is Converted in Array and Printed on Screen

Listing 5: List to Array Conversion- Code

protected void btnListtoArray_Click(object sender, EventArgs e)
    {
        string[] strArray = new string[SocialNetworkList.Count];

        SocialNetworkList.CopyTo(strArray);

        foreach (string str in strArray)
        {
            lblListDisplay.Text += "," + str;
        }
        lblListDisplay.Text = lblListDisplay.Text.TrimStart(',');

    }
 

List- Get Range:

List provide a GetRange() method to get the sublist from a given List. You can specify the Initial position and then length of the sublist and method will return the whole sublist. For example in the below program the GetRange method is called with (2,2) argument, it means start from 2 index and return the sublist of 2 length.

Get Range Items - The range (2,2) is fetched from List

Figure 4: Get Range Items - The range (2,2) is fetched from List

Listing 6: List- Get Range Items- Code

protected void btnGetRange_Click(object sender, EventArgs e)
    {
        string[] strArray = new string[100];

        List<string> tempNetworkList = SocialNetworkList.GetRange(2,2);

        foreach (string str in tempNetworkList)
        {
            lblListDisplay.Text += "," + str;
        }
        lblListDisplay.Text = lblListDisplay.Text.TrimStart(',');
    }
 

List- Find Item:

Find() method is used here to find an item in List. The Find() method support the Linq expression to pass the search query.

For Example the below code is using “item => item == txtListItem.Text.Trim()” it means search list with item name = “Given item”.

In below example, It is searching for “Google+” and it found in List.

Find Google in List

Figure 5: Find “Google+” in List

Listing 7: List- Find data - Code

protected void btnFind_Click(object sender, EventArgs e)
    {       

        if (txtListItem.Text.Trim() != "")
        {
            string str = SocialNetworkList.Find(item => item == txtListItem.Text.Trim());

            if (str != "")
            {
                lblListDisplay.Text = "List Item Found";
            }
            else
            {
                lblListDisplay.Text = "List Item Not Found.";
            }
        }
        else
        {
            lblListDisplay.Text = "Please Enter an Item to Search";
        }
        
    }
 

List - Other Functionality:

Apart from above common operations, C# List provide some more common functions.

BinarySearch: List provide binary search on List items and return the index.

IsExist: This method returns true or false for an search

IndexOf: The method returns the index of first occurrence of a given item

LastIndexOf: The method returns the index of last occurrence of a given item

Listing 8: List- Other operation

 
private void OtherFunctionality()
    {
        //BinarySearch
        int Index = SocialNetworkList.BinarySearch(txtListItem.Text.Trim());

        //Exists
        bool IsExist = SocialNetworkList.Exists(item => item == "Twitter");

        lblListDisplay.Text = " Get the index of Item: " + SocialNetworkList.IndexOf(txtListItem.Text.Trim());

        lblListDisplay.Text = " Get the LastIndexOf of item: " + SocialNetworkList.LastIndexOf(txtListItem.Text.Trim());
      
    }  

Conclusion:

Now I have done with the List Tutorial Part1 and Part2. The tutorials covered the good amount of features and operations of List. Go through both the tutorials and Comment here if you have any questions.



I am having total 7+ years of experience in developing applications using C#, VB.NET, ASP.NET, Silverlight, WCF, SQL Server and Oracle. I am holding a M.Tech degree in Intelligent systems. For more information, visit my Linkedin p...

What did you think of this post?

Did you like the post?

Help us to keep publishing good contents like this.

SUPPORT US

funded

remaining

[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually � in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits � in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login