C# -- 索引器、枚举类型
索引器允许类或结构的实例就像数组一样进行索引。 无需显式指定类型或实例成员,即可设置或检索索引值。 索引器类似于属性,不同之处在于它们的访问器需要使用参数。
1. 索引器
Main(= ( i = ; i < ; i++= + </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">for</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">int</span> j = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">0</span>; j <<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">7</span>; j++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">) { Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.Write(myBk[j]</span>+<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); } Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.ReadKey(); }}</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">class</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> MyBooks{ </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">private</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">string</span> [] arr = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">new</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">string</span>[<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080">7</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">]; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">pub<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>c</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">string</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">this</span>[<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> i] { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">get</span> { <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">return</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> arr[i]; } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">set</span> { arr[i] =<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> value; } }}</span></pre>
运行结果:
2. 枚举类型
枚举类型是包含一组已命名常量的独特值类型。 需要定义包含一组离散值的类型时,可以定义枚举。
Main( } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">static</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> Print<a href="https://m.jb51.cc/tag/color/" target="_blank" >color</a>(My<a href="https://m.jb51.cc/tag/color/" target="_blank" >color</a> <a href="https://m.jb51.cc/tag/color/" target="_blank" >color</a>) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">switch</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> (<a href="https://m.jb51.cc/tag/color/" target="_blank" >color</a>) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">case</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> My<a href="https://m.jb51.cc/tag/color/" target="_blank" >color</a>.Red: Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.Write<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ne(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">红色</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">break</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">case</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> My<a href="https://m.jb51.cc/tag/color/" target="_blank" >color</a>.Green: Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.Write<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ne(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"><a href="https://m.jb51.cc/tag/lvse/" target="_blank" >绿色</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">break</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">case</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> My<a href="https://m.jb51.cc/tag/color/" target="_blank" >color</a>.Blue: Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.Write<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ne(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">蓝色</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">break</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">defa<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>t</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">: Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.Write<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ne(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">红绿蓝之外的颜色</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">break</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000">; } }}</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff">enum</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"> My<a href="https://m.jb51.cc/tag/color/" target="_blank" >color</a>{ Red,Green,Blue}</span></pre>
运行结果:
总结以上是内存溢出为你收集整理的C# -- 索引器、枚举类型全部内容,希望文章能够帮你解决C# -- 索引器、枚举类型所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)