当前位置: 首页 > news >正文

视频嵌入网站手机网址大全123客户端下载

视频嵌入网站,手机网址大全123客户端下载,哪里有做网站系统,稿定设计在线制作官网1、索引器定义 什么是索引器 索引器(indexer)是这样一种成员:它使对象能够用与数组相同的方式(即使用下标)进行索引 索引器的声明参见 C# 语言定义文档注意:没有静态索引器 索引器是一组 get 和 set 访问…

1、索引器定义

什么是索引器

  • 索引器(indexer)是这样一种成员:它使对象能够用与数组相同的方式(即使用下标)进行索引
    索引器的声明
  • 参见 C# 语言定义文档
  • 注意:没有静态索引器

索引器是一组 get 和 set 访问器,与属性类似:

  • 和属性一样,索引器不用分配内存来存储
  • 索引器和属性都主要来访问其他数据成员,它们与这些成员关联,并为它们提供获取和设置访问。
  • 属性通常表示单个数据成员
  • 索引器通常表示多个数据成员

注意:

  • 索引器总是实例成员,因此不能被声明为 static。

索引器的特点:

  • 索引器的索引值(Index)类型不受限制
  • 索引器允许重载
  • 索引器不是一个变量

请添加图片描述

2、索引器代码例子

(1)代码例子:采用字典方式

 class Program{static void Main(string[] args){Student stu = new Student();stu["Math"] = 90;var mathScore = stu["Math"];Console.WriteLine(mathScore);}}class Student{private int age;public int Age{get { return age; }set { age = value; }}private Dictionary<string, int> scoreDictionary = new Dictionary<string, int>();public int? this[string subject]{get{if(this.scoreDictionary.ContainsKey(subject)){return this.scoreDictionary[subject];}else{return null;}}set{if (value.HasValue == false){throw new Exception("Score cannot be null.");}if(this.scoreDictionary.ContainsKey(subject)){this.scoreDictionary[subject] = value.Value;//value 为可空类型}else{this.scoreDictionary.Add(subject, value.Value);}}}}

(2)代码例子:采用索引值方式
以下所有例子都来源于:# 索引器的多个例子及重载

class Program{static void Main(string[] args){//索引器的使用IndexerClass Indexer = new IndexerClass();//“=”号右边对索引器赋值,其实就是调用其set方法Indexer[0] = "张三";Indexer[1] = "李四";//输出索引器的值,其实就是调用其get方法Console.WriteLine(Indexer[0]);Console.WriteLine(Indexer[1]);Console.ReadKey();}}public class IndexerClass{private string[] name = new string[2];public string this[int index]{get{if (index < 2){return name[index];}return null;}set{if (index < 2){name[index] = value;}}}}

(3)代码例子:采用字符串为下标方式

    class Program{static void Main(string[] args){IndexerClass Indexer = new IndexerClass();Indexer["A0001"] = "张三";Indexer["A0002"] = "李四";Console.WriteLine(Indexer["A0001"]);Console.WriteLine(Indexer["A0002"]);Console.ReadKey();}}public class IndexerClass{//用string作为索引器下标的时候,要用Hashtableprivate Hashtable name = new Hashtable();//索引器必须以this关键字定义,其实这个this就是类实例化之后的对象public string this[string index]{get { return name[index].ToString(); }set { name.Add(index, value); }}}

3、索引器重载

 class Program{static void Main(string[] args){IndexerClass Indexer = new IndexerClass();//第一种索引器的使用Indexer[1] = "张三";//set访问器的使用Indexer[2] = "李四";Console.WriteLine("编号为1的名字:" + Indexer[1]);//get访问器的使用Console.WriteLine("编号为2的名字:" + Indexer[2]);Console.WriteLine();//第二种索引器的使用Console.WriteLine("张三的编号是:" + Indexer["张三"]);//get访问器的使用Console.WriteLine("李四的编号是:" + Indexer["李四"]);Indexer["王五"] = 3;//set访问器的使用Console.WriteLine("王五的编号是:" + Indexer["王五"]);Console.ReadKey();}}public class IndexerClass{private Hashtable name = new Hashtable();//1:通过key存取Valuespublic string this[int index]{get { return name[index].ToString(); }set { name.Add(index, value); }}//2:通过Values存取keypublic int this[string aName]{get{//Hashtable中实际存放的是DictionaryEntry(字典)类型,如果要遍历一个Hashtable,就需要使用到DictionaryEntryforeach (DictionaryEntry d in name){if (d.Value.ToString() == aName){return Convert.ToInt32(d.Key);}}return -1;}set{name.Add(value, aName);}}}

4、多参索引器

   //入职信息类public class EntrantInfo{//姓名、编号、部门private string name;private int number;private string department;public EntrantInfo(){}public EntrantInfo(string name, int num, string department){this.name = name;this.number = num;this.department = department;}public string Name{get { return name; }set { name = value; }}public int Num{get { return number; }set { number = value; }}public string Department{get { return department; }set { department = value; }}}//声明一个类EntrantInfo的索引器public class IndexerForEntrantInfo{private ArrayList ArrLst;//用于存放EntrantInfo类public IndexerForEntrantInfo(){ArrLst = new ArrayList();}//声明一个索引器:以名字和编号查找存取部门信息public string this[string name, int num]{get{foreach (EntrantInfo en in ArrLst){if (en.Name == name && en.Num == num){return en.Department;}}return null;}set{//new关键字:C#规定,实例化一个类或者调用类的构造函数时,必须使用new关键ArrLst.Add(new EntrantInfo(name, num, value));}}//声明一个索引器:以编号查找名字和部门public ArrayList this[int num]{get{ArrayList temp = new ArrayList();foreach (EntrantInfo en in ArrLst){if (en.Num == num){temp.Add(en);}}return temp;}}//还可以声明多个版本的索引器...}class Program{static void Main(string[] args){IndexerForEntrantInfo Info = new IndexerForEntrantInfo();//this[string name, int num]的使用Info["张三", 101] = "人事部";Info["李四", 102] = "行政部";Console.WriteLine(Info["张三", 101]);Console.WriteLine(Info["李四", 102]);Console.WriteLine();//this[int num]的使用foreach (EntrantInfo en in Info[102]){Console.WriteLine(en.Name);Console.WriteLine(en.Department);}Console.ReadKey();}}
http://www.hkea.cn/news/613740/

相关文章:

  • 正规的网站制作与推广百度广告运营
  • 网站建设估价引擎搜索有哪些
  • 东莞网站建设选菲凡网络如何制作网站
  • 网站收录系统备案查询官网
  • 临朐县网站建设利用搜索引擎营销成功的案例
  • 利用网盘做视频网站镇江优化推广
  • 视频微网站开发哪个公司网站设计好
  • 品网站建设智能搜索引擎
  • 怎样在百度建网站seo建设者
  • 四海网络网站建设咨询什么叫做网络营销
  • 安徽建设网官方网站优化分析
  • 网站根目录文件名游戏推广员是做什么的
  • 个体工商户怎么做网站西安网站seo技术
  • 报名网站制作2345网址导航官网下载安装
  • 图书购物网站开发总结百度发广告需要多少钱
  • 做网站 业务流程图站长统计性宝app
  • 长沙做网站大概多少钱万网域名注册教程
  • 成都网站建设网站产品推广计划书怎么写
  • 深圳个人网站建设大连网络推广公司哪家好
  • 建设工程教育appseo技术培训中心
  • 家教中介怎么利用网站来做的免费广告推广
  • wordpress仿制建设seo是什么平台
  • 商城网站建设分为几块seo臻系统
  • 网络营销对于个人而言有什么作用seo文章
  • 做书籍封皮的网站今日中国新闻
  • 东莞建设网站电工培训技术学校
  • 深圳聘请做网站人员成都排名seo公司
  • 网站备案之后东莞网站关键词优化公司
  • 多种专业网站建设潍坊网站排名提升
  • 网站投稿系统怎么做网站制作流程是什么