C#中的泛型怎么使用

petit 2005-08-10 11:00:41
c#中的泛型怎么使用,比如Hashtable怎么支持泛型的?
在java中可以这样 Hashtable<String,String> = new
Hashtable(String,String);
C#中就不行,甚至是语法解析不了
...全文
765 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
understand999 2005-08-11
  • 打赏
  • 举报
回复
eform自定义表单平台是一个在IE浏览器中可视化的设计软件界面的工具。无论是输入界面还是报表界面,无论是简单的输入查询还是复杂的逻辑处理。都可以由eform设计出来。

eform自定义表单平台适用于网上OA系统的自定义表单模块,工作流系统的自定义表单模块,信息管理系统方面的软件开发项目等等。


方成eform自定义表单平台,纯BS结构.100%开放源码.可以在 http://218.30.103.110:8080/eform/index.html 在线试用.
foolnet 2005-08-11
  • 打赏
  • 举报
回复
http://www.microsoft.com/china/msdn/library/langtool/vcsharp/csharpgenerics.mspx
julong88 2005-08-11
  • 打赏
  • 举报
回复
mark
claus2001 2005-08-11
  • 打赏
  • 举报
回复
mark
namhyuk 2005-08-10
  • 打赏
  • 举报
回复
Hashtable table = new Hashtable();
table.Add("Sunday", "星期天");
table.Add("Monday", "星期一");
table.Add("Tuesday", "星期二");
...

string cnDay = (string)table["Tuesday"];

Items can also be added to a Hashtable using string indexes:
Hashtable table = new Hashtable();
table["Sunday"] = "星期天";
table["Monday"] = "星期一";
table["Tuesday"] = "星期二";
...

there's a difference between adding items with Add and adding them with indexers. Add throws an exception if the key you pass to it is already in the table. Indexers don't. They simply replace the old value with the new one.
Macosx 2005-08-10
  • 打赏
  • 举报
回复
语法不一样
crossrowman 2005-08-10
  • 打赏
  • 举报
回复
http://www.microsoft.com/china/msdn/library/langtool/vcsharp/csharpgenerics.mspx
TechEye 2005-08-10
  • 打赏
  • 举报
回复
Hashtable<String,String> hash = new Hashtable(String,String)();
应该是这样形式
nik_Amis 2005-08-10
  • 打赏
  • 举报
回复
/[up]
Macosx 2005-08-10
  • 打赏
  • 举报
回复
如果说vs.net 2005有点不准确 那就用.net framework 2.0吧
自己也可以写一个强类型的hashtable 但得加不少代码
petit 2005-08-10
  • 打赏
  • 举报
回复
现在2005还在beta中,等啊~~~。
其实vs.net 2005只是一个ide,c# 2.0 能通过什么方法在2003中使用?
itmingong 2005-08-10
  • 打赏
  • 举报
回复
up
yellowhwb 2005-08-10
  • 打赏
  • 举报
回复
vs.net 2003不支持泛型,vs.net 2005(.net framework 2.0)才支持泛型!
稍微有点无敌 2005-08-10
  • 打赏
  • 举报
回复
应该是在2.0里面,.net1.1不支持泛型的
petit 2005-08-10
  • 打赏
  • 举报
回复
是不是C# 2.0?如果是在2003中怎么使用,是beta版吗/
petit 2005-08-10
  • 打赏
  • 举报
回复
请问 Macosx(总算毕业了) :
在我vs.net 2003 中 没有using System.Collections.Generic;这个命名空间啊,你是用什么版本的?为什么我没有
Macosx 2005-08-10
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;

public class Example
{
public static void Main()
{
// Create a new dictionary of strings, with integer keys.
Dictionary<int, string> d = new Dictionary<int, string>();

// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
d.Add(1, "a");
d.Add(3, "abc");
d.Add(7, "abcdefg");
d.Add(19, "abc");

// The Add method throws an exception if the new key is
// already in the dictionary.
try
{
d.Add(7, "xxx");
}
catch (ArgumentException ae)
{
Console.WriteLine("An element with Key = 7 already exists.");
}

// The Item property is another name for the indexer, so you
// can omit its name when accessing elements.
Console.WriteLine("For key = 19, value = {0}.", d[19]);

// The indexer can be used to change the value associated
// with a key.
d[19] = "abcdefghijklmnopqrs";
Console.WriteLine("For key = 19, value = {0}.", d[19]);

// If a key does not exist, setting the indexer for that key
// adds a new key/value pair.
d[11] = "abcdefghijk";

// The indexer throws an exception if the requested key is
// not in the dictionary.
try
{
Console.WriteLine("For key = 5, value = {0}.", d[5]);
}
catch (KeyNotFoundException knfe)
{
Console.WriteLine("Key = 5 is not found.");
}

// When a program often has to try keys that turn out not to
// be in the dictionary, TryGetValue is the most efficient
// way to retrieve values.
string value = "";
if (d.TryGetValue(5, out value))
{
Console.WriteLine("For key = 5, value = {0}.", value);
}
else
{
Console.WriteLine("Key = 5 is not found.");
}

// ContainsKey is not an efficient way to retrieve values,
// but it's a good way to test keys before inserting them.
if (!d.ContainsKey(5))
{
d.Add(5, "abcde");
Console.WriteLine("Value added for key = 5: {0}", d[5]);
}

// When you use foreach to enumerate dictionary elements,
// the elements are retreived as KeyValuePair objects.
Console.WriteLine();
foreach( KeyValuePair<int, string> kvp in d )
{
Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}

// To get the values alone, use the Values property.
Console.WriteLine();
Dictionary<int, string>.ValueCollection dvc = d.Values;
// The elements of the ValueCollection are strongly typed
// with the type that was specified for dictionary values.
foreach( string s in dvc )
{
Console.WriteLine("Value = {0}", s);
}

}

}

/* This code example produces the following output:

An element with Key = 7 already exists.
For key = 19, value = abc.
For key = 19, value = abcdefghijklmnopqrs.
Key = 5 is not found.
Key = 5 is not found.
Value added for key = 5: abcde

Key = 1, Value = a
Key = 3, Value = abc
Key = 7, Value = abcdefg
Key = 19, Value = abcdefghijklmnopqrs
Key = 11, Value = abcdefghijk
Key = 5, Value = abcde

Value = a
Value = abc
Value = abcdefg
Value = abcdefghijklmnopqrs
Value = abcdefghijk
Value = abcde
*/
petit 2005-08-10
  • 打赏
  • 举报
回复
to weisunding(鼎鼎):这样写在java中可以,在c#中不行
to crossrowman(godi):我想知道System.Collections.Hashtable 是否内置支持泛型
to Macosx(总算毕业了) :正确的语法是什么呢?
to namhyuk(namhyuk) : 您说的很清楚,但这不是我想问的问题
petit 2005-08-10
  • 打赏
  • 举报
回复
我写错了
java中是这样写的Hashtable<String, String> table = new Hashtable<String, String>();
在c#中这样写,行不通,应该怎么表达呢?

110,533

社区成员

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

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

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