C# -- 索引器、枚举类型

C# -- 索引器、枚举类型,第1张

概述C# -- 索引器、枚举类型索引器允许类或结构的实例就像数组一样进行索引。 无需显式指定类型或实例成员,即可设置或检索索引值。 索引器类似于属性,不同之处在于它们的访问器需要使用参数。1. 索引器class Program{static void Main(string[] args){MyBooks myBk = new MyBooks(); ;for (int i = 0; i < 7; i++){myBk[i] = "书籍" + i.ToString();}for (int j = 0; j <7; j++){Console.Write(myBk[j]+" ");}Console.ReadKey();}}class MyBooks{private string [] arr = new string[7];public string this[int i]{get { return arr[i]; }set { arr[i] = value; }}} 运行结果: 2. 枚举类型枚举类型是包含一组已命名常量的独特值类型。 需要定义包含一组离散值的类型时,可以定义枚举。class Program{static void Main(string[] args){PrintColor(MyColor.Green);Console.ReadKey();}static void PrintColor(MyColor color){switch (color){case MyColor.Red:Console.WriteLine("红色");break;case MyColor.Green:Console.WriteLine("绿色");break;case MyColor.Blue:Console.WriteLine("蓝色");break;default:Console.WriteLine("红绿蓝之外的颜色");break;}}}enum MyColor{Red,Green,Blue}运行结果: 

C# -- 索引器、枚举类型

索引器允许类或结构的实例就像数组一样进行索引。 无需显式指定类型或实例成员,即可设置或检索索引值。 索引器类似于属性,不同之处在于它们的访问器需要使用参数。

1. 索引器

Main(= ( i = ; i < ; i++= + </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;for</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;int</span> j = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;0</span>; j <<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;7</span>; j++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;) { 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"&gt;"</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;); } 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"&gt;class</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt; MyBooks{ </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;private</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;string</span> [] arr = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;new</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;string</span>[<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080"&gt;7</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;]; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;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"&gt;string</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;this</span>[<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt; i] { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;get</span> { <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;return</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt; arr[i]; } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;set</span> { arr[i] =<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt; value; } }}</span></pre>

 运行结果:

2. 枚举类型

枚举类型是包含一组已命名常量的独特值类型。 需要定义包含一组离散值的类型时,可以定义枚举。

Main( } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;static</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;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"&gt; 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"&gt;switch</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt; (<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"&gt;case</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt; 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"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;红色</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;break</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;case</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt; 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"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;<a href="https://m.jb51.cc/tag/lvse/" target="_blank" >绿色</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;break</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;case</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt; 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"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;蓝色</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;break</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;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"&gt;: 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"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;红绿蓝之外的颜色</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;break</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt;; } }}</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff"&gt;enum</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000"&gt; My<a href="https://m.jb51.cc/tag/color/" target="_blank" >color</a>{ Red,Green,Blue}</span></pre>

运行结果:

总结

以上是内存溢出为你收集整理的C# -- 索引器、枚举类型全部内容,希望文章能够帮你解决C# -- 索引器、枚举类型所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1264224.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存