C#索引怎么用

发布时间:2021-12-03 10:04:36 作者:小新
来源:亿速云 阅读:113

小编给大家分享一下C#索引怎么用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

下面是贴出来的代码:

using System;   using System.Collections.Generic;   using System.Linq;   using System.Text;      namespace index   {           class Worker           {                   public string LastName;                   public string FirstName;                   public string MyBirth;                      public string this[int index]                   {                           set                           {                                   switch (index)                                   {                                           case 0: LastName = value;                                                   break;                                           case 1: FirstName = value;                                                   break;                                           case 2: MyBirth = value;                                                   break;                                           default:                                                   throw new ArgumentOutOfRangeException("index");                                                   break;                                   }                           }                           get                           {                                   switch(index)                                   {                                           case 0 : return LastName;                                           case 1 : return FirstName;                                           case 2 : return MyBirth;                                           default :                                                      throw new ArgumentOutOfRangeException("index");                                                   break;                                   }                                                                      }                   }           }           class Program           {                   static void Main(string[] args)                   {                           Worker a = new Worker();                           Console.WriteLine("print the value:{0},{1},{2}",a[0],a[1],a[2]);                           Console.WriteLine("please print your last name");                           a[0] = Console.ReadLine();                           Console.WriteLine("please print your first name");                           a[1] = Console.ReadLine();                           Console.WriteLine("please print your birthday");                           a[2] = Console.ReadLine();                           Console.WriteLine("Now,your name is {0},{1},and your birth is {2}",a[0],a[1],a[2]);                      }           }   }

首先什么是C#索引呢?

书上说它是一组get和set访问器,我个人就直接这么认为就是获值或设值的概念。(可能是错误的啊,呵呵,理论太差,刚看的)。

怎样声明C#索引呢?

他的语法是如下:

要注意下面几点:a:索引没有名称,它是通过关键字this。

b:参数列表在方括号里面。

c:参数列表至少必须声明一个参数。

ReturnType this [type param1,...]   {           get                   {                           ...                   }           set                   {                           ...                   }   }

以上是“C#索引怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. c#索引器
  2. C#索引器是什么

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

上一篇:C#的命名规范有哪些

下一篇:tk.Mybatis插入数据获取Id怎么实现

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》