C#: "Sharp", "Hash" or "Pound"?: David Evans
C#: "Sharp", "Hash" or "Pound"?: David Evans
C#: “Sharp”,
“Hash” or
“Pound”?
Point points[1000];
In fact, the built-in primitive
types (e.g., int) in C# are just
struct types!
26 November 2002 CS 201J Fall 2002 16
Boxing and Unboxing
• Value types need to be “boxed” before
they can be used as subtypes of object:
int i = 123; int i = 123;
object box = i; object box = new int_Box(i);
int j = (int) box;
int i = 123;
Boxing makes a copy of Object box = new Integer (i);
a value type on the heap.
Cast “unboxes”, can fail run-time type check.
Values: 21 / 20 / 21 / 21
int may be a subtype of object, but assignment means
something different for objects an ints!
26 November 2002 CS 201J Fall 2002 19
MyInt Class
class MyInt
{
private int val;
public MyInt (int value) { val = value; }
static void Main(string[] args) public static MyInt operator++ (MyInt mi) {
{ mi.val++; return mi;
int i = 20; }
object o = i;
public override string ToString () {
MyInt mi = new MyInt (20);
return val.ToString ();
MyInt mi2 = mi; }
i++; }
mi++;
Console.WriteLine (“Values: " + i + " / " + o + " / " + mi + " / " + mi2);
}
Values: 21 / 20 / 21 / 20
MyInt is a struct instead of a class, so it is now stored
on the stack, and assignment means copying!
26 November 2002 CS 201J Fall 2002 23
Other Differences
• synchronized → lock
• Meaning is identical (lock is a better name)
• import → using
• Meaning slightly different (more like C++ namespaces
than Java packages)
• extends → : implements → :
• Exceptions
• C# can have catch without exception type (catches
any exception like: catch (Exception e) …
– C# has no throws clauses in declaration, and will
compile code without catch clauses
In client code:
x.score = x.score + 1; x.setScore (x.getScore () + 1);
insert = proc (s: set, el: t) No this object, need to explicitly pass in set.
rep$addh (down (s), el) Explict conversions between rep and
end insert abstract type (up and down)
elements = iter (s: set) yields (t)
for el: t in rep$elements (down (s)) do
yield el
end Simple and elegant way to define iteration
end elements abstractions. Java has nothing (enumerations),
end set C# has foreach for builtin arrays and ArrayList
type, but you can’t define your own.
26 November 2002 CS 201J Fall 2002 30
Good News / Bad News
• Good News
– You now know enough to list that you know
Java, C, C# and CLU on your resume!
• Bad News
– No one is hiring CLU programmers
– You shouldn’t want to work for anyone too
easily impressed by you knowing the others
Applicants must also have extensive knowledge of Unix,
although they should have sufficiently good programming taste
to not consider this an achievement.
Hal Abelson, MIT job advertisement
26 November 2002 CS 201J Fall 2002 31
What is the “J” for?