user190560
user190560

Reputation: 3539

How the transformation is possible?

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
string[] strings = { "zero", "one", "two", "three", "four", "five", "six",
                     "seven","eight", "nine" };

 var textNums =
                from n in numbers
                select strings[n];

   Console.WriteLine("Number strings:");

   foreach (var s in textNums)
   {
                Console.WriteLine(s);
   }

1) What is the mechanism that transform an "integer" to representing the integer in "word" ?

2) Transformation like such kind is only possible with int to string? or can we do fun with this transformation?

Upvotes: 0

Views: 172

Answers (4)

AnthonyLambert
AnthonyLambert

Reputation: 8830

I would do the following:

public enum MyNumberType { 
        Zero = 0, One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten
        }

You could do what you want with it in the following ways:

namespace ConsoleApplication
{
    class Program
    {
        public enum MyNumberType { Zero = 0, One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten }

        private static int GetIntValue(MyNumberType theType) { return (int) theType; }
        private static String GetStringValue(MyNumberType theType) { return Enum.GetName(typeof (MyNumberType),theType); }
        private static MyNumberType GetEnumValue (int theInt) {
            return (MyNumberType) Enum.Parse( typeof(MyNumberType), theInt.ToString() ); }

        static void Main(string[] args)
        {
            Console.WriteLine( "{0} {1} {2}", 
                GetIntValue(MyNumberType.Five), 
                GetStringValue( MyNumberType.Three),
                GetEnumValue(7)
                );
            for (int i=0; i<=10; i++)
            {
                Console.WriteLine("{0}", GetEnumValue(i));
            }
        }
    }
}

Producing the following output:

5 Three Seven
Zero
One
Two
Three
Four
Five
Six
Seven
Eight
Nine
Ten

This could be extended for larger numbers and numbers not in a continuous range like so:

public enum MyNumberType { 
        ten= 10, Fifty=50, Hundred=100, Thousand=1000
        }

Enums can be used with other types as well not just int types so this is very flexible.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500215

  1. It's just array access - it's using the element from "numbers" as the index into the "strings" array.

  2. Only integers will work for arrays, but you could equally have a Dictionary<string, string> or whatever to do arbitrary mapping. In this case you can think of a string array as being like a Dictionary<int, string>. You could rewrite it that way too:

    int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
    var words = new Dictionary<int, string>
    {
        { 0, "zero" },
        { 1, "one" },
        { 2, "two" },
        { 3, "three" },
        { 4, "four" },
        { 5, "five" },
        { 6, "six" },
        { 7, "seven" },
        { 8, "eight" },
        { 9, "nine" }
    };
    var textNums = from n in numbers
                   select words[n];
    

    Console.WriteLine("Number strings:");

    foreach (var s in textNums) { Console.WriteLine(s); }

That's still using integers - but you can do the same thing with dictionaries where the keys are other types.

Upvotes: 7

Daniel Rodriguez
Daniel Rodriguez

Reputation: 1483

When you say strings[n] you are accessing the nth value of the array, and the array is ordered like:

strings[0] = "zero"; strings[1] = "one"; ... strings[4] = "four";

So, no magic here, just an ordered array :P

Upvotes: 2

Stan R.
Stan R.

Reputation: 16065

No. The string representations are just in the correct order that's all. There is no magic here.

Look at the string array

strings[0] = "zero";
strings[1] = "one";
strings[2] = "two";
.
.

the fact that its ordered correctly is why the mapping works.

Upvotes: 5

Related Questions