Introduction
In ASP.NET WebForms, you often need to handle multiple values — like product names, user IDs, or form inputs — together.
That’s where Arrays and Lists come in.
Arrays are fixed-size collections.
Lists are dynamic — you can add or remove items at runtime.
In this blog, we’ll understand both with real ASP.NET C# examples, such as displaying product names in a GridView.
What is an Array in C#?
An Array is a collection that stores multiple values of the same type in a fixed size.
Example
string[] products = { "Laptop", "Mouse", "Keyboard", "Monitor" };
You can access values using index positions:
Response.Write(products[0]); // Output: Laptop
Example 1: Display Array Data in ASP.NET WebForm
ASPX Page (ArrayExample.aspx)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ArrayExample.aspx.cs" Inherits="WebFormsDemo.ArrayExample" %>
<!DOCTYPE html>
<html>
<head>
<title>Array Example in ASP.NET</title>
</head>
<body>
<h2>Array Example: Product List</h2>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"></asp:GridView>
</body>
</html>
Code Behind (ArrayExample.aspx.cs)
using System;
using System.Data;
namespace WebFormsDemo
{
public partial class ArrayExample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Step 1: Create an array of products
string[] products = { "Laptop", "Mouse", "Keyboard", "Monitor", "Printer" };
// Step 2: Convert array to DataTable to display in GridView
DataTable dt = new DataTable();
dt.Columns.Add("Product Name");
foreach (string item in products)
{
dt.Rows.Add(item);
}
// Step 3: Bind data to GridView
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
Output
| Product Name |
|---|
| Laptop |
| Mouse |
| Keyboard |
| Monitor |
| Printer |
What is a List in C#?
A List is a generic collection that allows dynamic resizing — you can add, remove, or modify elements easily.
Example
List<string> employees = new List<string>();
employees.Add("John");
employees.Add("Priya");
Example 2: Display List Data in ASP.NET WebForm
ASPX Page (ListExample.aspx)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ListExample.aspx.cs" Inherits="WebFormsDemo.ListExample" %>
<!DOCTYPE html>
<html>
<head>
<title>List Example in ASP.NET</title>
</head>
<body>
<h2>List Example: Employee Details</h2>
<asp:Button ID="btnAdd" runat="server" Text="Add New Employee" OnClick="btnAdd_Click" />
<br /><br />
<asp:GridView ID="GridViewEmployees" runat="server" AutoGenerateColumns="true"></asp:GridView>
</body>
</html>

Join the conversation! Your thoughts help the community grow.