views:

121

answers:

4
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?

+5  A: 

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.

Stan R.
Thank you very much. I feel bad about my stupidity.I did not go through the example properly.
+7  A: 
  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.

Jon Skeet
Thank you very much Jon. :) Have a nice day
I was expecting your answer for one of my previous questions (Question Title : Linq- Running Total and Sub Total ).If time permits kindly help me in that too.
Jon, I don't think this is the best solution. Ophs what have I said!?!
Tony Lambert
@Tony: It's an explanation of how he could go from the integer version to a non-integer version. It will work for arbitrary types which work as keys in a dictionary - whereas enums won't.
Jon Skeet
@linqfying: The answer on that question is already perfectly good. I can't see what more I'd add to it.
Jon Skeet
Jon Do you mean answer to the previous question ?
I mean the answer to "Linq- Running Total and Sub Total".
Jon Skeet
+2  A: 

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

Seth Illgard
Sorry ! I did not go through it properly.Thank you. :)
+1  A: 

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.

Tony Lambert
This solution has the disadvantage that you are treating your code as both code and as text. The fact that an enum named "Three" can be turned into a string is useful in many places, but I feel like it's rather awkward to actually treat the name as a string in user output.
Brian
Additionally it won't work for non-integer types (e.g. strings, floats, Guids), and isn't easily internationalised (unlike a dictionary where you can just have different dictionaries for different languages).
Jon Skeet
he doesn't actually say he wants it to cook toast or indeed work internationally.... although he might..... he doesn't like toast I love toast.
Tony Lambert