asp.net c#非常简单的问题,解决马上给分

86y 2013-03-11 08:48:33
Dictionary<string, string>是键和值的集合,那有没有非集合的写法?
我不需要集合,但需要像Dictionary一样能够设置key和value

例如:

public object<string, string> demo(){
return null;
}

以上代码是错误的,没有这种写法
...全文
237 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
86y 2013-03-11
  • 打赏
  • 举报
回复
引用 8 楼 qsw1214 的回复:
Hashtable也是集合类型 C# code?12345678910111213141516171819202122protected void Page_Load(object sender, EventArgs e) { Dictionary<string, string> d = new Dictionary<string, string>(……
不用这么麻烦了,已经解决了
qsw1214 2013-03-11
  • 打赏
  • 举报
回复
Hashtable也是集合类型
protected void Page_Load(object sender, EventArgs e)
    {
        Dictionary<string, string> d = new Dictionary<string, string>();
        d.Add("1", "张三");
        d.Add("2", "李四");
        Hashtable h = new Hashtable();
        h.Add("1", "张三");
        h.Add("2", "李四");
    }
    public myObject<string, string> demo()
    {
        myObject<string, string> m = new myObject<string, string>();
        m.Key = "张三";
        m.Value = "李四";
        return m;
    }
最好还是自定义一个类型!
public class myObject<string, string>
{    
        public string Key { get; set; }
        public string Value { get; set; } 
}
86y 2013-03-11
  • 打赏
  • 举报
回复
引用 3 楼 dalmeeme 的回复:
KeyValuePair<int, string> pair = new KeyValuePair<int, string>();
这就是我想要的
wetcom 2013-03-11
  • 打赏
  • 举报
回复
对了补充一下,键和值,又不想用集合,Hashtable也是集合啊,那估计只能自定义类了。
gxingmin 2013-03-11
  • 打赏
  • 举报
回复
自己定义一个
 public class myObject<TKey, TValue>
    {
        public TKey Key { get; set; }
        public TValue Value { get; set; }
    }

public myObject<string, string> demo(){
    return null;
}
dalmeeme 2013-03-11
  • 打赏
  • 举报
回复
KeyValuePair<int, string> pair = new KeyValuePair<int, string>(3, "你好");
dalmeeme 2013-03-11
  • 打赏
  • 举报
回复
KeyValuePair<int, string> pair = new KeyValuePair<int, string>();
wetcom 2013-03-11
  • 打赏
  • 举报
回复
Hastable也可以
86y 2013-03-11
  • 打赏
  • 举报
回复
对,我不是要这样的

public object demo(string, string){
    return null;
}

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧