Differences between Java and C #

Source: Internet
Author: User

1. attributes: Get and set methods are used for definition and access in Java, which can be non-paired. C # Is a real attribute. Get and set must appear at the same time during definition. You can use a. number when asking questions. No get, Set
2. The object index is the object array public story this [int Index] {
3. in C #, when no range modifier is used, the default value is protect, so it cannot be accessed outside the class.
4. Because Java requires that only one public class can be included in a file, and the class name must be the same as the file name. This is a difference.
5. in C #, it uses the main method to locate the entry. If Program If there is no method named main in, the system will produce "the portal error cannot be found". Do not write main as main.
6. C # There are more predefined simple data types than Java. For example, C # has a unit, that is, an unsigned integer.
7. Forget the static final modifier in Java. In C #, the designer who can declare C # using the const keyword of a constant also adds the readonly keyword. The readonly field can only be set through the constructor or class.
8. entry point of the public class: C # Can Be Used to reload the main (main in Java), and allow the main with int return values and null Parameters
9. In Java, the switch statement can only process integers. But the switch statement in C # is different, and it can also process character variables. Consider using the switch statement below to process C # of string variables # Code
10. C # No> Shift Operator
11. Goto keywords: Java does not use goto keywords. In C #, Goto allows you to switch to the specified tag. However, C # treats Goto With a very cautious attitude. For example, it does not allow goto to be transferred to the inside of the statement block. In Java, you can replace the Goto in C # With the break or continue of the label-containing statement.
12. int [] x = {0, 1, 2, 3}; int X [] = {0, 1, 2, 3}; but in C, only the first line of code is valid. [] cannot be placed after the variable name.
13. Unlike Java, C # allows you to specify an alias for a namespace or a class in the namespace: Using theconsole = system. Console;
14. In Java, the package name is also an actual entity, which determines the directory structure of the. Java file. In C #, the physical package and the logical name are completely separated. The entity in the. Net package is called assembly ). Each Assembly contains a manifest structure. Manifest lists the files contained in an assembly, controls which types and resources are exposed outside the assembly, and maps references to these types and resources to files containing these types and resources. An assembly is self-contained. An assembly can be placed within a single file or divided into multiple files .. This encapsulation mechanism of net solves the problems faced by DLL files, that is, the notorious DLL hell problem.
15. In Java, the Java. lang package is the default package, and C # does not contain the default package.
The access modifier in 16. C # basically corresponds to the access modifier in Java, but there is an extra internal. In short, C # has five types of accessibility: public: Members can access from any code. Protected: A member can only be accessed from a derived class. Internal: members can only access from within the same assembly. Protected internal: A member can only be accessed from a derived class in the same assembly. PRIVATE: members can only be accessed within the current class.
17. Because the final keyword does not exist in C #, you can use the sealed keyword if you want a class to be no longer derived.
18. Unlike Java, interfaces in C # cannot contain fields ). In addition, in C #, all methods in the interface are public methods by default. In Java, a method declaration can carry a public modifier (even if this is not necessary), but in C #, explicitly specifying a public modifier for an interface method is invalid. For example, the following C # interface will generate a compilation error.
19. The is Operator in C # is the same as the instanceof operator in Java. Both of them can be used to test whether the instance of an object belongs to a specific type. In Java, there is no operator equivalent to the as operator in C. The as operator is very similar to the is operator, but it is more aggressive: If the type is correct, the as operator will try to convert the referenced object to the target type; otherwise, it sets the variable reference to null.
20. C # still retains the manual memory management method of C ++, which is suitable for use in extremely important scenarios of speed, which is not allowed in Java
21. In C #, all exceptions are derived from an exception class.
22. The Enum type (not available in Java) is used as the type of a variable value, so that the possible value range of the variable is limited to the value in the enumerator.
23. the structure (struct) is similar to the class, and the structure is a value type, which is stored in the stack or embedded. The structure can implement interfaces and have Members like the class, however, the structure does not support inheritance.
24. The first part of the attribute declaration syntax is very similar to the domain declaration. The second part includes a set process and/or a get process.
25. value passing method: When passing parameters for simple data types in Java, all parameters are passed as values; in C #, if a ref parameter is added, the value is passed as a reference (the external variable is changed when the parameter is changed inside the method). The addition of out is basically the same as that of ref, however, out does not require parameter initialization.
26. C # the pointer is retained. Unsafe
27. Proxy: Delegate can be viewed as a function pointer proxy in C ++ or other languages to encapsulate callable methods. You can write a method in the class and create a proxy on the method. Then the proxy can be passed to the second method. In this way, the second method can call the first method. The proxy is a reference type derived from the public base class system. Delegate. There are three steps to define and use a proxy: declare, create an instance, and call. The proxy uses delegate to declare the syntax.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.