What bothers me most about C# is that although, at first glance, it seems to be a
variation on Java, it doesn't follow the spirit of Java in at least one important
respect.
Specifically, one common advantage of both Haskell and Java is that they encourage
higher-order abstraction: Haskell through functional abstraction, and Java through
eliminating explicit pointers and memory management and enforcing object orientation
through the use of classes.
However, according to the C# Language Reference (at
http://msdn.microsoft.com/vstudio/nextgen/technology/csharpdownload.asp), "For
developers who are generally content with automatic memory management but sometimes
need fine-grained control or that extra iota of performance, C# provides the ability
to write “unsafe” code. Such code can deal directly with pointer types, and fix
objects to temporarily prevent the garbage collector from moving them." [Section 1.2]
Witness the following example [Section 1.2]:
-- quoted text begins --
using System;
class Test
{
unsafe static void WriteLocations(byte[] arr) {
fixed (byte *p_arr = arr) {
byte *p_elem = p_arr;
for (int i = 0; i < arr.Length; i++) {
byte value = *p_elem;
string addr = int.Format((int) p_elem, "X");
Console.WriteLine("arr[{0}] at 0x{1} is {2}", i,
addr, value);
p_elem++;
}
}
}
static void Main() {
byte[] arr = new byte[] {1, 2, 3, 4, 5};
WriteLocations(arr);
}
}
-- quoted text ends --
Even though the method is marked "unsafe," it allows direct pointer manipulation in
iterating over the elements and writing out the index, value, and location of each.
The reference adds, "Here is one possible output of the above program:
arr[0] at 0x8E0360 is 1
arr[1] at 0x8E0361 is 2
arr[2] at 0x8E0362 is 3
arr[3] at 0x8E0363 is 4
arr[4] at 0x8E0364 is 5
Of course, the exact memory locations are subject to change." [Section 1.2]
Taken to an extreme, this ability could encourage some programmers to ignore the
spirit of higher-level abstraction and focus back on The Old Way (TOW): rampant
pointer-level optimization to squeeze out that extra iota of performance at the
expense of clarity. But wasn't the whole point of higher-level abstraction to leave
this level of optimization to more intelligent compilers so that the programmer could
focus on writing clear, reusable code? Why allow us to get TOW'ed back (pardon the
pun)?
Somebody once wrote that a clearly written, well-documented program that doesn't work
is usually more valuable than a badly written, poorly-documented program that does
work because it can easily be fixed and reused. It just seems that allowing
programmers the ability to manipulate pointers directly is virtually hiding a
Pandora's Box in C#.
--Benjamin L. Russell
[EMAIL PROTECTED]
[EMAIL PROTECTED]
On Thu, 10 Aug 2000 10:46:04 -0700
"Craig Dickson" <[EMAIL PROTECTED]> wrote:
> <stuff deleted>
> </stuff deleted>
>
> More like "Microsoft Java", but of course they never
> mention Java, as if
> hoping that people will read all this tripe and not
> notice the similarities.
>
> I had a most exquisite sense of down-the-rabbit-hole a
> few weeks ago when I
> browsed through a Microsoft book on C#. I kept running
> into all this stuff
> about how C# was a revolutionary next-generation OO
> language, better than
> C++, but all the programming samples looked only
> trivially different from
> Java, which of course they never mentioned, as if the
> book had arrived from
> some parallel universe in which James Gosling was
> assassinated by Richard
> Stallman after inventing Gosling Emacs, and had thus
> never invented Java. I
> had to put the book down after a little while because I
> felt like I'd lose
> my mind if I kept going.
>
> I don't really mind them inventing a variation on Java.
> Either it will take
> off or it won't, and I don't really care either way. But
> I just wish they'd
> admit what they're doing, and stop treating us all as if
> we're so stupid
> that if they don't _tell_ us that C# is a Java
> derivative, we won't notice.
> <stuff deleted>
> </stuff deleted>