471,334 Members | 2,679 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,334 software developers and data experts.

foreach type unsafe

/* foreach does implicit type conversion on elements of a params argument or
Generic.List.
* This is not good.
* Examples of evil follow...
*/

using System;

// I love it when C# is strict with me...
using System.Collections.Generic;
// ...prettier than C++ templates, not slutty like ArrayList.

namespace DoubleUtilities
{
public static class MaxDouble
{
public static double Max(params double[] list)
{
if (list.Length == 0)
return 0.0D;
double m = list[0];
for (int i = 1; i < list.Length; i++)
{
// Taste the whip of love not given lightly...
//int d = list[i]; //Type error reported (double to int
conversion).
double d = list[i]; // The correct type declaration.

if (d > m)
m = d;
}
return m;
}

public static double MadMax(params double[] list)
{
if (list.Length == 0)
return 0.0D;
double m = list[0];
// Her FOG, her amphetamine, and her pearls...
// And she BREAKS just like a LITTLE GIRL
foreach (int d in list) //Type error unreported (double to int
conversion).
if (d > m)
m = d;
return m;
}
//__________________________________________________ ________________________
public static double Max(List<double> list)
{
if (list.Count == 0)
return 0.0D;
double m = list[0];
for (int i = 1; i < list.Count; i++)
{
// Taste the whip of love not given lightly...
//int d = list[i]; //Type error reported (double to int
conversion).
double d = list[i]; // The correct type declaration.

if (d > m)
m = d;
}
return m;
}

public static double MadMax(List<double> list)
{
if (list.Count == 0)
return 0.0D;
double m = list[0];

// Her FOG, her amphetamine, and her pearls...
// And she BREAKS just like a LITTLE GIRL
foreach (int d in list) //Type error unreported (double to int
conversion).
if (d > m)
m = d;
return m;
}
//__________________________________________________ _________________
public static void TestCases()
{
double expected_max = 8.345;
double[] array = { -3.6, 1.1, -2.2, 3.4, 6.6, expected_max, 7.2 };
TestMaxes(array, expected_max);
}

private static void TestMaxes( double[] parameters, double
expected_max )
{
/* Output
parameters={-3.6, 1.1, -2.2, 3.4, 6.6, 8.345, 7.2}
Max( parms double[] list): max=8.345, is_ok=True
Max( List<double> list): max=8.345, is_ok=True
MadMax( parms double[] list): max=8, is_ok=False
MadMax( List<double> list): max=8, is_ok=False
*/
string delimiter = String.Empty;
Console.Write("parameters={");
for (int i = 0; i < parameters.Length; i++)
{
Console.Write("{0}{1}",delimiter, parameters[i].ToString());
delimiter = ", ";
}
Console.WriteLine("}");

double max = Max(parameters);
bool is_ok = Math.Abs(max - expected_max) < 1E-12D; //paranoid
double comparison
Console.WriteLine("Max( parms double[] list): max={0},
is_ok={1}", max, is_ok);

List<double> list = new List<double>(parameters);
max = Max(list);
is_ok = Math.Abs(max - expected_max) < 1E-12D;
Console.WriteLine("Max( List<double> list): max={0}, is_ok={1}",
max, is_ok);

max = MadMax(parameters);
is_ok = Math.Abs(max - expected_max) < 1E-12D;
Console.WriteLine("MadMax( parms double[] list): max={0},
is_ok={1}", max, is_ok);

max = MadMax(list);
is_ok = Math.Abs(max - expected_max) < 1E-12D;
Console.WriteLine("MadMax( List<double> list): max={0},
is_ok={1}", max, is_ok);
}
}
}

Jan 15 '06 #1
3 1775
"pg********@blueyonder.co.uk"
<pg********@blueyonder.co.uk@discussions.microsoft .com> wrote in message
news:AE**********************************@microsof t.com...
/* foreach does implicit type conversion on elements of a params argument
or
Generic.List.
* This is not good.
* Examples of evil follow...
*/


That's just the way it is, not much you can do about it except not use for
each.

Michael
Jan 15 '06 #2
"Michael C" wrote:
That's just the way it is, not much you can do about it except not use for
each.


That's a pity: foreach has a nice uncluttered syntax.
I guess it is that way for historical reasons - I'd imagine it was those old
untyped collection classes that influenced the semantics of foreach.

You don't suppose that discussions in this group about holes in the type
system might influence the compiler writers to fix those holes, or at least
issue a warning?
Or is that too Pollyanna-ish of me?
Jan 16 '06 #3

"Paul Connolly" <Paul Co******@discussions.microsoft.com> wrote in message
news:F1**********************************@microsof t.com...
"Michael C" wrote:
That's just the way it is, not much you can do about it except not use
for
each.
That's a pity: foreach has a nice uncluttered syntax.
I guess it is that way for historical reasons - I'd imagine it was those
old
untyped collection classes that influenced the semantics of foreach.

You don't suppose that discussions in this group about holes in the type
system might influence the compiler writers to fix those holes, or at
least
issue a warning?
Or is that too Pollyanna-ish of me?


It is unlikely, I don't know that anyone even bothers monitoring this group.
But I do agree a warning should be there in this case.

Have you looked to see if there is a suggestion at the product feedback
center yet?
http://lab.msdn.microsoft.com/produc...k/default.aspx

Jan 16 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Phil | last post: by
13 posts views Thread by cody | last post: by
1 post views Thread by Pierre Drapeau | last post: by
104 posts views Thread by cody | last post: by
1 post views Thread by Gustyn | last post: by
29 posts views Thread by Jon Slaughter | last post: by
21 posts views Thread by Chad | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.