黑马程序员之C#学习笔记:C#索引器与属性的区别、索引器的具体使用
来源:互联网 发布:java与或非逻辑符号 编辑:程序博客网 时间:2024/05/22 13:41
--------------------------------------------------- 2345王牌技术员联盟、2345王牌技术员联盟、期待与您交流!---------------------------------------------------------
C#中索引器和属性类似,但也有一些区别:
using System;
using System.Collections.Generic;
using System.Text;
namespace Index
{
class sample<T> //这个类告诉我们如何使用客户端代码索引器
{
private T[] arr = new T[100];
public T this[int i] //索引器的签名由其形参的数量和类型组成。
{
get { return arr;}
set { arr = value;}
}
}
class IndexerClass
{
private int[] arr = new int[100]; //定义数组
public int this[int index] //索引器声明
{
get
{
if (index < 0 || index >100)
return 0;
return arr[index];
}
set
{
if (!(index < 0 || index > 100)) arr[index] = value;
}
}
}
class String_index
{
string[] days = { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };
private int GetDay(string testday)
{
int i = 0;
foreach(string day in days)
{
if (day == testday)
return i;
i++;
}
return -1;
}
public int this[string day]
{
get
{
return GetDay(day);
}
}
}
class Program
{
static void Main(string[] args)
{
sample<string> string_value = new sample<string>();
string_value[0] = "Hello world";
System.Console.WriteLine(string_value[0]);
indexerClass test = new IndexerClass();
//调用索引器初始化第2、4个数据
test[3] = 123;
test[5] = 1024;
for (int i = 0; i <= 10; i++ )
{
System.Console.WriteLine("数据为#{0} = {1}", i, test);
}
String_index week = new String_index();
System.Console.WriteLine("这是一周的第{0}天", week["Tues"]);
Console.ReadKey();
}
}
}
--------------------------------------------------- 2345王牌技术员联盟、2345王牌技术员联盟、期待与您交流!---------------------------------------------------------
- 黑马程序员之C#学习笔记:C#索引器与属性的区别、索引器的具体使用
- 黑马程序员之C#学习笔记:C#中属性与索引的区别
- C#索引器与属性的区别、索引器的具体使用
- C# 索引器与属性的区别
- c#笔记总结--属性与索引器的区别
- c#笔记总结--属性与索引器的区别
- C#索引器-索引器和属性的区别
- 【黑马程序员】C#之索引器
- C#索引器-索引器与数组属性的比较
- 黑马程序员-C#索引器
- 黑马程序员-C#索引器
- C#索引器与属性的区别,以及索引器使用方法
- C#之属性与索引器
- C#索引器与属性
- 细谈C#中的索引器跟属性的区别
- 细谈C#中的索引器跟属性的区别
- C#索引器的使用
- c#索引器的使用
- 如何在linux内核中调用用户空间的程序
- exit(),_exit()与return的比较与浅析
- Java 中正确使用 hashCode 和 equals 方法
- libcurl - curl_easy_setopt()(未完,待续)
- 设置时间格式的JSpinner
- 黑马程序员之C#学习笔记:C#索引器与属性的区别、索引器的具体使用
- Fortune-telling with camomile
- 输入身份证自动生成出生日期(2)
- VS2008 V2010 平台迁移中:error C2440: "static_cast"
- 小练1
- 堆栈的详解
- 电话情结
- jsp作业(1)--使用jsp+servlet完成一个表单验证
- aidl项目笔记-入门