装箱、拆箱、hashtable集合
1、装箱、拆箱
1)装箱用的什么类型,拆箱的时候也用什么类型
int n = 10; object o= n; int nn=(int)o; Console.WriteLine(nn);
2、hashtable:键---值集合
字典:拼音----->汉字 //键---->值 //创建了一个键值对集合 Hashtable ht=new Hashtable(); ht.Add("a", 1); ht.Add("b", 2); ht.Add("男",false); ht.Add("80kg", 178.99); //键必须是唯一的,值是可以重复的 ht.Add("c", 1); //给值重新赋值 ht["c"] = 3; //判断是否包含键 if (!ht.ContainsKey("男")) { ht.Add ("男", true); } else { Console.WriteLine("已经包含相同键"); } //清空集合 ht.Clear(); foreach(var itme in ht.Keys) { Console.WriteLine("键-----{0},值{1}",itme,ht[itme]); } Console.WriteLine(ht);
3、统计welcome to China 中的每个字符出现的次数
//统计welcome to China 中的每个字符出现的次数 string str = "welcom to china"; //字符------->出现的次数 Hashtable ht = new Hashtable(); for(int i = 0; i < str.Length; i++) { //为空是,跳过 if (str[i] == ' ') { continue; } //相同字符出现,+1 if (!ht.ContainsKey(str[i])) { //该字母在集合中第一次出现 ht.Add(str[i], 1); } else { //字母在键中出现过一次,值++ int n = (int)ht[str[i]]; n++; ht[str[i]] = n; } } foreach (var itme in ht.Keys) { Console.WriteLine("字母{0}出现了{1}次", itme, ht[itme]); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?