创建一个类,它的一个成员是字符串数组FriendsAddr.该数组应包含五个元素.为该数组构建一个字符串索引器.在Main()函数中,执行以下操作:
a.用朋友的姓名作为索引,在数组中接受五个地址作为元素.
(提示:使用get访问器)
b.使用索引器循环访问数组(使用foreach循环),以便打印朋友的地址(数组的元素)和名称.
a.用朋友的姓名作为索引,在数组中接受五个地址作为元素.
(提示:使用get访问器)
b.使用索引器循环访问数组(使用foreach循环),以便打印朋友的地址(数组的元素)和名称.
解决方案 »
- 请看过《C#入门经典》的朋友解惑
- 使用nhibernate 时cannot be specified as proxy
- c#sqlserver中生日提示,sql语句如何写
- 登陆窗体问题,
- 如何在DataGrid中不显示指定列中的重复值?
- 在ICSharpCode.SharpZipLib.dll这个包里可不可以对zip包进行"增删改查"?
- ~~~各位大虾,一定要帮帮小妹!!!如何在用C#调用outlook时如何去掉首次接收邮件和发送邮件的对话框
- 请教网络编程问题
- 入门问题:C#中方法的参数可以有默认值吗?
- c#,的一切皆对象,好象没有其它的语言可比喃
- c# event和delegate的应用?
- 从Listbox中取值的问题
{
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;
}
}
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);
}
}
}
给分
a.用朋友的姓名作为索引,在数组中接受五个地址作为元素.
(提示:使用get访问器)
b.使用索引器循环访问数组(使用foreach循环),以便打印朋友的地址(数组的元素)和名称.