Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

C#: A language alternative or just J--?, Part 2

The semantic differences and design choices between C# and Java

  • Print
  • Feedback

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.

  • Print
  • Feedback

Resources