实现索引和QUOT;运营商QUOT;在一类在C# [英] Implementing indexing "operator" in on a class in C#

查看:70
本文介绍了实现索引和QUOT;运营商QUOT;在一类在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个人怎么会去执行索引经营者一类在C#中?

How would one go about implementing the indexing "operator" on a class in C# ?

class Foo {

}

Foo f = new Foo();
f["key"] = "some val";
f["other key"] = "some other val";

在C#?搜索MSDN但空手而归。

in C# ? Searched MSDN but came up empty.

推荐答案

下面是使用字典作为存储为例:

Here is an example using a dictionary as storage:

public class Foo {

	private Dictionary<string, string> _items;

	public Foo() {
		_items = new Dictionary<string, string>();
	}

	public string this[string key] {
		get {
			return _items[key];
		}
		set {
			_items[key]=value;
		}
	}

}

这篇关于实现索引和QUOT;运营商QUOT;在一类在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆