Home > Backend Development > C#.Net Tutorial > Key-value pairs in C#

Key-value pairs in C#

王林
Release: 2023-08-25 22:49:02
forward
2082 people have browsed it

C# 中的键值对

The KeyValuePair class uses C# to store a pair of values ​​in a single list.

Set KeyValuePair and add elements -

1

2

3

4

5

6

var myList = new List<KeyValuePair<string, int>>();

 

// adding elements

myList.Add(new KeyValuePair<string, int>("Laptop", 20));

myList.Add(new KeyValuePair<string, int>("Desktop", 40));

myList.Add(new KeyValuePair<string, int>("Tablet", 60));

Copy after login

Here is the code to learn how to use KeyValuePair and display the keys and values ​​-

Example

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Using System;

using System.Collections.Generic;

class Program {

   static void Main() {

      var myList = new List<KeyValuePair<string, int>>();

      // adding elements

      myList.Add(new KeyValuePair<string, int>("Laptop", 20));

      myList.Add(new KeyValuePair<string, int>("Desktop", 40));

      myList.Add(new KeyValuePair<string, int>("Tablet", 60));

      foreach (var val in myList) {

         Console.WriteLine(val);

      }

   }

}

Copy after login

The above is the detailed content of Key-value pairs in C#. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template