What is KEY/VALUE Pair in Asp.net

Posted by Rajeshk under Others on 11/12/2012 | Points: 10 | Views : 7186 | Status : [Member] | Replies : 2
hi,
As a Beginer i dont know about value pair in .net so please help me to understand with your explanation ...and also i want know key/value pair and name-value pair are same or not?

Thanks&Regards
rajeshkommireddy@gmail.com



Responses

Posted by: Sravya on: 11/12/2012 [Member] Starter | Points: 25

Up
0
Down
Hi..Rajesh
KeyValue Pair Stores two values together. It is a single generic structure in c# progamming language.
The KeyValuePair type in System.Collections.Generic is simple and always available. It is used internally in Dictionary and other collections.
Example
List type

First this example shows how you can use KeyValuePair in a List, which is also in System.Collections.Generic. This is useful for storing pairs of values in a single List.
Alternatively: You could use two separate Lists, but that can complicate matters.

Program that uses KeyValuePair [C#]

using System;
using System.Collections.Generic;

class Program
{
static void Main()
{
// Shows a List of KeyValuePairs.
var list = new List<KeyValuePair<string, int>>();
list.Add(new KeyValuePair<string, int>("Cat", 1));
list.Add(new KeyValuePair<string, int>("Dog", 2));
list.Add(new KeyValuePair<string, int>("Rabbit", 4));

foreach (var element in list)
{
Console.WriteLine(element);
}
}
}
Output
[Cat, 1]
[Dog, 2]
[Rabbit, 4]
This Example shows how you can initialize a new List of type KeyValuePair. Inside the brackets in the KeyValuePair, there are two types separated by a comma. This example shows KeyValuePairs of one string and one int each.

KeyValue Pair and Name Value Pair Both are Different...

Thanks & Regards
Sravya

Rajeshk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Saratvaddilli on: 11/14/2012 [Member] [MVP] Bronze | Points: 25

Up
0
Down
good explanation sravya
apart from that i will include some points
As mentioned above key value pair was in System.Collections.Generic.
syntax ;
public struct KeyValuePair<KVPKey, KVPValue>

KVPkey : The type of the key. It is used to get the key in the keyvaluepair
KVPValue: The type of the Value.It is used to get the Value in the keyvaluepair

Methods in keyvaluepair:
Equals	        Indicates whether this instance and a specified object are equal. (Inherited from ValueType.)

GetHashCode Returns the hash code for this instance. (Inherited from ValueType.)
GetType Gets the Type of the current instance. (Inherited from Object.)
ToString Returns a string representation of the KeyValuePair<TKey, TValue>, using the string representations of the key and value. (Overrides ValueType.ToString().)


to dig more on this please go through this link
http://www.dotnetperls.com/keyvaluepair
http://msdn.microsoft.com/en-us/library/5tbh8a42.aspx

Thanks and Regards
V.SaratChand
Show difficulties that how difficult you are

Rajeshk, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response