Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Comparison of C Sharp and Java
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== {{anchor|Enumerated types}}Enumerations === Both languages define enumerations, but they are implemented in fundamentally different ways. As such, enumerations are one area where tools designed to automatically translate code between the two languages (such as Java to C# converters) fail. C# has implemented enumerations in a manner similar to C, that is as wrappers around the bit-flags implemented in primitive integral types (int, byte, short, etc.). This has performance benefits and improves interaction with C/C++ compiled code, but provides fewer features and can lead to bugs if low-level value types are directly cast to an enumeration type, as is allowed in the C# language. Therefore, it is seen as [[syntactic sugar]].<ref>{{cite web |url=https://www.dotnetperls.com/enum |title=Enum. |publisher=.NET Perls |location=Internet |language=en |access-date=14 November 2016 |quote='''Performance.''' Enums are fast. They are almost never a performance concern. They are just syntactic sugar on a type like int, which is also fast. […] '''Type.''' An enum has an underlying type. Each time we use the enum, we are using the underlying type. The enum has syntactic sugar on top.}}</ref> In contrast, Java implements enumerations as full featured collection of instances, requiring more memory and not aiding interaction with C/C++ code, but providing additional features in [[Reflective programming|reflection]] and intrinsic behavior. The implementation in each language is described in the table below. {| class=wikitable ! ! Java ! C# |- | Definition | In Java, the enumeration type is a [[Class (computer programming)|class]], and its values are [[Object (computer science)|objects]] (instances) of that class. The only valid values are the ones listed in the enumeration. The enumeration type may declare [[Member variable|fields]], allowing each individual enumerated value to reference additional data associated uniquely with that specific value. The enumeration type may also declare or override [[Method (computer programming)|methods]], or implement [[Protocol (object-oriented programming)|interfaces]].<ref name="enum">{{cite web |url=http://www.25hoursaday.com/CsharpVsJava.html#switch |title=A Comparison of Microsoft's C# Programming Language to Sun Microsystems' Java Programming Language |author=Dare Obasanjo |year=2007 |publisher=Dare Obasanjo | quote = In Java, enumerated types are a full-fledged class that means they are typesafe and can be extended by adding methods, fields or even implementing interfaces. Whereas in C#, an enumerated type is simply syntactic sugar around an integral type (typically an int) meaning they cannot be extended and are not typesafe. |access-date=6 September 2012 |url-status=live |df=dmy-all |archive-url=https://web.archive.org/web/20011217071122/http://www.25hoursaday.com:80/CsharpVsJava.html |archive-date=17 December 2001}}</ref> | [[Enumeration]]s in C# are implicitly derived from the {{mono|Enum}} type that again is a value type derivative. The value set of a C# enumeration is defined by the ''underlying type'' that can be a signed or unsigned integer type of 8, 16, 32 or 64 bits. The enumeration definition defines names for the selected integer values.<ref name="enum"/><ref>{{cite web |url=http://www.cstruter.com/ |title=Java 5: Taming the Tiger: Syntactic Sugar | last1 = Prof. Dr. Gruntz | first1 = Dominik |date=8 April 2005 | language = de |publisher=Fachhochschule Aargau, Nordwestschweiz |archive-url=https://web.archive.org/web/20120708065042/http://cstruter.com/ |archive-date=8 July 2012 |quote=Enumerationen sind die heimlichen Sieger von Java 1.5. Nach vielen Beteuerungen durch Sun, Enums seien in Java überflüssig und können einfach nachgebildet werden, wurden sie nun doch eingeführt. Die einfachste Möglichkeit einer Enumeration der Jahreszeiten sieht wie folgt aus … Das Schlüsselwort enum steht für eine spezielle Art von Klasse, die eine Enumeration definiert. … ''Im Gegensatz zu anderen Programmiersprachen wie C/C++ und C# kann man ihnen per Gleichheitszeichen keine ganzen Zahlen zuordnen.'' |access-date=10 September 2012 |url-status=dead |df=dmy-all}}</ref> By default the first name is assigned the value 0 (zero) and the following names are assigned in increments of 1. Any value of the underlying primitive type is a valid value of the enumeration type, though an explicit cast may be needed to assign it. |- | Combining | Java enumeration set and map collections provide functionality to combine multiple enumeration values to a combined value. These special collections allow compiler optimization to minimize the overhead incurred by using collections as the combination mechanism. | C# supports bit-mapped enumerations where an actual value may be a combination of enumerated values bitwise or'ed together. The formatting and parsing methods implicitly defined by the type will attempt to use these values. |} In both C# and Java, programmers can use enumerations in a [[switch statement]] without conversion to a string or primitive integer type. However, C# disallows implicit fall-through unless the case statement does not contain any code, as it is a common cause of hard-to-find bugs.<ref>{{cite web |url=http://www.25hoursaday.com/ |title=A Comparison of Microsoft's C# Programming Language to Sun Microsystems' Java Programming Language: C. An Ever So Slight Feeling of Dèjà Vu: 4. Switch Statement |author=Dare Obasanjo |year=2007 |publisher=Dare Obasanjo |archive-url=https://web.archive.org/web/20120919093308/http://25hoursaday.com/ |archive-date=19 September 2012 |access-date=10 September 2012 |url-status=dead |df=dmy-all}}</ref> Fall-through must be explicitly declared using a [[goto]] statement.<ref>{{cite web |url=http://msdn.microsoft.com/en-us/library/13940fs2%28v=vs.110%29.aspx |title=goto (C#) |publisher=Msdn.microsoft.com |access-date=18 August 2013}}</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)