Comparison of Java and C #



1. Access control: C # have public, internal, protected, private, much more internal than java, in fact, pack it with access to almost java, internal representation with a collection compiled under the category (such as exe, dll) can exchange visits.
For protected, java and C # are different. In java, protected and package access level almost, that is not private. And in C #, protected and private about the same, namely the members of its logo is private.
There is such a situation: a member of the class, it needs access to the quilt class, while the same can be set (whether the java package or a collection compiled in C #) to access other classes, how to do it? In java, as long as it protected on the line. In C #, you can specify the internal protected (both random order).
In such a situation are: a member of the class, subclasses need access to it, but can not be the same set (either a compiled set of java packages or in C #) to access other classes, how to do it ? In C #, you can specify protected (both random order). But java can not do anything.
2. C # have the concept of static constructor, which like java static initial module.
C #: static [class name] {}
Java: {} static
. 3. Java in the main function must be public static void main (String [] args) look, otherwise refuse to run virtual machines. In C #, the Main function may be private (or even may be protected), there may be no parameters, int return value. A bit like the C language.
4. Csc.exe have found a good function, followed by one of 100 lowercase L, it warns: "l" suffix easy and the number "1" confusion; for clarity, use the "L".
5.C # provides a mechanism so that a variable can be dynamically assigned once and can no longer be changed. That is the function readonly keyword.
6.java inheritance, polymorphism aspects, much stronger than C #. Java default polymorphism, C # requires adding virtual (inherited method) and the override (inherited method), and C # requires access modifier can not change the original, so unlike java, you can specify more relaxed access. If someone use C # to write the program, you must often take virtual and override, you must also copy the original access control characters will not be very depressed? When Does anyone use object-oriented features of C #, it will abandon characteristic polymorphic? This causes much confusion ah.
Polymorphism is the essence of object-oriented, so the default is not it better like java?
7. C # in the new method may also be used to specify a sub-class method with the same signature to hide the parent class. It is not superfluous? You can also do, but csc.exe will warn you, such as "lan.Other.Main (string [])" hides inherited member "lan.HelloWorld.Main (string [])" . If it is intentionally hidden, use the keyword new.
As default so nice like java.
But then again, C # to do so for a reason. If the class B inherits class A, B Next there is added a method called hi (), that is B-specific. Then class A (assumed to be someone else to maintain, you can not see the source code) suddenly also added a method hi (). If you own the B hi () with the A hi () return value is not the same, when you update the library A, may cause the program to run or error can not be compiled. C # is very good place to avoid this problem. (Although the probability of such problems quite small ...)
8.C # protected from a class to be inherited, use the keyword sealed. And when you define a constant, use const.
Like java unification with the final shining.
9. In C #, to compare two reference variables point to the same object can not be used in the java = =, and use in the Object ReferenceEquals method. C #, you can not use an instance of the class to call the class method of the class, you must use the class name. Therefore, the java o1 = = o2 equivalent to the C # Object.ReferenceEquals (o1, o2).
10.C # no original wrapper types, but also provides automatic unboxing features, and some, like java. The difference is, C # automatic packing, unpacking the necessary cast.
100 = I int;
Object obj = I;
I = (int) obj;
specifically how loaded and removed, we do not know. CLR will only know that the object is converted to int.
11.java inner classes are sometimes helpful. C # to that, it provides only a static inner classes. This means that an external class is only equivalent to a namespace only. Inner classes in C # to access private members of the outer class, which may make it a little bit with.
Although there are 12.C # operator overloading, but for consistency throughout .net, and should not be discouraged. Because some of .net languages no operator overloading, and a .net goal is to eliminate the differences in various languages.
13.C # struct a more value type, it is just like primitive types. When necessary, Microsoft will help you be packaged into a struct Object, like the package as an int. You can also think that struct inherit from Object, although struct itself does not support inheritance. (Struct can not initialize new, but inside it's content must be initialized before calling its methods; struct no destructor method; struct no default constructor).

November 2, 2007
1.java access control class character only public, or not (i.e., the default packet access). However, in C #, class, and interface access control identifier may be a public / private / internal / protected / internal protected. Of course, you must first obtain access to the class, it is possible to access to members of the class.
A C # collection can contain multiple public class or interface, nothing to do with the file name.
2.C # The interface can not contain constants, and java can.
3.C # in as java and instanceof function of the same. But C # is said to provide a higher efficiency as keywords.
4. Interface and abstract classes in C # and are similar in java, to mention here the abstract class interface design and the design of distinction. If you change the design of an interface, such as adding a method, using a user code before you will have to change their code, otherwise can not run and compile. But if it is an abstract class, you can provide a default implementation of the method containing the user code does not need to be changed.
5.C # When a class implements an interface, its associated methods do not have to specify override; but when a class inherits the abstract method of an abstract class, must be added the override, or as hidden. (In fact, only the abstract method or virtual, or interface method that is to be covered override. Reason not to override.)
. 6. There is a "polymorphic starting point" problem in C #. If a class implements an interface methods, but the interface to the class have polymorphic functions, multiple inheritance down to this, the class must specify a virtual, multi-state starting the next subclass override will be able to provide a multi-state, no more virtual.
However, abstract methods abstract class is a polymorphic default starting point subsequent subclasses on the line as long as the override.
7. When a class implements two interfaces, two interfaces have the same definition of a method, C # there is a resolution mechanism, called a display realized. Java simply do not deal with this situation, anyway, will be able to achieve after the call, and do not indicate which interface, leaving the programmer to consider.
Of course, C # display realize there are other features. For example, the interface has a method called F A (), B class implements A. It stands to reason instance B will be able to free calls f (), but if there is such a request: Example B only after being cast as A can invoke f (). In java, such unreasonable demands are not allowed. But C # can do, it is to show by way of implementation. Who would use this feature?

Reproduced in: https: //www.cnblogs.com/yitian/archive/2009/05/27/1490890.html

Guess you like

Origin blog.csdn.net/weixin_34268843/article/details/93710359