Events provide the ability to send notifications to the outside world for instances of classes and classes, whereas index indicators can index objects like arrays. In C and C + +, there is no concept of events and index indicators, which were first proposed in C #.
13.1 C # events (event)
13.2 Index Indicator
Index indicators (indexer) can use subscript for objects like arrays. It provides us with a way to easily access the data information of a class through indexing.
Statement of 13.2.1 C # index indicator
13.2.2 instance
This example gives a simple example of using an index indicator. Example is a web application that resolves an IP address based on a domain name.
Program Listing 13-5:
Using System;
Using System.Net;
Class Resolvedns
{
ipaddress[] m_arrayips;
public void Resolve (string s_host) {
iphostentry ip=dns. gethostbyname (s_host);
M_arrayips=ip. AddressList;
}
Public IPAddress This[int nindex]{
get{return
m_arrayips[nindex];
}
public int iplength{
get{return
m_arrayips.length
}
} Class TestApp
{public
static void Main ()
{
resolvedns resolver1=new resolvedns ();
Resolve1. Resove (www.sohu.com);
int N=resolver1. Iplength;
Console.wrieline ("Get the IP address of the host");
Console.WriteLine ();
for (int i=0;i〈n;i++)
Console.WriteLine (Resover1[i]);
}
A few instructions for the program:
The host name can be resolved using the DNS class in the System.Net namespace. A static method gethostbyname is provided in the DNS class, which returns a Iphostentry object that contains a list of IP addresses.
When compiling the program, you must declare the containing System.Net namespace in the compiler:
Csc/r:system.net.dll/out:resolver.exe Resover.cs
The compilation parameters for the CSC can be browsed using csc/.