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!
=== 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.
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)