C # and C , Java Comparison Overview Ben Albahari Glory Translation This paper is earlier. Welcome to any misfortune. Thank you. Thanks to the support and feedback of the following people (in the order of letters): Don Box, C.R. Manning, Joe Nalewabau, John Osborn, Thomas Rhode, Daryl Richter. This article is available in new programming methods provided by C # and its improvement to two nearest neighbor Java and C . C # In many ways to improve C in a similar manner, I don't intend to repeat things such as single object hierarchy. The body starts with the similar point of C # and Java, and then focuses on the new features of C #. Background In June 2000, Microsoft announced a new programming language called C # at the same time. C # is a highly integrated simplicity, expression, and strength-oriented object-oriented language. The .NET platform is run in public language (similar to Java virtual machine) and a variety of languages (they can work together by compiling into intermediate languages). Some symbiosis relationships: C #'s characteristics and .NET collaboration is very good, and vice versa (although .NET's goals are well collaborated with multiple languages). This article is mainly concerned about C #, but it will be occasionally mentioned .NET. The design of C # draws on a variety of languages, but the main thing is Java and C . It is designed by Anders Hejlsberg (Ding Ding Delphi Language Designer) and Scott Wiltamuth. table of Contents
C # and Java Attribute Indexer Entrusted Event Enumeration Collection and Foreach Statement Structure Type Consistent Operator Overload Polymorphical Interface Process Parameters Modifier Characteristics Select Statement Predefined Type Field Modifier Jump Standard Set, Name Space and Access Level Pointer computing multi-dimensional array constructor and sectors Hosting Running Room Interoperability Conclusion 1.C # and Java are the list of C # and Java shared features, and the purpose is to improve C . Although these features are non-culturally focused, it is very important to understand the similarities between them.
Compile to machine independence, language independent code, running in a managed operating environment using garbage collection mechanism, and abandoning pointers (C #, pointers are limited to use within code "code-based code) powerful reflex capacity without head files, All code is in the package or document set, there is no class declaration loop dependence all classes are derived from Object, and must be assigned with the new keyword on the heap when entering the target to lock / synchronize code, by the object Plocking to support multi-threaded interface support: Multi-inherit interface Functions or constants must belong to categories and strings to save the length of the length. The boundary check capability is always used forever. "Operator, no longer" -> "," :: "operator null and boolean / bool Yes Keyword All values must be initialized if the IF statement must be initialized to use an integer as a judgment condition try statement, you can follow Finally clause 2. Properties For Delphi and Visual Basic users, the property is a familiar concept. The purpose of the property is to formally formulate the concept of the acquired / setter, which is a widely used mode, especially in the RAD (fast application development) tool. The following is a typical code you might write in Java or C : foo.setsize (GetSize () 1); label.getfont (). SetBold (true); the same code will become: foo.size ; Label.font.bold = true; C # code is more intuitive for users who use foo and label, readability. In terms of the realization of attributes, almost the same simple: Java / C : public int getsize () {Return size;} public void setsize (int value) {size = value;} C #: public int size {get {return size;} set {size = value;}} Especially for readable and writable properties, C # provides a more refreshing way to handle this concept. In C #, the GET and SET methods are intrinsic, and people are maintained in Java and C . There are many advantages in the processing method of C #. It encourages programmers to think about attribute - labeled this property to be readily writable and who is more natural? Or should not be attribute at all? If you want to change your properties, you can check one (I have seen the acquisition device and setup of the same data member / field in the acquisition device of several hundred lines of code. . Note As long as you can, this also avoids the problem of each other. IDE can help do this (in fact, I suggest Microsoft Developers to do this), but should keep in mind a basic programming principle: try to do the abstraction of our problem space. And a language that supports attributes will help get better abstraction. Author Note: An opposition to this advantage of attributes believes: When using this grammar, you can't do it in a field or attribute. However, in Java (of course, it also includes C #), almost all of the true complex classes do not have a public field.
Fields generally have only as little access level as possible (the default default defined by the private / protected, or language), and only through the acquisitioner and the setup device method, which means you can get a beautiful grammar. Let IDE parsing code is also fully feasible: you can use different colors to highlight properties, or provide code completion information to indicate whether it is an attribute. We should also see that if a class design is good, the user will only care about the interface (or specification) of the class, not its internal implementation. Another possible argument is that attribute is not effective enough. In fact, a good compiler can return only a field of acquired acquisition, which is as fast as the direct access field. Say that even if the field is used to be valid than the acquiring / setup device, the use of properties also has the following: You can change the fields associated with the property in the future, without affecting the code that is dependent on this property. 3. Indexer C # By providing an indexer, you can handle objects like processing an array. Especially attributes, each element is exposed in a GET or SET method. Public class skyscraper {story; public story this [int index] {get {return stories [index];} set {if (value! = null) {stories [index] = value;}}} // .. . It looks more natural, more in line with the actual thinking mode. 4. The entrustment delegation can be considered a type of security, object-oriented function pointer, which can have multiple methods. The issue of delegate processing can be handled in C , and the interface can be handled in Java. It improves the function pointer mode by providing type security and supporting multi-way; it improves the interface by making method calls without internal class adapters or additional code dealing with multi-method call issues. The most important use is the event handling, and the next section will be introduced through an example. 5. Event C # provides direct support for events. Although event processing has always been the basic part of programming, it is surprising that most languages have made efforts to format this concept. If you look at how the mainstream framework is handled, we can mention the following example: Delphi's function pointer (called closed bag) and Java's internal class adapter, of course, there is also a Windows API message system. C # provides a refreshing event processing scheme using the Delegate and Event keywords.
I think the best way to describe this mechanism is to give an example to explain the process of declaration, trigger, and handling events: // The delegation declares defines the method of the method that can be called public Delegate Void Scoreeeventhandler (Int Newscore, Ref Bool Cancel); / / Generate Events Public Class Game {// Note Use Keyword Public Event ScoreeeventHandler Scorechange; Int Score; // Property Score;} set {r (score! = Value) {BOOL CANCEL = False; scorechange (value, ref cancel); if (! can) score = value;}}}} // Handling Event class public class refereE {public refree (Game Game) {// Monitor the score change in GAME game.ScoreChange = new ScoreChangeEventHandler (game_ScoreChange);} // Note that the method signatures and ScoreChangeEventHandler method signatures to match private void game_ScoreChange (int newScore, ref bool cancel) {if (newScore <100) System.Console.WriteLine ( " Good score "); Else {Cancel = True; System.Console.writeline ("No Score Can Be That High!");}}} // Test class public class gametest {public static void main () {game game = new game (); Referee RefereE = New Referee (Game); Game.score = 70; // Translation: Output good score game.score = 110; // Translation: Output no score can be this high!}} In GameTest, we created a GAME and a surveillance, respectively. Game's Referee, then, then we change the game of Game to see where Referene is backed up. In this system, Game does not know any knowledge of Refree, any class can listen and react with the GAME's score change. Keyword EVENT hides all entrustment methods other than = and - =. These two operators allow you to add (or remove) multiple event processors for processing the event.
Translation: We include the meaning of this sentence: public class game {public evening scoreeeeventhandler scorechange; protected void onscorechange () {if (scorechange! = Null) scorechange (30, ref true); // In the class, you can So use}} In this class, scorechange can only appear on the left side of the operator = and - = you may first encounter this system in the Graphical User Interface Frame. GAME is like a control of the user interface, which is triggered according to the user input, and Refree is similar to a form, it is responsible for processing the event. Author Note: Entrusted the first time to introduce by Microsoft Visual J is also designed by Anders Hejlsberg, and it is also one of the causes of Sun and Microsoft disputes in technology and legal. James Gosling, Java designer, has a good commentary for Anders Hejlsberg, and said that he is also a hobby, saying that he should call him "Method of Methods" because he and Delphi have broken silk. After studying Sun's dispute, I think Gosling is "everything is a class." It seems to be equal :) In the past few years, in the programming industry, "doing efforts to simulate the abstraction of reality" has been many people Instead, "The realistic is object-oriented, so we should use object-oriented abstraction to simulate it." Sun and Microsoft's debate about the commission can be seen here:
http://www.javasoft.com/docs/white/delegates.html http://msdn.microsoft.com/visualj/technical/tharticles/delegates/truth.asp 6. Enumeration enumeration you can specify a group Object, for example: public enum direction {north, east, west, South}; use: Direction Wall = Direction.North; this is a elegant concept, this is also C # why it will decide to keep it, but why Java But I chose to give up? In Java, you have to do this: declaration: public static int north = 1; public final static int ent = 2; public final static int west = 3; public final static intang = 4;} : Int Wall = Direction.North; It seems that the Java version is more expressive, but this is not the case. It is not type safe, you may accidentally assure any int type value to Wall and the compiler will not issue any complaints. Frankly, in my Java programming experience, I have never spent too much time because of non-type safety, I have written some extra things to capture errors. However, it is a quick thing to have an enumeration. C # A surprise to you is that when you debug the program, if you set a breakpoint in the place where the enumeration variable is used, the debugger will automatically translate the Direction and give you a well-readable information, not one you. I have to rectify the value: declaration: public enum direction {north = 1, EAST = 2, West = 4, South = 8}; Used: Direction Direction = Direction.North | Direction.west; IF ((Direction & Direction) .NORTH)! = 0) // .... If you set a breakpoint on the if statement, you will get a Direction you read and not a value of 5. Translation: This example will be modified, which will more help: declaration: public enum direction {north = 1, EAST = 2, West = 4, South = 8, middle = 5 / * Note this code * /} Using: Direction Direction = Direction.North | Direction.west; if ((Direction & Direction.North)! = 0) // .... If you set a breakpoint on the IF statement, you will get a readability. Good Direction (ie Middle) is not a value of 5. Author Note: The reason for enumeration is abandoned by Java may be because it can be replaced by class. As I mentioned above, use the class, we can't express a certain feature like other concepts. Java's "If it can be handled by class, what is the advantage of the philosophy of a new structure? The biggest advantage is simple - shorter learning curves, and there is no need to consider multiple ways to do with the same thing.
In fact, Java language has improved C in many ways, for example, without pointers, no header files, and single object hierarchy. All of these simplified commonality is actually programming - oh - simple, but because there is no enumeration, attributes and events we just mentioned, it makes your code more complicated. 7. Collection and foreach statement C # provides a shortcut for a For loop, and it also promotes more consistent collection: in Java or C : 1.While (! Collection.isempty ()) {Object O = Collection.get ( ); Collection.next (); // ... 2.FOR (int i = 0; i The C template parameter accepts any type as long as this type provides the implementation of the operation defined by the template. C # provides a different solution to this problem. In the previous section, I introduced the structure in the C #, pointing out that the basic type is just an alias for the structure. Since the structure has a method of all object types, then the code can write: int i = 5; system.console.writeline (I.TOSTRING ()); if we want to use the structure like the object, C # will be for you This structure is boxed to be object. When you need to use a structure again, you can implement: Stack Stack = new stack (); stack.push (i); // Packing Int J = (int) stack.pop (); // Unpacking unpacking is not only the needs of type conversion, but it is also a way of seamless processing between structures and classes. You have to make a clear packing is a work of creating a packaging class. In addition, the CLR can provide additional optimization processing for the contained object. Demolition: It can be thought that in C #, there is any value (structure) type, there is a package class: class t_box // t represents any value type {t value; t_box (t t) {value = t;}} When packing, for example: int N = 1; Object Box = N; conceptually equivalent to: int N = 1; Object Box = new int_box (i); when unpacking,, for example: Object Box = 1; int N = (int) Box; conceptually equivalent to: object box = new int_box (1); int N = ((int_box) box) .value; Author Note: The designer of C # should consider the template during the design process. I doubt that there are two reasons why they have not been used: the first is confusion, the template may be difficult to integrate with object-oriented features, which has brought too many (confusing) design possibilities for programmers, and it is very Cooperation between difficulties and reflex mechanisms. The second point is that the template will not be too useful if the .NET library (such as a set class) does not use a template. However, fruit .NET class uses them, there will be more than 20 languages of the .NET class have to be able to collaborate with template, which is technically very difficult to implement. Noting that template (generics) has been considering the Java Language Norms by Java Society. Perhaps each company will sing each other, SUN says ".NET has the least public syndrome", and Microsoft said "Java does not support multiple languages." (August 10, apologized) After reading an interview with Anders Hejlsberg (http://windows.oreilly.com/news/hejlsberg_0800.html), I feel that the template seems to have a horizon. However, the C # first edition has not yet, the reason is that the various difficulties we mentioned above. See the IL specification is such that the IL code can display templates (using a non-destroyed way to make the reflex mechanism can work well) and the bytecode is not, it is a very interesting thing. Here, I also give a link to the Java community consider the link to joining generic features: http://jcp.org/jsr/detail/014.jsp 10. Operator overload utilize operator overload mechanism, programmers can create people It feels like a simple type (such as int, long, etc.). C # implements a limited version of the C operator overload, which can make a brilliant example in which a plurality of operators overload such a good example is good. In C #, operators == are non-virtual methods of object classes (operators can't be virtual), which is compared by reference. When you build a class, you can define your own == operator. If you use your class in the collection, you should implement the IComparable interface. This interface has a Compareto (Object) method, if "this" is greater than, less than or equal to this object, it should return positive, negative, or 0 accordingly. If you want users to use your class with elegant syntax, you can choose to define <, <=,> =,> methods. Numeric type (int, long, etc.) implements the IComparable interface. Below is a simple example of how to handle and compare operations: public class score: iComparable {int value; public score {value = score;} public static bool operator == (score x, score y) {Return X .value == y.value;} public static bool Operator! = (score x, score y) {return x.Value! = y.Value;} public int compareto (Object O) {Return Value - ((Score) O ) .value;}} score a = new score (5); score b = new score (5); Object C = a; Object D = b; compare A and B: System.Console.writeline as compared to reference A == (Object) b); / / The result is FALSE comparison between A and B: System.Console.writeline (a == b); / / The result is TRUE compared to C and D: System.Console.writeline (c == d); / / The result is FALSE to compare the value of C and D: System.Console.writeline ((iComparable) c) .compareto (d) == 0); / / The result is TRUE you can The score class adds <, <=,> =,> operators. C # The compile period guarantees logically to appear an operator (! = And ==,> and <,> = and <=) must be defined together. 11. Polygon object-oriented language uses the false method expression polymorphism. This means that the derived class can have the same signature with the parent class, and the parent class can call the derived class. In Java, the method is virtual by default. In C #, you must use the Virtual keyword to be called by the parent class. In C #, an Override keyword is also required to indicate a method to overwrite (or implementing an abstract method) method of its parent class: class b {public virtual void foo ()}} Class D: b {public override void foo () {}} Attempts to override a non-virtual method will result in a compilation error unless the method is added to the NEW keyword to indicate how the method is intended to hide the parent class. Class N: D {public new void foo () {}} n n = new n (); n.foo (); // call N FOO ((D) N) .foo (); // Call D FOO ((b) n) .foo (); // Call D of the Foo and C , Java compared to the C # Override keyword makes it clear to see which methods can be clearly written when you read the source code. However, it is advantageous to use the imaginary method. First, avoid using the virtual method can slightly improve the running speed, followed, which method can be clearly understood. However, interest may be disadvantage. In contrast, the default ignore the final modifier in Java, and the Virtual modifier is ignored by default in C . The default options in Java make your program slightly lose some efficiency, while in C , it may hinder scalability, although this is unpredictable for the realizes of the base class. 12. Interfaces in Interface C # are similar in Java, but there is a greater flexibility. The class can be freely explicitly implemented an interface: public interface iteller {void next ();} public interface itrator {void next ();} public class clars: ney {itrator {void iteller.next () {} Void Iiterator. Next () {}} This has brought two benefits to the classes that implement the interface. First, a class can achieve several interfaces without having to worry about naming conflicts. Second, if a method is not used by the general user, the class can hide the method. The call to the explicit implementation needs to transform the object into the corresponding interface: Clark Clark = new clars (); (iTeller) CLARK .Next (); 13. Version Processing Solution The problem has become a .net framework Main considerations, most of these considerations are reflected in the assembly. In C #, different versions of the same assembly can be run in the same process, which gives people a deep impression. When the new version of the code (especially .NET library) is created, C # can prevent software from failing. This problem is described in detail in the C # language reference. I briefly explained the following: In Java, assuming that we deploy a class called D, it is derived from a class released by VM. The class D has a method called Foo, and it is not this method when it is released. Later, the upgrade of class B, and B also included a method called Foo, and the new VM is now installed on the machine using class D. Now, the software to use D may fail because the new implementation of class B may cause a virtual function call to D, which has implemented a class B begin to move the unlesseds [Translation: Because of Java] It is virtual]. In C #, the FOO method of class D should be declared that it is not necessary to use Override modifiers (this truly expressed programmers' will), therefore, runtime (Runtime knows how to let class D hide classes B's Foo method, and Not overwriting it. Here, the C # reference manual is interesting in a C # reference manual: "C # processing version issues, is achieved by requesting developers to clear their intentions." Although using Override is an expression of the intent, the compiler can also generate - by a overwriting in the execution (rather) in the compile period. This means that you can still have the same language like Java (Java does not need Virtual and Override keywords) and still handle version issues correctly. See the field modifier part. 14. Parameter modifier (1) Ref parameter modifier C # (compared to Java) allows you to pass parameters as reference. The most obvious example describing this is a general SWAP method. Unlike C , in C #, not only when the declaration, add the Ref indicator when calling: public class test {public static void main () {int a = 1; int b = 2; swap (Ref A, REF B); Naturally supplement. The REF modifier requires that the parameters must be assigned before the method, and the OUT modifier indicates that the parameter assignment is explicitly indicated when the method returns. (3) Params parameter modifier Params modifier can be added to the final parameters of the method, meaning the method will accept parameters of any number of specified types. For example: public class test {public static void main () {console.writeline (add (1, 2, 3, 4) .tostring ());} public static int address (params int address) {int sum = 0 Foreach (Int i in array) SUM = I; Return Sum;}} Author Note: When learning Java, a very surprised thing is that Java can't pass parameters as reference, although soon, you have little I don't need it when I think this function and write code. When I read the C # specification for the first time, I often think, "They do it, don't write this function, without it, I can write code." After reflection, I realized that this is not a problem that some functions useful, but more explains the problem that you can do if you need another elsewhere. When considering how C is doing, Java is a good thing, it simplifies how the parameters are delivered. In C , the parameters and method of the method calls through the pass value, reference, pointers make the code unnecessary complex. C # Explicitly transfer reference, whether it is a method declaration or when it is called. It greatly reduces the chaos [translation: more than one, in C , sometimes you don't know if you use an object or an object reference, this section has an example], and reaches the same goal with Java, but C # The way is more expressive. Obviously this is the main purpose of C # - it does not put the programmer circle in a circle, so that they must do something around a big bend. Remember Java? Java Guide Suggestions on how to solve pass references: You should pass an array with 1 element to save your value, or do another class to save this value. Translation: Class ParentCls {PUBLIC: Virtual Void f () {Printf ("ParentCls / T");}}; class childcls: public parentcls {publicclic: Virtual void f () {printf ("childcls / t");}} Void Test1 (ParentCls PC) {pc.f ();} void test2 (Parentcls & PC) {pc.f ();} int main (int Argc, char * argv []) {childcls cc; // only call At, we don't know if it is quoted or an object, but the results are different! TEST1 (CC); // Output ParentCls Test2 (CC); // Output Childcls Return 0;} 15. Attribute C # and Java Compile Codes include information similar to field access level. C # extends this capability, any element in the class, such as class, method, field or even independent parameters, you can compile custom information and get this information at runtime. An example of a very simple use of the class: [AuthorAttribute ("Ben Albahari")] Class A {[localizable (true)] public string text {get {return text;} // ...}} Java A pair of / ** * / and @ tag comments to include additional information for classes and methods, but this information (except @DepRecated [translation: Java1.1 and later]) is not Build to the Biode code. C # Using predefined features Obsolete features, the compiler can warn you, exclude waste code (就 @Deprecated), and use the Conditional feature to make conditional compilation. Microsoft's new XML library use features to express the fields to be serialized into XML, which means that you can easily put a class into XML and reconstruct it again. Another proper application of features is to create a truly powerful class browsing tool. The C # language specification explains how to create and use characteristics. 16. Switch statement in the Switch statement c # You can use integer, characters, enumerations, or strings (C and Java can't be). In Java and C , if you ignore a BREAK statement in any CASE statement, you have the danger of other CASE statements being performed. I don't think this rarely needs and easily erroneous behavior into the default behavior in Java and C , I am also very happy to see C # is not this. Translation: C # does not allow way to run through another CASE tag from a CASE tag. If you need this, you can use Goto Case or Goto Default implementation. 17. The predefined type C # Basic type is basically similar to Java, except for the former, no symbolic type. C # There is Sbyte, Byte, Short, Ushort, Int, Uint, Long, Ulong, Char, Float, and Double. The only place is that there is a 16-byte numeric type Decimal that has a 16-byte [translation: original text misunderstood 12 bytes], which can take advantage of the latest processor. Translation: Supplement, although Decimal takes 128 bits, but its range is much smaller than float (32-bit), Double (64-bit) is much smaller, and its accuracy is much higher than the two, which can meet the accuracy requirements Very high financial calculation, etc. 18. Field modifier C # in field modifiers basically java the same. In order to indicate a field that cannot be modified, C # uses const and readonly modifiers. Const field modifier is like a Final field modifier with Java, which is compiled into a part of the IL code. The read-only field calculates the value at runtime. This allows upgrades without destroying the code you have deployed. 19. The jump statement may not be more surprising from more surprising, except for the inexperienced goto statement. However, this is very different from the more than 20 years ago 20 years ago. A goto statement must point to a label [Demolition: GOTO statement must be within the scope of the label. Only the GOTO statement is allowed to transfer control to a nested scope without passing control to a nested domain] or a selection branch in the Switch statement [Translation:, the so-called Goto Case statement]. The use of the label is similar to the continue. The label freedom in Java is large. [Translation] After the Break and Continue statements in Java, you can follow the label]. In C #, the GOTO statement can point to any of its scope, this scope is the same method or finally block [Demo: If the goto statement appears in the finally statement, the destination of the GOTO statement must also be In the same finally statement block]. The CONUE statement in C # and the basic equivalent in Java, but cannot point to a label in C #. Translation: Java uses GOTO as a reserved word, but does not implement it. 20. Access, namespaces, and access levels in C #, you can organize components (clauses, structures, entrustries, etc.) in your source code to files, namespaces, and program sets. The name space is just a small honey sugar in the long-class syntax. For example, you don't need this to write genamics.winforms.grid, you can declare the class grid and contain it: namespace genamics.winforms {public class grid {// ....}} For classes using Grid, you can use Using Keyword Import [Transplement Note: Using Genamics.winForms] without having to use its full class name genamics.Grid. The assembly is an EXE or DLL compiled from the project file. The .NET runtime uses configurable features and version rules to create them to the assembly, which greatly simplifies deployment: No need to write a registry, just copy the program to the relevant directory. The assembly can also form a type boundary to solve the problem of class conflicts. Multiple versions of the same assembly can coexist in the same process. Each file can contain multiple classes, multiple namespaces. A namespace can span several assemblies. In this way, the system will achieve greater freedom. There are five access levels in C #: Private, Internal, Protected, Internal protected and public. Private and public and Java mei. In C #, there is no indication level of Private instead of package (package). INTERNAL Access is confined in the program set instead of name space (this and Java more similar). INTERNAL protected is equivalent to Java protected. Protected is equivalent to Java's Private Protected, the latter has been discarded by Java. 21. The pointer operation is in C #, which can be used in a method that is marked as a UNSAFE modifier. When the pointer points to an object that can be collected by the garbage, the compiler force you to secure the object using the Fixed keyword because the garbage collector is recycling memory by moving objects. If you use the original pointer just when you use the original pointer, the object it refers to is moved, then your pointer will point to garbage. I think this keyword with unsafe is a good choice - it does not encourage developers to use pointers unless they really want to do this. 22. Multidimensional array C # can create an error array [translation note: Interleave array is an array of elements as arrays, and its elements can be different] and multi-dimensional arrays. Arrange arrays and Java arrays are very similar. The multi-dimensional array makes it more efficient and more accurately expressed. The following is an example of this array: int [,,] array = new int [3, 4, 5]; // Create an array int [1, 1, 1] = 5; // Translation: This line of code is Mistaken, should be array [1,1,1] = 5; use interlace array: int [] [] array = new int [3] [4] [5]; // Translation: This line code is incorrect, Be: int [] [] [] array = new int [3] [] []; int [1] [1] [1] = 5; // Translation: This line code is incorrect, it should be array [1 [1] [1] = 5; If the structure is used in combination, the high efficiency provided by C # makes an array a good choice for graphics and mathematics. 23. Constructor and Destructor You can specify an optional constructor parameter: Class test {public test (): this (0, null) {} public test (int x, object o) {}} You can also specify Static constructor: Class test {static int [] ascendingArray = new int [=; static test () {for (int i = 0; i If you are accustomed to adopting certainty-based programming mode (you know when you are called), then you must adapt to this different programming model when you transfer to Java or C #. Microsoft is recommended and implemented, running through the entire .NET framework is Dipose mode. You want to define a Dispose () method for classes that need to manage external resources such as graphic handle or database. For distributed programming, the .NET framework provides an agreed basic model to improve the reference count of DCOM. 24. The hosting environment is not avoidable to [C # / IL code / CLR] and [Java / byte / JVM]. I think the best way is to first figure out why these technologies come. Write programs with C and C , which is generally compiled into a assembly language code [Translation note: should be machine code], which can only run on a specific processor and a specific operating system. The compiler needs to know the target processor because the different processor instruction sets are different. The compiler also knows the target operating system, because different operating systems are different about how to perform work and how to implement basic C / C concepts such as memory allocation. C / C This model has achieved huge success (most of the software you use may be compiled), but there is also its limitations: there is no export of programs to interact with other procedures (Microsoft COM is for Overcoming this restrictions) The program cannot be distributed in a cross-platform form that cannot be restricted in a safe operation in a safe operation in order to solve these problems. In the virtual machine. The basic structure of the bytecode maintained before being compiled. This makes the Java programs and other programs to make a variety of interactions. The bytecode is also machine neutral, which means that the same Class file can run in different platforms. Finally, the fact that Java languages has no explicit memory operation (through pointers) makes it very suitable for writing "Sandbox Programs". The initial virtual machine uses the interpreter to convert the byte code instruction stream into machine code, but this process is slow, so that the programmer who focuses on performance has never been attractive. Today, most JVMs use the JIT compiler to basically compile into machine code before and before the formation frame range. Before it runs, you can convert Java programs to assembly languages to avoid startup time and instant compiled memory burden. This process does not need to remove the runtime dependence compared to compiling Visual C programs. Java runtime (this term is hidden under the term Java virtual machine) will handle many critical aspects of the program run, such as garbage collection and security management. It is also considered to be a managed operating environment at runtime. Although the terminology is ambiguous, despite the unused interpreter, the .NET basic model is also described above. The important improvement in .Net will come from the improvement of IL itself. Java can match the only way to modify the bytecode specification to achieve strict compatibility. I don't want to discuss these improvements, which should be left to those developers who know both byte size and understand the IL code. 99% of developers like me are not intended to study the IL code specification, which lists some IL design decisions that intend to improve bytecodes: Providing a better type of neutral (help to implement template) to provide better language to compile language, never explain additional declarative information to classes, methods, and the like. See 15. At present, the CLR provides multi-operating system support, providing better interoperability support for JVM in other areas. See 26. Interoperability. 25. If the library has no library, the language is not used. C # is known as no core library, but it uses the .NET framework library (some of them are created with C #). This article focuses on the specialty of the C # language, not .NET - That should be an illustration. Simply put, .NET library includes rich threads, collection, XML, ADO , ASP , GDI , and Winform libraries [translation: now these multiple have become .NET]. Some libraries are cross-platform, and some depend on Windows, please read the discussion on the platform support. 26. Interoperability I think it is more appropriate to divide interoperate into three parts: language interoperability, platform interoperability, and standard interoperability. Java is longer than platform interoperability, C # is longer than language interoperability. In terms of standard interoperability, both have a long short. (1) Language interoperability and other language integration capabilities are only different from integration and difficulty. JVM and CLRs allow you to write code in multiple languages, just compile them into one or IL code. However, .NET platform has a lot of work - not only to compile other language code into an IL code, which also makes multiple languages to share and expand each other libraries. For example, Eiffel or Visual Basic programmers can import C # classes, override their virtual methods; C # objects can also use Visual Basic Method (Polymorphism). If you suspect, VB.NET has been upgraded, which has a modern object-oriented characteristic (of course, the loss of VB6 compatibility). Third-Party .NET language can usually be inserted into the Visual Studio.NET environment. If necessary, the same RAD framework can be used. This overcomes the impression of "second-class citizens" in other languages. C # provides a P / Invoke [Translation: Platform Invocation Service, Platform Call Service], which is much simpler than Java's JNI and C code interaction (no DLL). This feature is very like J / Direct, the latter is a characteristic of Microsoft Visual J . (2) Platform interoperability is generally, which means operating system interoperability, but in the past few years, IE itself is more and more like a platform. The C # code is running in a hosted operation environment. This is an important step that makes C # can run over different operating systems. However, some .NET libraries are Windows based, especially WinForms libraries, which depends on how much woors' Windows API. There is a transfer from the Windows API to the UNIX system, but there is no startup yet, and Microsoft has not clearly implied to do this. However, Microsoft did not ignore platform interoperability. The .NET library provides extension capabilities to write HTML / DHTML solutions. For clients that can be implemented with HTML / DHTML, C # /. Net is a good choice. For projects that require more complex customer interfaces across platforms, Java is a good choice. Kylix - a version of Delphi, allowing the same code to be able to compile on Windows, perhaps a good choice for cross-platform solutions in the future. (3) Standard interoperability almost all standards, such as database systems, graphics libraries, Internet protocols, and object communication standards such as COM and CORBA, can be accessed via C # access.