在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
使用索引时,另外还有一些注意事项如下:
声明索引:声明索引的语法如此下,请注意以下几点:
声明索引类似于声明属性。下图有他们的相同点和不同点:
索引的set访问器:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { public class Employee { public string FirstName; public string Lastname; public string CityOfBirth; /// <summary> /// 创建索引 /// </summary> /// <param name="index"></param> /// <returns></returns> public string this[int index] { set { switch (index) { case 0: FirstName = value; break; case 1: Lastname = value; break; case 2: CityOfBirth = value; break; default: throw new ArgumentOutOfRangeException("index"); } } get { switch (index) { case 0: return FirstName; case 1: return Lastname; case 2: return CityOfBirth; default: throw new ArgumentOutOfRangeException("index"); } } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 索引复习 { class Program { static void Main(string[] args) { Student stu = new Student() { firstName="c", lastName="b", cityOfBirth="w" }; //测试索引的访问 Console.WriteLine("firstName={0}",stu[0]); Console.ReadKey(); } } }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论