C#中的索引器
在Java中,一般会这样使用get,set方法:
class Person{
private String name;
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
}
public static void main(String [] args){
Person person = new Person();
person.setName("test");
String name = person.getName();
}
其中get,set是可以传参的,但是在C#中,我们一般使用属性的get,set,一般使用如下:
class Person{
private string name {set; get};
}
public static void Main(){
Person person = new Person();
person.name = "test";
string name = person.name;
}
可以看到这里面set,get没有参数,但是如果我们需要传入参数怎么办?在C#中有类似Java的语法,叫做索引器[Index]:
//索引器
element-type this[int index]
{
// get 访问器
get
{
// 返回 index 指定的值
} // set 访问器
set
{
// 设置 index 指定的值
}
}
索引器使用方法如下:
using System;
namespace IndexerApplication
{
class IndexedNames
{
private string[] namelist = new string[size];
static public int size = ;
public IndexedNames()
{
for (int i = ; i < size; i++)
namelist[i] = "N. A.";
}
public string this[int index]
{
get
{
string tmp; if( index >= && index <= size- )
{
tmp = namelist[index];
}
else
{
tmp = "";
} return ( tmp );
}
set
{
if( index >= && index <= size- )
{
namelist[index] = value;
}
}
} static void Main(string[] args)
{
IndexedNames names = new IndexedNames();
names[] = "Zara";
names[] = "Riz";
names[] = "Nuha";
names[] = "Asif";
names[] = "Davinder";
names[] = "Sunil";
names[] = "Rubic";
for ( int i = ; i < IndexedNames.size; i++ )
{
Console.WriteLine(names[i]);
}
Console.ReadKey();
}
}
} //输出
/*
Zara
Riz
Nuha
Asif
Davinder
Sunil
Rubic
N. A.
N. A.
N. A.
*/
同时我们可以重载索引器:
using System;
namespace IndexerApplication
{
class IndexedNames
{
private string[] namelist = new string[size];
static public int size = ;
public IndexedNames()
{
for (int i = ; i < size; i++)
{
namelist[i] = "N. A.";
}
}
public string this[int index]
{
get
{
string tmp; if( index >= && index <= size- )
{
tmp = namelist[index];
}
else
{
tmp = "";
} return ( tmp );
}
set
{
if( index >= && index <= size- )
{
namelist[index] = value;
}
}
}
public int this[string name]
{
get
{
int index = ;
while(index < size)
{
if (namelist[index] == name)
{
return index;
}
index++;
}
return index;
} } static void Main(string[] args)
{
IndexedNames names = new IndexedNames();
names[] = "Zara";
names[] = "Riz";
names[] = "Nuha";
names[] = "Asif";
names[] = "Davinder";
names[] = "Sunil";
names[] = "Rubic";
// 使用带有 int 参数的第一个索引器
for (int i = ; i < IndexedNames.size; i++)
{
Console.WriteLine(names[i]);
}
// 使用带有 string 参数的第二个索引器
Console.WriteLine(names["Nuha"]);
Console.ReadKey();
}
}
} //输出
/*
Zara
Riz
Nuha
Asif
Davinder
Sunil
Rubic
N. A.
N. A.
N. A.
2
*/
C#中的索引器的更多相关文章
- C#中的索引器原理
朋友们,还记得我们在C#语言开发中用到过索引器吗? 记得在获得DataGridView控件的某列值时:dgvlist.SelectedRows[0].Cells[0].Value; 记得在获得List ...
- C#中的索引器的简单理解和用法
索引器是一种特殊的类成员,它能够让对象以类似数组的方式来存取,使程序看起来更为直观,更容易编写. 1.索引器的定义 C#中的类成员可以是任意类型,包括数组和集合.当一个类包含了数组和集合成员时,索引器 ...
- C#中的索引器(Indexers)
前两天刚刚学习完了属性,这两天又搂完了索引器,发现两者非常的相似,但是相似之外还有一些不同之处.今天就来总结一下索引器--Indexers 索引器的作用及格式 索引器的作用就是能够使类或者结构体的实例 ...
- C# 接口中的索引器
索引器可在 接口(C# 参考) 上声明.接口索引器的访问器与类索引器的访问器具有以下方面的不同: 接口访问器不使用修饰符. 接口访问器没有体. 因此,访问器的用途是指示索引器是读写.只读还是只写.以下 ...
- 描述一下C#中索引器的实现过程,是否只能根据数字进行索引?
不是.可以用任意类型. 索引器是一种特殊的类成员,它能够让对象以类似数组的方式来存取,使程序看起来更为直观,更容易编写. 1.索引器的定义 C#中的类成员可以是任意类型,包括数组和集合.当一个类包含了 ...
- C# 中常用的索引器
使用 C# 中的索引器和 JavaScript 中访问对象的属性是很相似. 之前了解过索引器,当时还把索引器和属性给记混了, 以为索引器就是属性,下面写下索引器和属性的区别,以及怎么使用索引器 先说明 ...
- C# 索引器,实现IEnumerable接口的GetEnumerator()方法
当自定义类需要实现索引时,可以在类中实现索引器. 用Table作为例子,Table由多个Row组成,Row由多个Cell组成, 我们需要实现自定义的table[0],row[0] 索引器定义格式为 [ ...
- 索引器(C# 编程指南)
原文地址:https://msdn.microsoft.com/zh-cn/library/6x16t2tx(VS.80).aspx 索引器允许类或结构的实例按照与数组相同的方式进行索引.索引器类似于 ...
- C# 索引器简介
索引器是一种特殊的类成员,它能够让对象以类似数组的方式来存取,是程序看起来更为直观,更容易编写. 1.索引器的定义 C#中的类成员可以是任意类型,包括数组和集合.当一个类包含了数组和集合成员时,索引器 ...
随机推荐
- SharePoint 2013 创建 Site Collection
在之前的文章中,通过SharePoint Central Administration 创建了Web Application.在这篇文章中将继续SharePoint 2013之旅——还是以Step B ...
- netty LEAK: ByteBuf.release() was not called before it's garbage-collected
背景.netty抛出完整的error信息如下: 2018-02-08 14:30:43.098 [nioEventLoopGroup-5-1] ERROR io.netty.util.Resource ...
- Java 8 – TemporalAdjusters examples
1. TemporalAdjusters Example to move a date to firstDayOfMonth, firstDayOfNextMonth, next Monday and ...
- Sql Server Compact 4.0数据库部署安装
Sql Server Compact 4.0相比3.5版本增强了很多,支持Entity Framework 4.1,对于轻量级应用来讲,使用Sql Server Compact 4.0是个很好的选择, ...
- fiddler 抓取iphone发出的http和https包
1.清理iphone的描述文件,在通用里面设置.这一步目的防止手机里面已经存在了DO_NOT_TRUST_FiddlerRoot证书,导致后面抓不了包,所以先清理下 2.下载安装fiddler,百度或 ...
- 安装Flume的时候出现File Channel transaction capacity cannot be greater than the capacity of the channel capacity -解决方案 摘自网络
部署flume集群时,在启动collector服务器没报错,启动agent服务器报错: File Channel transaction capacity cannot be greater than ...
- 【剑道】步法(Ashi Sabaki)
转自 http://www.openkendo.com/class7.html 步法(Ashi Sabaki)可能算是剑道中最重要的部分.,以下大致做一归纳讲解,希望能够帮助到各位新人的练习. “折足 ...
- Android Manifest.xml文件的结构及作用
原文链接:http://android.eoe.cn/topic/android_sdk 每一个应用程序在工程的根目录下必须要有一个AndroidManifest.xml文件(一定要用这个名称).这个 ...
- 彻底删除Cygwin
cygwin是一个好软件,凝聚了大家很多的心血,在win10下运行的很流畅,远比微软自己搞得那个ubuntu顺手,但它有个小问题,重装系统后,如果原来的cgywin文件夹没有删除的话,你会发现你无法删 ...
- 【Unity】第11章 第三人称角色控制器和碰撞体
分类:Unity.C#.VS2015 创建日期:2016-05-02 一.简介 第三人称视角控制器涉及的相关概念有: 1.刚体(Rigidbody). 2.碰撞体(Collider).包括球体碰撞体( ...