-
IEnumerable IEnumerator
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Collections;
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
namespace ConsoleApplication2
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
9
public class ListBoxList : IEnumerable<string>
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
11
private string[] strings;
12
private int ctr = 0;
13
//Enumerable class can return an enumerator
14
public IEnumerator<string> GetEnumerator()
15![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
16
foreach (string s in strings)
17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
18
yield return s;
19
}
20
}
21
//Explicit interface implemention
22
IEnumerator IEnumerable.GetEnumerator()
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
24
return GetEnumerator();
25
}
26
//initialize the listbox with string
27
public ListBoxList(params string[] initialString)
28![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
29
strings = new String[8];
30
//copy the strings passed into the constructor
31
foreach (string s in initialString)
32![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
33
strings[ctr++] = s;
34
}
35
}
36
//add a single string to the end of the listbox
37
public void Add(string theString)
38![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
39
strings[ctr] = theString;
40
ctr++;
41
}
42
//allow array-like access
43
public string this[int index]
44![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
45![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
get
47![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
48
if (index < 0 || index >= strings.Length)
49![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
50
//handle the index
51
} return strings[index];
52
}
53
set
54![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
55
strings[index] = value;
56
}
57
}
58
//publish howmany strings you holds
59
public int GetEnmEntries()
60![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
61
return ctr;
62
}
63
}
64
class Program
65![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
66
static void Main(string[] args)
67![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
68
//create a new listboxlist and initalize
69
ListBoxList lbt = new ListBoxList("hello","world");
70
lbt.Add("Who");
71
lbt.Add("Is");
72
lbt.Add("Douglas");
73
lbt.Add("Adams");
74
string subst = "Universe";
75
lbt[1] = subst;
76
//accexx the listboxlist
77
foreach (string s in lbt)
78![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
79
Console.WriteLine(s);
80
}
81![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
82
Console.ReadKey();
83
}
84
}
85
}
86![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-
相关阅读:
Redis实现分布式锁
Redis数据结构
Mysql与redis缓存一致性
mysql分库分表
mysql主从同步
mysql配置优化
Netty 参数优化
JAVA多线程之park & unpack
网络时钟服务器(网络校时服务器)无法同步的排查方法
GPS北斗共视授时中的多径效应分析
-
原文地址:https://www.cnblogs.com/binlyzhuo/p/1433025.html
Copyright © 2020-2023
润新知