不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
让C#类支持关联数组
编辑:dnawo 日期:2010-01-05
复制内容到剪贴板 程序代码
using System;
using System.Collections;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TestClass t = new TestClass();
//添加遍历
t["aaa"] = "111";
t["bbb"] = "222";
t["ccc"] = "333";
foreach (string item in t)
{
Console.WriteLine(item + "=" + t[item]);
}
//删除再遍历
t["bbb"] = null;
foreach (string item in t)
{
Console.WriteLine(item + "=" + t[item]);
}
Console.ReadKey();
}
}
/// <summary>
/// 实现关联数组的类
/// </summary>
class TestClass : IEnumerable, IEnumerator
{
Dictionary<string, string> _dict = new Dictionary<string, string>();
List<string> _list = new List<string>();
public TestClass()
{ }
public string this[string name]
{
get
{
if (_dict.ContainsKey(name))
return _dict[name];
else
return string.Empty;
}
set
{
if (string.IsNullOrEmpty(name))
throw new ArgumentException("键不能为空。");
else
{
if (!_dict.ContainsKey(name))
{
_dict.Add(name, value);
_list.Add(name);
}
else
{
if (value == null || string.IsNullOrEmpty(value)) //值为空时删除
{
_dict.Remove(name);
_list.Remove(name);
}
else
{
_dict[name] = value;
}
}
}
}
}
//IEnumerable实现部分
public IEnumerator GetEnumerator()
{
return (IEnumerator)this;
}
//IEnumerator实现部分
private int position = -1;
public bool MoveNext()
{
position++;
if (position < _list.Count)
return true;
else
{
Reset();
return false;
}
}
public void Reset()
{
position = -1;
}
public object Current
{
get
{
try
{
return _list[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}
}
using System.Collections;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TestClass t = new TestClass();
//添加遍历
t["aaa"] = "111";
t["bbb"] = "222";
t["ccc"] = "333";
foreach (string item in t)
{
Console.WriteLine(item + "=" + t[item]);
}
//删除再遍历
t["bbb"] = null;
foreach (string item in t)
{
Console.WriteLine(item + "=" + t[item]);
}
Console.ReadKey();
}
}
/// <summary>
/// 实现关联数组的类
/// </summary>
class TestClass : IEnumerable, IEnumerator
{
Dictionary<string, string> _dict = new Dictionary<string, string>();
List<string> _list = new List<string>();
public TestClass()
{ }
public string this[string name]
{
get
{
if (_dict.ContainsKey(name))
return _dict[name];
else
return string.Empty;
}
set
{
if (string.IsNullOrEmpty(name))
throw new ArgumentException("键不能为空。");
else
{
if (!_dict.ContainsKey(name))
{
_dict.Add(name, value);
_list.Add(name);
}
else
{
if (value == null || string.IsNullOrEmpty(value)) //值为空时删除
{
_dict.Remove(name);
_list.Remove(name);
}
else
{
_dict[name] = value;
}
}
}
}
}
//IEnumerable实现部分
public IEnumerator GetEnumerator()
{
return (IEnumerator)this;
}
//IEnumerator实现部分
private int position = -1;
public bool MoveNext()
{
position++;
if (position < _list.Count)
return true;
else
{
Reset();
return false;
}
}
public void Reset()
{
position = -1;
}
public object Current
{
get
{
try
{
return _list[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}
}
上一篇: Effective C# 原则34:创建大容量的Web API
下一篇: time命令无法修改时间,提示:系统无法接受输入的时间
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
最新日志:
下一篇: time命令无法修改时间,提示:系统无法接受输入的时间
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
最新日志:
评论: 0 | 引用: 0 | 查看次数: 6355
发表评论
请登录后再发表评论!