创建一个类,它的一个成员是字符串数组FriendsAddr.该数组应包含五个元素.为该数组构建一个字符串索引器.在Main()函数中,执行以下操作:
a.用朋友的姓名作为索引,在数组中接受五个地址作为元素.
(提示:使用get访问器)
b.使用索引器循环访问数组(使用foreach循环),以便打印朋友的地址(数组的元素)和名称.

解决方案 »

  1.   

    public class FriendsAddr
        {
            string[] friends =new string[5];        public FriendsAddr(string a, string b, string c, string d, string e)
            {
                friends[0] = a;
                friends[1] = b;
                friends[2] = c;
                friends[3] = d;
                friends[4] = e;
            }        public string this[int index]
            {
                get
                {
                    return ((index < 0 || index >= friends.Length) ?"" : friends[index]);
                }
                set
                {
                    if (index >= 0 && index <= friends.Length)
                        friends[index] = value;
                }
            }        public string this[string name]
            {
                get
                {
                    int i = 0;
                    while (i < friends.Length && name.ToLower() != friends[i])
                    {
                        i++;
                    }
                    return (i == friends.Length ? "" : friends[i]);
                }
                set
                {
                    int i = 0;
                    while (i < friends.Length && name.ToLower() != friends[i])
                    {
                        i++;
                    }                if (i != friends.Length)
                        friends[i] = value;
                }
            }
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.Text;namespace FriendsAddr1
    {
        public class FriendsAddr
        {
            public string[] friends = new string[5];//默认为private        public FriendsAddr(string a, string b, string c, string d, string e)
            {
                friends[0] = a;
                friends[1] = b;
                friends[2] = c;
                friends[3] = d;
                friends[4] = e;
            }        public string this[int index]
            {
                get
                {
                    return ((index < 0 || index >= friends.Length) ?"" : friends[index]);
                }
                set
                {
                    if (index >= 0 && index <= friends.Length)
                        friends[index] = value;
                }
            }        public string this[string name]
            {
                get
                {
                    int i = 0;
                    while (i < friends.Length && name.ToLower() != friends[i])
                    {
                        i++;
                    }
                    return (i == friends.Length ? "" : friends[i]);
                }
                set
                {
                    int i = 0;
                    while (i < friends.Length && name.ToLower() != friends[i])
                    {
                        i++;
                    }                if (i != friends.Length)
                        friends[i] = value;
                }
            }
        
        }    class Program
        {
            static void Main(string[] args)
            {
                FriendsAddr fa = new FriendsAddr("a", "b", "c", "d", "e");
                Console.WriteLine(fa[0]);
                Console.WriteLine(fa["c"]);            foreach (string s in fa.friends)
                    Console.WriteLine(s);
            }
        }
    }
    给分
      

  3.   

    不符合题意啊!!!创建一个类,它的一个成员是字符串数组FriendsAddr.该数组应包含五个元素.为该数组构建一个字符串索引器.在Main()函数中,执行以下操作:
    a.用朋友的姓名作为索引,在数组中接受五个地址作为元素.
    (提示:使用get访问器)
    b.使用索引器循环访问数组(使用foreach循环),以便打印朋友的地址(数组的元素)和名称.