你最喜欢的 C# 扩展方法是什么?(codeplex.com/extensionoverflow) [英] What are your favorite extension methods for C#? (codeplex.com/extensionoverflow)

查看:25
本文介绍了你最喜欢的 C# 扩展方法是什么?(codeplex.com/extensionoverflow)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们列出您最喜欢的优秀扩展方法的答案列表.

Let's make a list of answers where you post your excellent and favorite extension methods.

要求必须发布完整的代码以及示例和使用说明.

The requirement is that the full code must be posted and a example and an explanation on how to use it.

基于对这个主题的高度兴趣,我在 Codeplex<上设置了一个名为 extensionoverflow 的开源项目/strong>.

Based on the high interest in this topic I have setup an Open Source Project called extensionoverflow on Codeplex.

请将您的答案标记为接受将代码放入 Codeplex 项目.

请发布完整的源代码而不是链接.

Codeplex 新闻:

24.08.2010 Codeplex 页面现在在这里:http://extensionoverflow.codeplex.com/

24.08.2010 The Codeplex page is now here: http://extensionoverflow.codeplex.com/

11.11.2008 XmlSerialize/XmlDeserialize 现在是 实施单元测试.

11.11.2008 XmlSerialize / XmlDeserialize is now Implemented and Unit Tested.

11.11.2008 仍有更多开发者的空间.;-) 现在加入!

11.11.2008 There is still room for more developers. ;-) Join NOW!

11.11.2008 第三个贡献者加入ExtensionOverflow,欢迎BKristensen

11.11.2008 Third contributer joined ExtensionOverflow, welcome to BKristensen

11.11.2008 FormatWith 现在是 实施单元测试.

11.11.2008 FormatWith is now Implemented and Unit Tested.

09.11.2008 第二个贡献者加入了 ExtensionOverflow.欢迎使用 chakrit.

09.11.2008 Second contributer joined ExtensionOverflow. welcome to chakrit.

09.11.2008 我们需要更多的开发人员.;-)

09.11.2008 We need more developers. ;-)

09.11.2008 ThrowIfArgumentIsNull 现在 实施单元测试 在 Codeplex 上.

09.11.2008 ThrowIfArgumentIsNull in now Implemented and Unit Tested on Codeplex.

推荐答案

这是罗马数字的来回.不经常使用,但可能很方便.用法:

Here's a to-and-from for Roman numerals. Not often used, but could be handy. Usage:

if ("IV".IsValidRomanNumeral())
{
   // Do useful stuff with the number 4.
}

Console.WriteLine("MMMDCCCLXXXVIII".ParseRomanNumeral());
Console.WriteLine(3888.ToRomanNumeralString());

来源:

    public static class RomanNumeralExtensions
    {
        private const int NumberOfRomanNumeralMaps = 13;

        private static readonly Dictionary<string, int> romanNumerals =
            new Dictionary<string, int>(NumberOfRomanNumeralMaps)
            {
                { "M", 1000 }, 
                { "CM", 900 }, 
                { "D", 500 }, 
                { "CD", 400 }, 
                { "C", 100 }, 
                { "XC", 90 }, 
                { "L", 50 }, 
                { "XL", 40 }, 
                { "X", 10 }, 
                { "IX", 9 }, 
                { "V", 5 }, 
                { "IV", 4 }, 
                { "I", 1 }
            };

        private static readonly Regex validRomanNumeral = new Regex(
            "^(?i:(?=[MDCLXVI])((M{0,3})((C[DM])|(D?C{0,3}))"
            + "?((X[LC])|(L?XX{0,2})|L)?((I[VX])|(V?(II{0,2}))|V)?))$", 
            RegexOptions.Compiled);

        public static bool IsValidRomanNumeral(this string value)
        {
            return validRomanNumeral.IsMatch(value);
        }

        public static int ParseRomanNumeral(this string value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            value = value.ToUpperInvariant().Trim();

            var length = value.Length;

            if ((length == 0) || !value.IsValidRomanNumeral())
            {
                throw new ArgumentException("Empty or invalid Roman numeral string.", "value");
            }

            var total = 0;
            var i = length;

            while (i > 0)
            {
                var digit = romanNumerals[value[--i].ToString()];

                if (i > 0)
                {
                    var previousDigit = romanNumerals[value[i - 1].ToString()];

                    if (previousDigit < digit)
                    {
                        digit -= previousDigit;
                        i--;
                    }
                }

                total += digit;
            }

            return total;
        }

        public static string ToRomanNumeralString(this int value)
        {
            const int MinValue = 1;
            const int MaxValue = 3999;

            if ((value < MinValue) || (value > MaxValue))
            {
                throw new ArgumentOutOfRangeException("value", value, "Argument out of Roman numeral range.");
            }

            const int MaxRomanNumeralLength = 15;
            var sb = new StringBuilder(MaxRomanNumeralLength);

            foreach (var pair in romanNumerals)
            {
                while (value / pair.Value > 0)
                {
                    sb.Append(pair.Key);
                    value -= pair.Value;
                }
            }

            return sb.ToString();
        }
    }

这篇关于你最喜欢的 C# 扩展方法是什么?(codeplex.com/extensionoverflow)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆