C#: A language alternative or just J--?, Part 2
The semantic differences and design choices between C# and Java
By Mark Johnson, JavaWorld.com, 12/21/00
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Page 2 of 5
Sparse2DArray s2d = new Sparse2DArray(1000, 10);
s2d[512, 6] = 3.14159265;
Console.WriteLine(s2d[12,5]); // will print
zero
Structs, enumerations, properties, delegates
C# defines several class member types that do not exist in Java: structs, enumerations, properties, delegates. This section describes the function of each type.
Structs
A struct is somewhat like a struct in C, except that a C# struct may have any kind of class member, including constructors
and methods; and the default accessibility for struct members is private, rather than public as in C. Like C structs, though,
C# structs always copy by value and are therefore both mutable and exempt from dynamic memory management (i.e., garbage collection).
Variables that copy by value don't need garbage collection because the memory used to represent them disappears when those
variables go out of scope. In many ways, a struct is similar to a class: a struct can implement interfaces and can have the
kinds of members that classes can have. But structs can't inherit from other structs.
Interestingly, the scalar types like int and double are implemented in C# as aliases for predefined structs in the System namespace. In other words, when you define an int variable in C#, for example, you're actually defining an instance of System.Int32, which is a struct predefined in C# language. The struct System.Int32, in turn, inherits all of the members of System.Object. (While the language specification says structs can't inherit, it also says they do inherit from System.Object; presumably, this is an exception.) So, basically, every primitive type in C# is also an object, and therefore "object" in
C# means something other than the traditional interpretation of "instance of a class."
Since primitives, classes, and structs inherit from System.Object, everything in C# is an object and can be treated as such. This is how C# represents primitives as objects: they already
are objects. Yet instances of primitives and structs copy by value, while instances of class copy by reference. Conversion from
value types to reference types and vice versa are the boxing and unboxing operations mentioned earlier.
Enums
Another class member type Java does not provide is the enum. While similar to enums in C, C# enums are based on an "underlying
type," which can be any signed or unsigned integer type. Enumerations are derived from the built-in class System.Enum, and therefore every enum inherits all of that class's members.
For example, an enum based on an unsigned long integer can be defined as:
enum description: ulong {
Good,
Bad,
Ugly
};
Enums are inherently type-safe and require explicit type casts when they are assigned to and from integer types, even the
type from which the enum is derived.
A common idiom in Java for "faking" enums is to define public final ints in an interface and then inherit the interface, like this:
public interface Description {
public final int Good = 0,
public final int Bad = 1,
public final int Ugly = 2
};
public class Sheriff implements Description {
protected int _description;
public Sheriff() {
_description = Good;
}
}
While this idiom improves readability in Java, it doesn't provide type safety, nor can methods overload on two such "enums,"
because they're really integers. C# provides true enumerated types.
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Resources
- Read Part 1 of "C#A Language Alternative or Just J--?," Mark Johnson (November, 2000) for a comparison of language features
and design trade-offs between C# and Java
http://www.javaworld.com/javaworld/jw-11-2000/jw-1122-csharp1.html
- "C#, The Natural Progression," Michael Perry (JavaWorld, August 2000), a high-level article, reprinted from ITworld.com, that outlines C# technology
http://www.javaworld.com/javaworld/jw-08-2000/jw-0804-itw-csharp.html
- ITworld.com and JavaWorld columnists duke it out (pardon the pun) on C# vs. Java
http://www.javaworld.com/jw-11-2000/jw-1122-letters.html
- Mark Johnson on how Java and C# differ (audio clip 3:45)
http://mithras.itworld.com/media/hearit/1100/001122mjohnson_csharp.ram
- Mark Johnson compares the pros and cons of .Net and the Java 2 Platform, Enterprise Edition (audio clip 2:18):http://mithras.itworld.com/media/hearit/1100/001122mjohnson_msdotnet.ram
- "Bits, Flames, and Links," Bobby Schmidt (MSDN Online, Sept. 29, 2000) -- gets you started experimenting with C#, and includes a generous resource list
http://msdn.microsoft.com/library/welcome/dsmsdn/deepc08172000.htm
- "C#, The Natural Progression," Michael Perry (JavaWorld, August 2000) -- a high-level article, reprinted from ITworld.com, that outlines the C# technology
http://www.javaworld.com/javaworld/jw-08-2000/jw-0804-itw-csharp.html
- ITworld.com and JavaWorld columnists duke it out (pardon the pun) on C# vs. Java
http://www.javaworld.com/jw-11-2000/jw-1122-letters.html
- "C# Under the Microscope" (Slashdot, Aug. 9, 2000) -- a good, quick analysis of C#. The hacker prattle following the article provides more heat than light, but
also the occasional worthwhile observation
http://slashdot.org/articles/00/08/09/1612254.shtml
- "Microsoft .Net vs. J2EE -- How Do They Stack Up?" Jim Farley (java.oreilly.com)
http://java.oreilly.com/news/farley_0800.html
- If you're running Windows 2000, download this "pre-beta" evaluation kit
http://msdn.microsoft.com/downloads/default.asp
- "Deep Inside C#An Interview with Microsoft Chief Architect Anders Hejlsberg," John Osborn, (www.oreilly.co) -- a fascinating article with the designer of C#. Be sure to read the James Gosling interview mentioned here, since Gosling's
comments, it appears to me, were used out of context
http://windows.oreilly.com/news/hejlsberg_0800.html
- A preliminary injunction in the Sun vs. Microsoft case, from Northern California U.S. District Court
http://techlawjournal.com/courts/sunwvmsft/19981117ord.htm
- "Sharp New LanguageC# Offers the Power of C++ and Simplicity of Visual Basic," Joshua Trupin (MSDN Online, September 2000)
http://msdn.microsoft.com/msdnmag/issues/0900/csharp/csharp.asp
- "Report from Microsoft PDC 2000," John Ruley (Byte.com, July 17, 2000) -- a first-hand report from the Microsoft Professional Developers Conference, where C# was presented
http://www.byte.com/feature/BYT20000714S0003
- "Microsoft Challenges Java with C Sharp," Charles Babcock, (Inter@ctive Week, July 3, 2000) -- includes news about potential C# standardization
http://www.zdnet.com/intweek/stories/news/0,4164,2597408,00.html
- "Java business conferenceSun cancels Java standard plans," John Cox (Network World Fusion Dec. 8, 1999) -- documents Sun's flip-flop on Java standardization
http://www.nwfusion.com/news/1999/1208std.html
- "Microsoft Pitches 'C sharp' Against Java," John Leyden (vnunet.com, June 27, 2000)
http://www.vnunet.com/News/1104875
- "Microsoft Cans Visual J++ Tool," John Geralds (vnunet.com, July 12, 2000) -- Surprise, surprise
http://www.vnunet.com/News/1106388
- "Microsoft .Net Plans Face Court Hurdle," John Leyden (vnunet.com, June 23, 2000) -- How will .Net play in the context of the antitrust case? Fear, uncertainty, and doubt, for those who just
can't get enough
http://www.vnunet.com/News/1104495
- A large list of C# programming articles
http://www.hitmill.com/programming/dotNET/csharp.html
- ECMA (European Computer Manufacturers Association) homepage
http://www.ecma.ch/
- ECMA News has a discussion of C#'s submission to ECMA
http://www.ecma.ch/ecma1/NEWS/NEWS.HTM#Two%20new%20projects%20for%20ECMA%20TC39
- "C Sharp Previews Sound Flat," Charles Babcock, (Inter@ctive Week, July 17, 2000) has a good competitive analysis
http://www.zdnet.com/intweek/stories/news/0,4164,2604273,00.html
- "Microsoft's .Net Strategy," Gopalan Suresh Raj (Oct. 13, 2000)
http://www.execpc.com/~gopalan/misc/viewpoint.html
- "C# Strikes a Chord", Jacques Surveyer (Dr. Dobb's Journal)
http://www.ddj.com/articles/2000/0065/0065g/0065g.htm
- "C# Is Pronounced 'See Sharp,'" Arthur Griffith (windows.oreilly.com)
http://windows.oreilly.com/news/seesharp_0700.html
- Sign up for the JavaWorld This Week free weekly email newsletter and keep up with what's new at JavaWorld
http://www.idg.net/jw-subscribe