What do you mean by "C# wouldn't allow abuse of the comma operator"? C# doesn't have a comma operator. A quick search pulled up this little gem (I wonder how it got copied from the MSDN foum :sigh: ): http://www.dotnetmonster.com/Uwe/Forum.aspx/dotnet-csharp/93126/C-for-loop-and-comma-operator[^] Yes, that's a horror -- the method should have a better name and it could even be made generic!
public T
LastOf
(
params object[] list
)
{
return ( (T) list [ list.Count - 1 ] ) ;
}
It's still silly, but this just points up how easy it is to implement a way to get around the limitation. Come to think of it, couldn't you do it with Linq? I don't know Linq, but something like: (new int[] { a++ , b++ }).Last ? P.S. Here's a little test of the concept:
int i = 0 ;
int j = 0 ;
while ( (new int\[\] { i++ , j += 2 }).Last() < 10 )
{
System.Console.WriteLine ( "{0} {1}" , i , j ) ;
}
for ( i = 0 , j = 0 ; (new int\[\] { i++ , j += 2 }).Last() < 10 ; )
{
System.Console.WriteLine ( "{0} {1}" , i , j ) ;
}