Whidbey Readiness Quiz: Converting array values
Thinking about my last little quiz, I realize there are thousands (literally) of new methods across the framework in Whidbey. How better to introduce\explain them than illustrating the problem they are intended to fix..
Let’s say you had this code:
string[] inputValues = {"0","1","2","3","4","5","6","7","8","9","10","bad value"};
int[] outputValues;
outputValues = (int[])inputValues;
Clearly that last line will not compile….
foo.cs(11,24): error CS0030: Cannot convert type 'string[]' to 'int[]'
But how do you convert all the that string[] to an int[]? What if you want out of range inputs to map to -1? See if you can do it with no loops (foreach, while, for, etc), oh, and while you are at it no exception handling either… And for extra credit, print the odd values out to the console in red ;-).
Comments
- Anonymous
October 26, 2004
The comment has been removed - Anonymous
October 26, 2004
The comment has been removed - Anonymous
October 26, 2004
If Nat's version works, would this also work without an extra method by using these new Python-style "inline" functions (are they called anonymous functions?).
Or are these kind of functions only available for event handler? - Anonymous
October 26, 2004
string[] inputValues = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "bad value" };
int[] outputValues;
outputValues = Array.ConvertAll<string, int>(inputValues, delegate(string from)
{
int to;
if (!Int32.TryParse(from, out to))
{
to = -1;
}
return to;
});
Array.ForEach<int>(outputValues, delegate(int i)
{
if (i % 2 == 0)
{
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
}
System.Console.WriteLine(i);
}
);
System.Console.ReadLine(); - Anonymous
October 26, 2004
funny you call it Python-style inlining... call me a pedant (and/or correct me if i'm wrong), but I think it was Lisp that introduced the concept :) - Anonymous
October 26, 2004
Wulong--
C#'s not first in this game with the anonymous delegates syntax, and Python does have similar "apply function" features available. But you're right: LISP was certainly the first to introduce this concept.
It'd go a little something like this:
(mapcar #'(lambda (x)
(IF (INTEGERP x) x -1)
'(0 1 2 3 4 5 6 7 8 9 10 BADVALUE))
<joe/> - Anonymous
October 26, 2004
The comment has been removed - Anonymous
October 26, 2004
The comment has been removed - Anonymous
October 26, 2004
Misread the description on color information a bit :P
string[] inputValues = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "bad value" };
int[] outputValues;
outputValues = Array.ConvertAll<string, int>(inputValues, delegate(string from)
{
int i;
if (!int.TryParse(from, out i)) i = -1;
if ((i & 1) == 1)
Console.ForegroundColor = ConsoleColor.Red;
else
Console.ResetColor();
Console.WriteLine(i);
return i;
}
);
Console.ReadLine(); - Anonymous
October 26, 2004
Nat, dotnetjunky, Why do you assume that console color were initialy default ? As well - why you forget to restore color back to original if finished on odd number ?
Not good ;-)
Everybody has bugs - even my submission ;-) - Anonymous
October 26, 2004
The comment has been removed - Anonymous
October 26, 2004
AT, also your foreground coloring does not work. The requirement was that the color should depend on the index into the array, not the converted array value. - Anonymous
October 27, 2004
True.... so this one might be a little better :)
Misread the description on color information a bit :P
string[] inputValues = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "bad value" };
int[] outputValues;
outputValues = Array.ConvertAll<string, int>(inputValues, delegate(string from)
{
int i;
if (!int.TryParse(from, out i)) i = -1;
if ((i & 1) == 1)
Console.ForegroundColor = ConsoleColor.Red;
else
Console.WriteLine(i);
Console.ResetColor();
return i;
}
);
Console.ReadLine(); - Anonymous
October 27, 2004
G:
@int " I will not tell you ;-)" Read C# specs. This is nice feature allowed even in versions before C# 2.0.
"print the odd values" It's clear that "odd values". Sure - I've printed even values - but this no way related to "odd indexes" ;-)
Nat:
Your if (i & 1 == 1 ) does not provide any real performance benefits - but decrease readability. As well it's flawed - why you are ignoring odd values by simply setting color ? Always use {} - this is mandatory for error-free coding.
As well you are reseting both foreground and background color by ResetColor() if it was set to non-standart before running your method. Even Main(String[] args) can be called from others functions - it's not always an entry point. - Anonymous
October 27, 2004
AT, can you have a constructive comment here? Do not be too much sarcastic. It is just a simple snippet.
P.S.I know that the code is not perfect. Even the last one I posted still has some problem since I forgot to remove "else" before posting it. - Anonymous
October 27, 2004
The @ allows a keyword to be used as an identifier.
What the heck AT is doing with @ I don't know. I think he's just trying to mess with us. - Anonymous
October 27, 2004
The extra credit is for someone else:
string[] inputValues = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "bad value" };
int[] outputValues;
outputValues = Array.ConvertAll<string, int>(inputValues, delegate(string from){
int output;
if (int.TryParse(from, out output )) {
return output;
} else {
return -1;
}
}); - Anonymous
October 27, 2004
Dan: Exactly ! You got the point. My entire sniplet is real mess (if you are unable to figure this from my comments). Coding / C# are not boring as everything think ;-) - Anonymous
October 27, 2004
AT, i think the main focus of the quiz is on the new Array.ConvertAll method and anonymous method construct, NOT on those trivial console's color thing. That's why i didn't pay much attention to it when I wrote my snippets. - Anonymous
October 27, 2004
dotnetjunky: You posted your source code to web-site. This mean that some people can start using it and hit unexpected problems you were able to solve using two-lines.
This is not good to post buggy code to public.
But overall - I agree this quiz about ConvertAll - so it's pretty fine to create bugs in everything else - but "extra credit" must not apply for this :-) - Anonymous
October 27, 2004
AT: Quote from you "This is not good to post buggy code to public".
I really don't understand your statement. Well, let me tell you this: even sample codes in MSDN articles contain bugs. And are you sure that your codes is perfectly correct ? The point here is that these public codes are for learning purposes only, not for production uses. If somebody uses my snippet codes (which is posted at a very informal place like this), then it's their fault, not mine.
Also, my code is just a "snippet" only, not a complete program. I posted it here to answer Brad's quiz. Why would you keep insisting it's buggy while it satisfies all Brad's requirements ?