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

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

问题描述

让我们在那里您发布出色,喜欢的扩展方法的答案列表。

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

的要求是,满code必须张贴和一个例子,以及如何使用它的解释。

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

根据本主题中的高利息我已经安装在 一个开源项目叫做extensionoverflow codePLEX

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

请注明与接受你的答案把code在codePLEX项目。

请发布完整的源$ C ​​$ C,而不是链接。

codePLEX新闻:

2010年8月24日的codePLEX页面现在在这里: HTTP://extensionoverflow.$c$ cplex.com/

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

2008年11月11日的 XMLSERIALIZE / XmlDeserialize 现<一个href=\"http://www.$c$cplex.com/extensionoverflow/SourceControl/FileView.aspx?itemId=284374&changeSetId=17001\"相对=nofollow>实施和<一个href=\"http://www.$c$cplex.com/extensionoverflow/SourceControl/FileView.aspx?itemId=288847&changeSetId=17001\"相对=nofollow>进行单元测试。

11.11.2008 XmlSerialize / XmlDeserialize is now Implemented and Unit Tested.

2008年11月11日还有空间可以容纳更多的开发者。 ;-) 现在就加入!

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

2008年11月11日三贡献者加入 ExtensionOverflow ,欢迎的 BKristensen

11.11.2008 Third contributer joined ExtensionOverflow, welcome to BKristensen

2008年11月11日的 FormatWith 现<一个href=\"http://www.$c$cplex.com/extensionoverflow/SourceControl/FileView.aspx?itemId=284374&changeSetId=16839\"相对=nofollow>实施和<一个href=\"http://www.$c$cplex.com/extensionoverflow/SourceControl/FileView.aspx?itemId=288847&changeSetId=16839\"相对=nofollow>进行单元测试。

11.11.2008 FormatWith is now Implemented and Unit Tested.

2008年9月11日第二次贡献者加入 ExtensionOverflow 。欢迎 chakrit

09.11.2008 Second contributer joined ExtensionOverflow. welcome to chakrit.

2008年9月11日我们需要更多的开发者。 ; - )

09.11.2008 We need more developers. ;-)

2008年9月11日的 ThrowIfArgumentIsNull 在现在的实施和<一个href=\"http://www.$c$cplex.com/extensionoverflow/SourceControl/FileView.aspx?itemId=284112&changeSetId=16468\"相对=nofollow>进行单元测试上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天全站免登陆