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新手或者工作一年以内开发人员很有用处] 常用方法等收集.添加扩展类. ...
随机推荐
- c++ vector用法和迭代器
1.在c++中,vector是一个十分有用的容器,下面对这个容器做一下总结. (1)头文件#include<vector>. (2)创建vector对象,vector<int> ...
- 使用ffmpeg合并视频
命令: ffmpeg -i concat:"1.avi|2.avi" -vcodec copy -acodec copy "3.avi" ffmpeg下载:ht ...
- webview加载url出现空白页面,有些页面没问题
用webview加载url出现空白页,测试后把百度,Github之类的url传进去都没问题,后来发现是因为布局的原因,因为webview对不同的网站兼容性没有那么强,特别是现在出现的各种前端布局,没法 ...
- 进度条控件JProgressBar的使用
----------------siwuxie095 工程名:TestUI 包名:com.siwuxie095.ui 类名:TestList.j ...
- 05 HTML字符串转换成jQuery对象、绑定数据到元素上
1 要求 将一段 HTML脚本 封装成一个字符串,将这个字符串转换成一个jQuery对象:然后将这个jQuery对象添加到指定的元素中去 2 步骤 定义字符串 var str = '<div i ...
- 在PyCharm 软件中设置你的项目 使用的Python版本
在PyCharm 软件中设置你的项目 使用的Python版本 python2 和 python3 有很大的不同,使用python2 编写的程序,如果使用python3 就运行不了:使用python3编 ...
- Git 之 协同开发
GitHub中多人协同开发和单人开发还是有点差别,协同开发一般有两种方式: 合作者,将其他用户添加到仓库合作者中之后,该用户就具有向当前仓库提交代码. 组织,创建一个组织,然后再该组织下可以创建多个项 ...
- 对Spark的理解
Spark作为一个新的分布式计算引擎正慢慢流行起来,越来越来的企业也准备用它的替换MapReduce,根据自己在工作的一些体会谈谈的优势. 分布式计算归根到底还是一个Map和Reduce操作,Map操 ...
- margin,CSS边距重叠
CSS外边距叠加就是margin-collapse,边距合并指的是,当两个垂直外边距相遇时,它们将形成一个外边距,水平边 距永远不会重合. 重叠结果计算规则: 两个相邻的外边距都是正数时,折叠结果是它 ...
- Altium Designer 3D模型的下载与添加
先 先晒几个图:是不是很逼真啊.. ---------------------------------------教程---------------------------------------- ...