C#索引器简单实例代码
public class Fruit
{
string peach = "a round juicy fruit that has a soft yellow or red skin and a large hard seed in the center, or the tree that this fruit grows on";
string orange = "a round fruit that has a thick orange skin and is divided into parts inside";
string banana = "a long curved tropical fruit with a yellow skin";
string apple = "a hard round fruit that has red, light green, or yellow skin and is white inside ";
public string this[string fruitName]
{
get
{
switch (fruitName)
{
case "peach":
return peach;
case "orange":
return orange;
case "banana":
return banana;
case "apple":
return apple;
default:
throw new Exception("wrong fruit name");
}
}
set
{
switch (fruitName)
{
case "peach":
peach = value;
break;
case "orange":
orange = value;
break;
case "banana":
banana = value;
break;
case "apple":
apple = value;
break;
default:
throw new Exception("wrong fruit name");
}
}
}
}
class Program
{
static void Main(string[] args)
{
Fruit f = new Fruit();
//关联数组的方式访问get方法
Console.WriteLine(f["peach"]);
//关联数组的方式访问set方法
f["peach"] = "I like to eat peach.";
Console.WriteLine(f["peach"]);
Console.ReadLine();
}
}
相关推荐
-
C#索引器介绍
索引器是一种特殊的类成员,它能够让对象以类似数组的方式来存取,使程序看起来更为直观,更容易编写. 1.索引器的定义 C#中的类成员可以是任意类型,包括数组和集合.当一个类包含了数组和集合成员时,索引器将大大简化对数组或集合成员的存取操作. 定义索引器的方式与定义属性有些类似,其一般形式如下: 复制代码 代码如下: [修饰符] 数据类型 this[索引类型 index] { get{//获得属性的代码} set{ //设置属性的代码} } 修饰符包括 public,protected,privat
-
C#入门之索引器使用实例
本文实例展示了C#索引器的使用方法,对于C#的初学者来说是很有必要熟练掌握的,具体用法如下: 首先,索引器(Indexer)是C#引入的一个新型的类成员,它使得类中的对象可以像数组那样方便.直观的被引用.索引器非常类似于属性,但索引器可以有参数列表,且只能作用在实例对象上,而不能在类上直接作用.定义了索引器的类可以让您像访问数组一样的使用 [ ] 运算符访问类的成员.(当然高级的应用还有很多,比如说可以把数组通过索引器映射出去等等) 索引器的语法如下: 1.它可以接受1个或多个参数 2.使用th
-
C#实现Ruby的负数索引器
C#实现Ruby的负数索引器 public class InvertibleList<T> : List<T> { public new T this[int index] { get { if (index >= 0) return base[index]; if (Count + index < 0) throw new IndexOutOfRangeException(); return this[Count + index]; } set { if (index
-
深入理解C#索引器(一种支持参数的属性)与属性的对比
索引器是一种特殊的类成员,它能够让对象以类似数组的方式来存取,使程序看起来更为直观,更容易编写. 1.索引器的定义C#中的类成员可以是任意类型,包括数组和集合.当一个类包含了数组和集合成员时,索引器将大大简化对数组或集合成员的存取操作.定义索引器的方式与定义属性有些类似,其一般形式如下:时,索引器//this表示的是操作本对象的数组或集合成员,可以简单把它理解成索引器的名字,所以,当相同类型的时候,记得通过参数区分. 复制代码 代码如下: //[修饰符] 数据类型 this[索引类型 index
-
c#索引器详解示例
1.索引器的定义 C#中的类成员可以是任意类型,包括数组和集合.当一个类包含了数组和集合成员时,索引器将大大简化对数组或集合成员的存取操作. 定义索引器的方式与定义属性有些类似,其一般形式如下: [修饰符] 数据类型 this[索引类型 index] 复制代码 代码如下: { get{//获得属性的代码} set{ //设置属性的代码}} 修饰符包括 public,protected,p
-
C#索引器简单实例代码
复制代码 代码如下: public class Fruit { string peach = "a round juicy fruit that has a soft yellow or red skin and a large hard seed in the center, or the tree that this fruit grows on"; string orange = "a round fruit that has a thick orange skin a
-
Handler制作简单相册查看器的实例代码
Handler类简介 在Android平台中,新启动的线程是无法访问Activity里的Widget的,当然也不能将运行状态外送出来,这就需要有Handler机制进行信息的传递了,Handler类位于android.os包下,主要的功能是完成Activity的Widget与应用程序中线程之间的交互. 开发带有Handler类的程序步骤如下: 1. 在Activity或Activity的Widget中开发Handler类的对象,并重写handlerMessage方法. 2. 在新启动的线程中调用s
-
Java web的读取Excel简单实例代码
目录结构: Data.xls数据: 后台页面: public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //System.out.println(this.getServletContext().getRealPath ("/")); try{ Workbook wb = Workbook.getWorkbook(
-
分享Ajax创建简单实例代码
XmlHttp是一套可以在Javascript.VbScript.Jscript等脚本语言中通过http协议传送或从接收XML及其他数据的一套API.XmlHttp最大的用处是可以更新网页的部分内容而不需要刷新整个页面.几乎所有的浏览器都支持XMLHttpRequest对象,它是Ajax应用的核心技术. js代码如下: <html> <head> <title> New Document </title> <meta charset="utf
-
仿百度换肤功能的简单实例代码
效果:(换肤出来一个div,选择你想要的图片,作为网页背景,保存) 要点:cookie保存状态 html代码: <body> <div id="header"> <div id="header_con"> <div class="dbg"><a href="javascript:;" onclick="showImgBox()">换肤</a&
-
matplotlib简介,安装和简单实例代码
官网介绍: Matplotlib is a Python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. Matplotlib can be used in Python scripts, the Python and IPython shell, the ju
-
vue使用混入定义全局变量、函数、筛选器的实例代码
说一种没人发的,利用混入mixins来实现全局变量和函数.mixins里面的方法.变量.筛选器会和组件里面的方法.变量.筛选器合并.这种方法优点是ide会有方法.变量.筛选器提示. 一.main.js文件 import Vue from 'vue' import App from './App' import router from './router' import store from './store' import mixin from './utils/mixin' Vue.proto
-
Vue验证码60秒倒计时功能简单实例代码
template <template> <div class='login'> <div class="loginHeader"> <input type="tel" class="loginBtn border-bottom" placeholder="请输入手机号" /> <input type="tel" class="codeBtn&q
-
SpringBoot配置拦截器方式实例代码
步骤: 1.实现WebMvcConfigurer配置类 2.实现拦截器 3 . 把拦截器添加到配置中 4.添加需要拦截的请求 5.添加需要排除的请求 package com.zp.springbootdemo.interceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springfr
随机推荐
- 浅析ThinkPHP的模板输出功能
- sublime text 3配置使用python操作方法
- 教你自动恢复MySQL数据库的日志文件(binlog)
- jquery事件机制扩展插件 jquery鼠标右键事件。
- python字符串编码识别模块chardet简单应用
- 设置ASP.NET页面的运行超时时间详细到单个页面及站点
- datalist,Repeater和Gridview的区别分析
- JS实现模仿微博发布效果实例代码
- php SQLite学习笔记与常见问题分析第1/2页
- Android全局获取Context实例详解
- Joomla开启SEF的方法
- Sql Server使用cursor处理重复数据过程详解
- jquery 模拟类搜索框自动完成搜索提示功能(改进)
- JavaScript中的函数重载深入理解
- Cors实现java后端完全跨域实例
- Linux操作系统添加新硬盘方法
- C++设计模式之组合模式
- Android GridView实现动画效果实现代码
- C#自定义的字符串操作增强类实例
- php反射应用示例
