C#工具类之字典扩展类
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace G2.Utils
{
/// <summary>
/// 字典帮助类
/// </summary>
public static class DictionaryHelper
{
/// <summary>
/// 字典排序(默认排序)
/// </summary>
/// <typeparam name="TKey">Key类型</typeparam>
/// <typeparam name="TValue">Value类型</typeparam>
/// <param name="dictionary">无序字典</param>
/// <returns>有序字典</returns>
public static IDictionary<TKey, TValue> Sort<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
{
if (dictionary == null)
{
throw new ArgumentNullException("字典为空,不能排序");
} return new SortedDictionary<TKey, TValue>(dictionary);
} /// <summary>
/// 字典排序(自定义排序)
/// </summary>
/// <typeparam name="TKey">Key类型</typeparam>
/// <typeparam name="TValue">Value类型</typeparam>
/// <param name="dictionary">无序字典</param>
/// <param name="comparer">自定义排序方法</param>
/// <returns>有序字典</returns>
public static IDictionary<TKey, TValue> Sort<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, IComparer<TKey> comparer)
{
if (dictionary == null)
{
throw new ArgumentNullException("字典为空,不能排序");
} if (comparer == null)
{
throw new ArgumentNullException("自定义排序方法为空,不能排序");
} return new SortedDictionary<TKey, TValue>(dictionary, comparer);
} /// <summary>
/// 字典排序(依据值默认排序)
/// </summary>
/// <typeparam name="TKey">Key类型</typeparam>
/// <typeparam name="TValue">Value类型</typeparam>
/// <param name="dictionary">无序字典</param>
/// <returns>有序字典</returns>
public static IDictionary<TKey, TValue> SortByValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
{
return dictionary.OrderBy(v => v.Value).ToDictionary(item => item.Key, item => item.Value);
} /// <summary>
/// 字典排序(依据值自定义排序)
/// </summary>
/// <typeparam name="TKey">Key类型</typeparam>
/// <typeparam name="TValue">Value类型</typeparam>
/// <param name="dictionary">无序字典</param>
/// <param name="comparer">自定义排序方法</param>
/// <returns>有序字典</returns>
public static IDictionary<TKey, TValue> SortByValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, IComparer<TValue> comparer)
{
return dictionary.OrderBy(v => v.Value, comparer).ToDictionary(item => item.Key, item => item.Value);
} /// <summary>
/// 判断字典是否为空或者无数据
/// </summary>
/// <param name="dictionary">字典</param>
/// <returns>True:字典为空或没有数据;否则为False</returns>
public static bool IsEmpty<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
{
if (dictionary == null)
{
throw new ArgumentNullException("字典为空");
} return dictionary.Count == ;
} /// <summary>
/// 判断字典是否为空或者无数据
/// </summary>
/// <param name="dictionary">字典</param>
/// <returns>True:字典为空或没有数据;否则为False</returns>
public static bool IsNullOrEmpty<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
{
return dictionary == null || dictionary.Count == ;
} /// <summary>
/// 获取字典值(如果不包含该值则由自定义方法返回)
/// </summary>
/// <typeparam name="TKey">Key类型</typeparam>
/// <typeparam name="TValue">Value类型</typeparam>
/// <param name="dictionary">字典</param>
/// <param name="key">Key值</param>
/// <param name="function">自定义方法</param>
/// <returns>字典里的值或者自定义方法返回值</returns>
public static TValue GetOrGetByFunction<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TValue> function)
{
if (dictionary == null)
{
throw new ArgumentNullException("字典为空");
} if (dictionary.ContainsKey(key))
{
return dictionary[key];
} if (function == null)
{
throw new ArgumentNullException("自定义取值方法为空,不能获取值");
} return function();
} /// <summary>
/// 新增或重写(如果没有该Key值,则新增;否则重写该值)
/// </summary>
/// <typeparam name="TKey">Key类型</typeparam>
/// <typeparam name="TValue">Value类型</typeparam>
/// <param name="dictionary">字典</param>
/// <param name="key">Key值</param>
/// <param name="value">Value新值</param>
/// <returns>字典</returns>
public static IDictionary<TKey, TValue> AddOrSet<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
if (dictionary == null)
{
throw new ArgumentNullException("字典为空");
} if (dictionary.ContainsKey(key))
{
dictionary[key] = value;
}
else
{
dictionary.Add(new KeyValuePair<TKey, TValue>(key, value));
} return dictionary;
}
}
}
C#工具类之字典扩展类的更多相关文章
- [Django REST framework - 视图组件之视图基类、视图扩展类、视图子类、视图集]
[Django REST framework - 视图组件之视图基类.视图扩展类.视图子类.视图集] 视图继承关系 详图见文章末尾 视图组件可点我查看 两个视图基类:APIView.GenericAP ...
- UI(UGUI)框架(二)-------------UIManager单例模式与开发BasePanel面板基类/UIManage统一管理UI面板的实例化/开发字典扩展类
UIManage单实例: /// 单例模式的核心 /// 1,定义一个静态的对象 在外界访问 在内部构造 /// 2,构造方法私有化 private static UIManager _instanc ...
- C#工具类之字符串扩展类
/// <summary> /// 字典串帮忙类 /// </summary> public static class StringHelper { /// <summa ...
- C#工具类之素数扩展类
/// <summary> /// 素数帮忙类 /// 本类是从.net源码 类 internal static class HashHelpers 类里抽取相应的代码 /// https ...
- C#工具类之XmlNode扩展类
using System; using System.Linq; using System.Xml; /// <summary> /// XmlNodeHelper /// </su ...
- C#工具类之日期扩展类
/// <summary> /// DateTimeHelper /// </summary> public static class DateTimeHelper { /// ...
- c#工具类之Int扩展类
public static class IntHelper { /// <summary> /// 转换为2进制字符串 /// </summary> /// <param ...
- DRF框架(五)——context传参,二次封装Response类,两个视图基类(APIView/GenericAPIView),视图扩展类(mixins),子类视图(工具视图),视图集(viewsets),工具视图集
复习 1.整体修改与局部修改 # 序列化get (给前端传递参数) #查询 ser_obj = ModelSerializer(model_obj) #只传递一个参数,默认是instance的参数,查 ...
- ios开发总结:Utils常用方法等收集,添加扩展类,工具类方法,拥有很多方便快捷功能(不断更新中。。。)
BOBUtils 工具大全 本人github开源和收集功能地址:https://github.com/niexiaobo [对ios新手或者工作一年以内开发人员很有用处] 常用方法等收集.添加扩展类. ...
随机推荐
- 获取百度搜索结果的真实url以及摘要和时间
利用requests库和bs4实现,demo如下: #coding:utf- import requests from bs4 import BeautifulSoup import bs4 impo ...
- NLP整体流程的代码
import nltk import numpy as np import re from nltk.corpus import stopwords # 1 分词1 text = "Sent ...
- [转]PHP 面试问哪些问题可以比较准确的反映出应聘者的开发水平?
基础题 场景: 你入职了一家新公司. 上班第一天,接待人给你安排好了座位,然后拉过来一台没拆封的新电脑. 你把电脑连接好之后,按下电源.... 好吧,这真是一台新电脑,里边竟然内置了个DOS系统!! ...
- ARC100C Linear Approximation
传送门 分析 这道题真的好水呀QwQ,想必大家都知道对于式子|x-2|+|x-3|x取什么值可以使式子结果最小,这道题也是这个原理,只需要将要额外减的1.2.3……提前减掉就行了. 代码 #inclu ...
- Entity Framework Tutorial Basics(7):DBContext
DBContext: As you have seen in the previous Create Entity Data Model section, EDM generates the Scho ...
- Inheritance with EF Code First: Part 3 – Table per Concrete Type (TPC)
Inheritance with EF Code First: Part 3 – Table per Concrete Type (TPC) This is the third (and last) ...
- CodeForces 478D Red-Green Towers (DP)
题意:给定 n 块红砖,m 块绿砖,问有多少种方式可以建造成最高的塔,每一层颜色必须一样. 析:首先要确定最高是多少层h,大约应该是用 h * (h+1) <= (m+n) * 2,然后dp[i ...
- Git相关安装包打包下载
Git相关软件偶尔需要***才能下载,故分享于此 1.Git-2.15.0-64-bit.exe 2.TortoiseGit-2.5.0.0-64bit.msi 3.TortoiseGit-Langu ...
- Android camera调用出现错误解决方法
开发时,先是使用三星的手机测试,发现一切正常: 但是到了小米的手机的时候,发现图片很模糊,发现是设置camera.setParameters(parameters);报错导致用的是默认的最小的分辨率, ...
- [OpenGL]点阵显示生日快乐小程序
刚工作没多久的时候,业余学习了OGL的知识(这是写不好的借口吧), 在某个异性生日的时候写了这个程序. 编译平台: MinGW GCC gcc -o happOK happyOK.c -lglut32 ...