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!
== Preprocessing, compilation and packaging == {| class="wikitable" style="width:80%;" |- ! style="width:40%;"| [[Preprocessor|Preprocessing]], [[Compiler|compiling]] and [[Modular programming|packaging]] !! style="width:30%;"|Java !! style="width:30%;"|C# |- |[[Namespaces]] || {{yes|Packages}} || {{yes|Namespaces}} |- |File contents || {{no|Restricted}} || {{yes|Free}} |- |Packaging || {{yes|Package}} || {{yes|public/internal visibility on namespace members, which the build system translates to modules and assemblies at the CLR-level}} |- |Classes/assembly search path || {{partial|ClassPath}} || {{yes|Both compile-time and runtime}}<ref>{{cite web |url=http://msdn.microsoft.com/en-us/library/823z9h8w.aspx |title=Element |publisher=Msdn.microsoft.com |access-date=18 August 2013}}</ref><ref>{{cite web |url=http://www.vcskicks.com/csharp_assembly.php |title=C# Assembly β Custom Reference Path β Visual C# Kicks |publisher=Vcskicks.com |access-date=18 August 2013}}</ref> |- |Conditional compilation || {{no}}; but see Apache Ant<ref>{{cite web |url=https://weblogs.java.net/blog/schaefa/archive/2005/01/how_to_do_condi.html |title=How to do Conditional Compilation with Java |publisher=weblogs.java.net |access-date=11 August 2015 |archive-url=https://web.archive.org/web/20130105043626/http://weblogs.java.net/blog/schaefa/archive/2005/01/how_to_do_condi.html |archive-date=5 January 2013 |url-status=dead |df=dmy-all}}</ref> || {{yes}} |- |Custom errors/warnings || {{yes}}; AnnotationProcessor || {{yes}} |- |Explicit regions || {{no}} || {{yes}} |} === Namespaces and file contents === In C#, [[namespace]]s are similar to those in [[C++]]. Unlike [[Java package|package]] names in Java, a namespace is not in any way tied to the location of the source file. While it is not strictly necessary for a Java source file location to mirror its package directory structure, it is the conventional organization. Both languages allow importing of classes (e.g., {{code|import java.util.*|java}} in Java), allowing a class to be referenced using only its name. Sometimes classes with the same name exist in multiple namespaces or packages. Such classes can be referenced by using fully qualified names, or by importing only selected classes with different names. To do this, Java allows importing a single class (e.g., {{code|import java.util.List|java}}). C# allows importing classes under a new local name using the following syntax: {{code|1=using Console = System.Console|2=csharp}}. It also allows importing specializations of classes in the form of {{code|1=using IntList = System.Collections.Generic.List<int>|2=csharp}}. Both languages have a [[static import]] syntax that allows using the short name of some or all of the static methods/fields in a class (e.g., allowing {{code|foo(bar)}} where {{code|foo()}} can be statically imported from another class). C# has a static class syntax (not to be confused with static inner classes in Java), which restricts a class to only contain static methods. C# 3.0 introduces [[extension method]]s to allow users to statically add a method to a type (e.g., allowing {{code|foo.bar()}} where {{code|bar()}} can be an imported extension method working on the type of {{mono|foo}}). The [[Sun Microsystems]] Java compiler requires that a source file name must match the only public class inside it, while C# allows multiple public classes in the same file, and puts no restrictions on the file name. C# 2.0 and later allows splitting a class definition into several files by using the {{mono|partial}} keyword in the source code. In Java, a public class will always be in its own source file. In C#, source code files and logical units separation are not tightly related. === Conditional compilation === Unlike Java, C# implements [[conditional compilation]] using [[preprocessor directive]]s. It also provides a {{mono|Conditional}} [[Annotation|attribute]] to define methods that are only called when a given compilation constant is defined. This way, [[Assertion (computing)|assertions]] can be provided as a framework feature with the method {{code|Debug.Assert()}}, which is only evaluated when the {{mono|DEBUG}} constant is defined. Since version 1.4, Java provides a language feature for assertions, which are turned off at runtime by default but can be enabled using the {{code|-enableassertions}} or {{code|-ea}} switch when invoking the JVM.
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)