Difference between Java and C #
1. source file. C # Can Be In A. CS file. Multiple name sapce (namespace, somewhat similar to pacakage) appears and multiple classes are defined. C # contains preprocessing commands.
2. data type.
A) C # defines the struct (structure), which cannot be abstract or inherited. It is a value type.
B) C # There is no basic type concept, all of which are object-oriented, and INT is short for int32.
C) in Java, const is a reserved word. Final has the same meaning as const in C #. in C #, readonly allows initial values to be assigned during runtime.
D) string comparison. You can use = to compare values in C # And use stringbuffer to reduce memory overhead like Java.
3. operators. C # added checked and unchecked to bypass compilation errors. For example, "pay a long value to int32. If the value is 32 or zero, no error will occur. Otherwise, an overflow error will occur. unchekced means to tell the compiler not to handle overflow and convert it as much as possible"
4. Branch statements. In Java, Goto is a reserved word. The case in the switch in C # Won't be executed sequentially, and the break or goto keyword must be at the end.
5. The loop statements are similar.
6. Access modifiers. Put main, C # added internal and protected internal in the range. The former indicates that it can only be visible from the current Assembly, that is, the DLL or EXE that can be assembled during compilation, similar to jar; the latter indicates that it is visible to the current Assembly or the class inherited from this include class. Sealed is equivalent to the final keyword in Java.
7. methods. Similar to Java, only basic types are passed through values, and others are passed references. However, C # can also pass the basic type in reference mode and use the ref keyword. The out keyword is similar to the Oracle stored procedure. The difference with ref is that the method ignores the out parameter initial value and the method must assign a value to the out parameter.
8. attributes. The getter and setter statements are not the same.
9. Inheritance. C # adds the base keyword to point to the parent class. In C #, if the parent class method has the virtual keyword, it can be overwritten by the quilt class. Override must be declared during rewriting. Similar to abstract classes, abstract keywords must also be overwritten by override. The interface in C # cannot declare constants, and its attributes only represent getter or setter.
10. Events. Java does not exist (Java implements custom listener class execution events ). C # defines a delegate similar to a function pointer to implement events.
11. Operator overloading. Java. The operator overload must be a static method of the class. The overload =, and other operations such as judgment must also be overloaded.
12. Exceptions. Similar to Java, except that there is no throws keyword and exceptions do not need to be intercepted (of course, they must be intercepted at the main entrance of the program ).
13. C # adds an indexer, which can be a category class or structure like an array. My personal feeling is just a few words, but it still causes some troubles.
14. C # has a much higher attribute than Java's annotation. However, after adding AOP, this dynamic function Java is also good.
15. In C #, you can also directly use pointers to open up or recycle memory. You only need to use two keywords: unsafe and fixed.
Original article address