惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
博客园 - 叶小钗
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
腾讯CDC
S
SegmentFault 最新的问题
D
DataBreaches.Net
Hugging Face - Blog
Hugging Face - Blog
L
LangChain Blog
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
N
Netflix TechBlog - Medium
大猫的无限游戏
大猫的无限游戏
M
MIT News - Artificial intelligence
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
F
Fortinet All Blogs
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Martin Fowler
Martin Fowler
雷峰网
雷峰网
J
Java Code Geeks
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
小众软件
小众软件
云风的 BLOG
云风的 BLOG
T
Tailwind CSS Blog

博客园 - Snapping

PLSQL-Developer beautifier xml skill note Delete temp files to get more disk space 设置控件text(兼容浏览器) Oracle: where to add tns key sql server process query Default permissions and user rights for IIS 6.0 C#获取项目程序路径的方法 Get the source code via a url __doPostBack ASP.NET 2.0 中的UrlRewrite var container = $get('divA'); var links = container.getElementsByTagName("A"); static variables and web.config file Register dll for asp application The report server cannot decrypt the symmetric key install report server 短歌行 CSS: 在半透明的层上放一个不透明层 技巧和诀窍;在VS 2005里优化ASP.NET 2.0Web项目的Build性能
泛型 List 和 Dictionary 类的互相转换
Snapping · 2008-01-11 · via 博客园 - Snapping

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace testcs
{
    
class Program
    {
        
static void Main(string[] args)
        {
            ListAndDictionary();

        }

/// <summary>
        
/// 泛型 List 和 Dictionary 类的互相转换
        
/// </summary>
        private static void ListAndDictionary()
        {
            
// dict -> list 
            
// 转换为 List<T>,T 的类型是 KeyValuePair<TKey, TValue>
            Dictionary<stringint> d = new Dictionary<stringint>();
            var l 
= d.ToList();

            List

<KeyValuePair<stringint>> l2 = new List<KeyValuePair<stringint>>{
                
new KeyValuePair<string,int>("a"1),
                
new KeyValuePair<string,int>("b"2)
            };
// list -> dict 两种转换方式// 1. 可指定 keySelector 和 valueSelector;完全自定义字典 key, value 的类型            
            Dictionary<stringint> d2 = l2.ToDictionary(entry => entry.Key, entry => entry.Value);
            
// 2. 也可只指定 keySelector, value 为 entry 的值。
            Dictionary<float, KeyValuePair<stringint>> d3 = l2.ToDictionary(
                entry 
=> (float)entry.Value);
        }
    }
}