• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#索引器:在集合或数组中取出某一个元素举例_【转】

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

Garmmar:

[访问修饰符] 数据类型 this[参数列表]

{

        get

        { 获取索引器的内容 }

        set

        { 设置索引器的内容 }

}

Eg:

 1     <span style="font-size:14px;">using System;  
 2     using System.Collections.Generic;  
 3     using System.Text;  
 4       
 5     namespace IndexerUsing  
 6     {  
 7         class Photo  
 8         {  
 9              
10             private string name;  
11       
12             public string Name  
13             {  
14                 get { return name; }  
15                 set { name = value; }  
16             }  
17             public Photo() { }  
18             public Photo(string name)  
19             {  
20                 this.name = name;  
21             }  
22         }  
23         class Album  
24         {  
25             private Photo[] _photos;  
26             public Album()  
27             { }  
28             public Album(int count)  
29             {  
30                 _photos = new Photo[count];  
31             }  
32             public Photo this[int index]  
33             {  
34                 get  
35                 {  
36                     if (index < 0 || index > _photos.Length)  
37                         return null;  
38                     else  
39                         return _photos[index];  
40                 }  
41                 set  
42                 {  
43                     if (index < 0 || index > _photos.Length)  
44                         return;  
45                     else  
46                         _photos[index] = value;  
47                 }  
48             }  
49         }  
50       
51         class Program  
52         {  
53             static void Main(string[] args)  
54             {  
55                 Album album = new Album(3);  
56                 Photo photo1 = new Photo("王云鹏");  
57                 Photo photo2 = new Photo("黄利云");  
58                 Photo photo3 = new Photo("李文平");  
59                 album[0] = photo1;  
60                 album[1] = photo2;  
61                 album[2] = photo3;  
62                 Console.WriteLine("输入第一张照片:{0}", album[0].Name);  
63       
64             }  
65         }  
66     }  
67     </span>  

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#编码规范[转载]发布时间:2022-07-10
下一篇:
C#树形控件节点拖动发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap