Step 2: Code source
ClassIndexer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for ClassIndexer
/// </summary>
public interface InterfaceIndexer
{
// Indexer declaration:
int this[int index] { get; set;
}
}
public class ClassIndexer : InterfaceIndexer
{
public String strMsg; public string offLimit;
public ClassIndexer()
{
//
// TODO: Add constructor logic here
//
}
// Declare an array to store the data elements.
private int[] arr = new int[5];
public int this[int index] // indexer declaration
{
get
{
// Check the index limits.
if (index < 0 || index >= 5)
{
strMsg = (string)index.ToString();
offLimit = " Data off limit";
return 0;
}
//
else
{
return arr[index];
}
}
set
{
if (!(index < 0 || index >= 5))
arr[index] = value ;
}
}
}
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ClassIndexer cis = new ClassIndexer();
cis[0] = ("Hello, World").Length;
cis[1] = ("Good, World <br/>").Length;
cis[2] = ("Again, World <br/>").Length;
cis[3] = ("").Length;
L1.Text += (cis[0])+ " char long ," + (cis[1])+" char long ," +
(cis[2])+"char long," +(cis[3])+" long<br/> -----" ;
L1.Text += "<br/>Total Char "+ ( (cis[0]) + (cis[1]) + (cis[2])) ;
//
ClassIndexer test = new ClassIndexer();
L1.Text += "<br/>------------";
for (int i = 0; i < 7; i++)
{
test[i] = i;
if(test[i]<= 5){ L1.Text += "<br/>Round " + test[i].ToString() +
test.offLimit;}
else { L1.Text += "<br/>----Round off limit returned " +
test[i].ToString() + test.offLimit; }
}
L1.Text += "<br/>------------";
for (int i = 0; i < 7; i++)
{
L1.Text+=( "<br/>Element x 10 "+ "round " + i+ " OutPut : " + test[i]*10
);
}
}
}
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>mm.IndexerInterface1</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="L1" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>