Events and index indicators for C #

Source: Internet
Author: User

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/.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.