如何在asp.net中创建字典? [英] How to create dictionary in asp.net?

查看:186
本文介绍了如何在asp.net中创建字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

字典< string,string>

如何在asp.net中创建字典,它的优点是什么.
我们什么时候应该使用它?

请帮我举一个VB代码示例.

在此先感谢

Hi All.

Dictionary<string,string>

How to create dictionary in asp.net and what are the advantages of it.
When should we use it?

Please help me with an example in vb code.

Thanks in advance

推荐答案

您将在后面的代码中创建字典-坦率地说,它是Win Forms还是ASP.NET还是选择.NET库类型选择的库类型无关紧要.

当您想存储带有定义良好的键的东西,以便以后查找时,它非常有用. Session对象是ASP.NET中字典的经典示例,它可以使用有意义的键存储任意对象(称为Value).

在您的示例中,您将使用
You would create the dictionary in the code behind - quite frankly whether it''s Win Forms or ASP.NET or pick your .NET library type of choice, the library type is immaterial.

It''s very useful when you want to store something with a well defined key that you can look up later. A classic example of a dictionary in ASP.NET is the Session object which can store an arbitrary object (known as the Value) using a meaningful Key.

In your example, you''d create the dictionary using
Dim myDict As New Dictionary(Of String, String)

通常,您可以使用

If Not myDict.ContainsKey(theKeyToAdd) Then
  myDict.Add(theKeyToAdd, theValue)
End If

最后,您可以使用

Dim value As String
If Not myDict.TryGetValue(theKeyToRetrieve, value) Then
  Console.WriteLine("The key could not be found")
Else
  Console.WriteLine(theValue)
End If


尝试:
Dim dic As New Dictionary(Of String, String)()
dic.Add("Key", "Value")

字符串,字符串字典基本上是一个列表,其中每个条目都有唯一的名称,而不是数字索引.如果您要设置人员列表及其电子邮件地址,那么可以说出

A string, string dictionary is basically a list, where each entry has a unique name, rather than a numeric index. If you want to set up a list of people, and their email addresses, then it is useful to be able to say

Dim dic As New Dictionary(Of String, String)()
dic.Add("Mike Smith", "Mike.Smith@ADomain.Com")
dic.Add("Joe Smith", "OldJoe@AnotherDomain.Com")
Dim email As String = dic("Joe Smith")

您无法使用列表.


了解字典类的用法
http://msdn.microsoft.com/en-us/library/xfhwa508.aspx [ ^ ]

Google针对特定问题.
Learn the use of Dictionary class
http://msdn.microsoft.com/en-us/library/xfhwa508.aspx[^]

Google for specific issues.


这篇关于如何在asp.net中创建字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆