14.07.2013 Views

Contents - Cultural View

Contents - Cultural View

Contents - Cultural View

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Comparison of Java and C Sharp 65<br />

frequently do) implement interfaces. For example, the built-in integer types implement a number of interfaces.<br />

Apart from the built-in primitive types, Java does not include the concept of value types.<br />

Enumerations<br />

Enumerations in C# are derived from a primitive integer type (8, 16, 32, or 64 bit). Any value of the underlying<br />

primitive type is a valid value of the enumeration type, though an explicit cast may be needed to assign it. C# also<br />

supports bit-mapped enumerations where an actual value may be a combination of enumerated values bitwise or'ed<br />

together.<br />

Enumerations in Java, are full objects. The only valid values are the ones listed in the enumeration. Also, as objects,<br />

each enumeration can contain its own methods and fields, like a dedicated toString() method, for instance, or<br />

comparators. They can be considerered classes with a predefined amount of instances, which are the enum constants.<br />

Special enumeration set and map collections provide fully type-safe functionality with minimal overhead.<br />

In both C# and Java programmers can use them in a switch statement without conversion to a string or primitive<br />

integer type.<br />

Delegates / method references<br />

C# implements object oriented method pointers in the form of delegates. A delegate is a special type which can<br />

capture a type-safe reference to a method. This reference can then be stored in a delegate-type variable or passed to a<br />

method through a delegate parameter for later invocation. C# delegates support covariance and contravariance, and<br />

can hold a reference to any signature-compatible static method, instance method or anonymous method or lambda<br />

expression.<br />

Delegates should not be confused with closures and inline functions. The concepts are related because a reference to<br />

a closure/inline function must be captured in a delegate reference to be useful at all. But a delegate does not always<br />

reference an inline function, it can also reference existing static or instance methods. Delegates form the basis of C#<br />

events but should not be confused with those either.<br />

Java does not have a language-level construct like the C# delegate. To mimic the functionality the developer will<br />

have to create an ad-hoc implementation of a class (typically an anonymous inner class) where one method performs<br />

the desired invocation. The following code snippets show how to mimic most of the delegate functionality in Java.<br />

Java C#

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!