C#索引器使用
来源:互联网 发布:定制家具效果图软件 编辑:程序博客网 时间:2024/04/30 03:52
索引器
索引器用于封装内部集合或数组。
索引器在语法上方便了程序员将类、结构或接口作为数组进行访问。
要声明类或结构上的索引器,需要使用this关键字。
例如:
public int this[int index] // 索引器声明
{
// get and set accessors
}
//////////////////////////////////////////////////////////////////////////
//索引器使用
using System;
namespace IndexExample{
class DayCollection {
string[] days = { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };
public int this[string day] {
get {
return (GetDay(day));
}
}
public string this[int i] {
get {
return (days[i]);
}
}
private int GetDay(string testDay)
{
int i = 0;
foreach (string day in days)
{
if (day == testDay)
{
return i;
}
i++;
}
return -1;
}
}
class Program
{
static void Main(string[] args)
{
DayCollection week = new DayCollection();
Console.WriteLine(week[1]);
Console.WriteLine(week["Fri"]);
Console.WriteLine(week["Other Day"]);
Console.ReadLine();
}
}
}
- C#索引器使用
- C#索引器的使用
- c#索引器的使用
- C#索引器的使用
- C# 索引器的使用
- c#中索引器的使用
- 使用索引器(C# 编程指南)
- c#中索引器的使用
- C# 类中索引器的使用
- C# 类中索引器的使用
- C# 类中索引器的使用
- C# 类中索引器的使用
- C# 类中索引器的使用
- C# 类中索引器的使用
- C#类中索引器的使用
- C#中索引器简单使用
- C#索引器
- C#索引器
- 约瑟夫环问题(C与C++混合版)
- Unity 3D GUI控制
- Assignment 7: Shortest Path Algorithms
- 总结OnOK()、OnCancel()、OnClose()、OnDestroy()之间的区别
- Unity Manual用户手册-创建游戏
- C#索引器使用
- C#动态时钟
- C#银行开户模拟
- 按键映射位置
- 日期类的实现(值调用,引用调用)
- C#图形面积计算(重载)
- C#中override重写与new隐藏的区别,以及C#与Java的Override区别[转载]
- C#委托,delgete
- C#序列化与反序列化