Dictionary TryGetValue() case insensitive
The Dictionary class represents a collection of keys and values. The
.net framework’s Dictionary is located under the System.Collections.Generic
namespace. The Dictionary object constructor is Dictionary<TKey,TValue>.
The TKey is the data type of the keys in the Dictionary and the TValue is the
data type of the values in the Dictionary. We can initialize an empty
Dictionary instance and add elements to it using its Add() method.
The following .net c# tutorial code demonstrates how we can use Dictionary TryGetValue() method. In this .net c# tutorial code we will also demonstrate how we can use Dictionary TryGetValue() method in a case-insensitive way. So, we will pass a key in a different case instead the original case such as ‘SNOW Goose’ instead of ‘Snow Goose’.
The Dictionary TryGetValue() method gets the valueAssociated with the specified key. The TryGetValue() method has two parameters named ‘TKey key’ and ‘out TValue value.’ The first parameter is the key of the value to get. And the second parameter is value.
When this method returns, contains the value associated with the specified key if the key is found; otherwise the default value for the type of the value parameter.
The Dictionary TryGetValue() method returns true if the Dictionary contains an element with the specified key otherwise it returns false. The TryGetValue() method throws ArgumentNullException if the key is null.
The following .net c# tutorial code demonstrates how we can use Dictionary TryGetValue() method. In this .net c# tutorial code we will also demonstrate how we can use Dictionary TryGetValue() method in a case-insensitive way. So, we will pass a key in a different case instead the original case such as ‘SNOW Goose’ instead of ‘Snow Goose’.
The Dictionary TryGetValue() method gets the valueAssociated with the specified key. The TryGetValue() method has two parameters named ‘TKey key’ and ‘out TValue value.’ The first parameter is the key of the value to get. And the second parameter is value.
When this method returns, contains the value associated with the specified key if the key is found; otherwise the default value for the type of the value parameter.
The Dictionary TryGetValue() method returns true if the Dictionary contains an element with the specified key otherwise it returns false. The TryGetValue() method throws ArgumentNullException if the key is null.
dictionary-trygetvalue-case-insensitive.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
//initialize a dictionary with keys and values.
Dictionary<string, int> birds = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase) {
{"Swan Goose",10},
{"Snow Goose",20},
{"Canada Goose",30},
{"Whooper Swan",40}
};
Label1.Text = "dictionary keys and values..........";
foreach (KeyValuePair<string, int> pair in birds)
{
Label1.Text += "<br />" + pair.Key + " ........ " + pair.Value;
}
int value;
string key = "SNOW Goose";
Label1.Text += "<br /><br />key [" + key + "] value is: ";
if (birds.TryGetValue(key, out value))
{
Label1.Text += value;
}
else
{
Label1.Text += "key [" + key + "] does not exists in dictionary";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>c# example - dictionary trygetvalue case insensitive</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
c# example - dictionary trygetvalue case insensitive
</h2>
<hr width="550" align="left" color="Gainsboro" />
<asp:Label
ID="Label1"
runat="server"
Font-Size="Large"
>
</asp:Label>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
Text="dictionary trygetvalue case insensitive"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>