示例#1
0
        public string DecodeNumber(int num)
        {
            List <KeyValuePair <int, string> > markers = new Dictionary <int, string>
            {
                { HelperStrategy.BYTENUMBER, HelperStrategy.BYTELETTERS },
                { HelperStrategy.BITNUMBER, HelperStrategy.BITLETTERS }
            }
            .OrderBy(kv => kv.Key).ToList();

            // var names = markers.Where(kv => num % kv.Key == 0).Select(kv => kv.Value);
            var names = markers.Where(kv => HelperStrategy.IsMatch2(num, kv.Key)).Select(kv => kv.Value);

            return(names.Any() ? string.Join(string.Empty, names) : num.ToString());
        }
示例#2
0
        // Strategy using Delegate <int, int, bool> and Two-Dimensional Array including the condition
        public string DecodeNumber(int num)
        {
            var markers = new[]
            {
                new { Name = HelperStrategy.BITBYTELETTERS, Condition = HelperStrategy.IsBitByteMatch(num) },
                new { Name = HelperStrategy.BITLETTERS, Condition = HelperStrategy.IsBitMatch(num) },
                new { Name = HelperStrategy.BYTELETTERS, Condition = HelperStrategy.IsByteMatch(num) },
                new { Name = num.ToString(), Condition = HelperStrategy.IsDefaultMatch(num) }
            };

            var names = markers.Where(kv => kv.Condition).Select(kv => kv.Name);

            return(names.FirstOrDefault());
        }
示例#3
0
        public string DecodeNumber(int num)
        {
            // Arrays of anonymous type new { integer Number, string Name }
            var markers = new[]
            {
                new { Number = HelperStrategy.BITBYTENUMBER, Name = HelperStrategy.BITBYTELETTERS },
                new { Number = HelperStrategy.BITNUMBER, Name = HelperStrategy.BITLETTERS },
                new { Number = HelperStrategy.BYTENUMBER, Name = HelperStrategy.BYTELETTERS }
            };

            // var names = markers.Where(kv => num % kv.Key == 0).Select(kv => kv.Value);
            var names = markers.Where(kv => HelperStrategy.IsMatch2(num, kv.Number)).Select(kv => kv.Name);

            return(names.Any() ? names.FirstOrDefault() : num.ToString());
        }
示例#4
0
        // Strategy using StringBuilder and Delegate <int, bool>
        // The StringBuilder class can be used when you are working with strings in a tight loop.
        // Instead of creating a new string over and over again,  StringBuilder uses a string buffer internally to improve performance.
        // The StringBuilder class even enables you to change the value of individual characters inside a string
        // StringBuilder s = new StringBuilder();
        // for (int i = 0; i< 10000; i++) { s.Append(“x”); }
        // x | x | x | x   => Only one copy of memory created
        // One thing to keep in mind is that the StringBuilder does not always give better performance.
        // When concatenating a fixed series of strings, the compiler can optimize this and combine individual concatenation operations into a single operation.
        // When you are working with an arbitrary number of strings, such as in the loop example, a StringBuilder is a better choice
        public string DecodeNumber(int num)
        {
            StringBuilder output = new StringBuilder();

            if (HelperStrategy.IsBit(num))
            {
                output.Append(HelperStrategy.BITLETTERS);
            }

            if (HelperStrategy.IsByte(num))
            {
                output.Append(HelperStrategy.BYTELETTERS);
            }

            if (string.IsNullOrEmpty(output.ToString()))
            {
                output.Append(num.ToString());
            }

            return(output.ToString());
        }