Java Map等价于C#

编程入门 行业动态 更新时间:2024-10-24 12:23:41
本文介绍了Java Map等价于C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用我选择的键保存集合中的项目列表。在Java中,我将简单地使用Map如下:

class Test { Map< Integer,String&实体; public String getEntity(Integer code){ return this.entities.get(code); } }

在C#中有同样的方法吗? System.Collections.Generic.Hashset 不使用哈希,我不能定义自定义类型键 System.Collections .Hashtable 不是通用类 System.Collections.Generic.Dictionary 没有 get(Key)方法

解决方案

'get'。

字典< string,string> example = new Dictionary< string,string>(); ... example.Add(hello,world); ... Console.Writeline(example [hello]);

测试/获取值的有效方法是 TryGetValue (thanx to Earwicker):

if(otherExample.TryGetValue(key,out value)) { otherExample [key] = value + 1;使用这种方法,您可以快速获取值和无异常的值(如果存在) 。

资源:

字典键

尝试获取价值

I'm trying to hold a list of items in a collection with a key of my choice. In Java, I would simply use Map as follows:

class Test { Map<Integer,String> entities; public String getEntity(Integer code) { return this.entities.get(code); } }

Is there an equivalent way of doing this in C#? System.Collections.Generic.Hashset doesn't uses hash and I cannot define a custom type key System.Collections.Hashtable isn't a generic class System.Collections.Generic.Dictionary doesn't have a get(Key) method

解决方案

You can index Dictionary, you didn't need 'get'.

Dictionary<string,string> example = new Dictionary<string,string>(); ... example.Add("hello","world"); ... Console.Writeline(example["hello"]);

An efficient way to test/get values is TryGetValue (thanx to Earwicker):

if (otherExample.TryGetValue("key", out value)) { otherExample["key"] = value + 1; }

With this method you can fast and exception-less get values (if present).

Resources:

Dictionary-Keys

Try Get Value

更多推荐

Java Map等价于C#

本文发布于:2023-11-09 15:37:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572754.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Java   Map

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!