Contents - Cultural View
Contents - Cultural View Contents - Cultural View
Comparison of Java and C Sharp 76 extended with a protected/public method whose signature is already in use by a derived class. Generator methods Any C# method declared as returning IEnumerable, IEnumerator and/or the generic versions of these interfaces can be implemented using yield syntax. This is a form of limited, compiler-generated continuations and can drastically reduce the code required to traverse or generate sequences, although that code is just generated by the compiler instead. The feature can also be used to implement infinite sequences, e.g. the sequence of Fibonacci numbers. Below is an example of a C# generator method that takes an enumerable input (possibly an array of ints) and yields the even numbers. The method will not iterate through all items on the invocation; rather it will return an iterator which will pull items from numbers only on demand. public static IEnumerable GetEven(IEnumerable numbers) { } foreach(int i in numbers) if (i % 2 == 0) yield return i; In Java, generators can be defined only using (possibly anonymous) classes, requiring more boilerplate code. Explicit interface implementation In either language if a method (or property in C#) is specified with the same name and signature in multiple interfaces, the members will clash when a class is designed which implements those interfaces. An implementation will by default implement a common method for all of the interfaces. If separate implementations are required (because the methods really do serve separate purposes, or because return values differ between the interfaces) C#'s explicit interface implementation will solve the problem, though allowing different results for the same method, depending on the current cast of the object. In Java there is no way to solve this problem other than cleaning the code and refactoring one or more of the interfaces to avoid name clashes. Closures A closure is an inline function which captures variables from its lexical scope. [14] [15] C# supports closures as anonymous methods or lambda expressions with full-featured closure semantics. In Java, anonymous inner classes remains the preferred way to emulate closures. Aside from being a more verbose construction, this approach also has some drawbacks compared to real closures, such as limited access to variables from the enclosing scopes (only final members can be referenced). When a reference to a method can be passed around for later execution, a problem arises about what to do when the method has references to variables/parameters in its lexical scope. C# closures can fully capture any variable/parameter from its lexical scope. In Java's anonymous inner classes only references to final members of the lexical scope are allowed, thus requiring the developer to artificially introduce extra levels of indirections and boxing primitive types if he wants to reference and update those from the inner class. While Java does not currently feature closures, it has been announced that some form of closures will be included in JDK 7. [16]
Comparison of Java and C Sharp 77 Language integrated query C#s Language INtegrated Query (LINQ) is not really a single feature; rather it is a number of features designed to work together to allow for in-language querying capabilities. LINQ has emerged as one of the most distinguishing features between Java and C#. LINQ consists of the following features: • Extension methods allow existing interfaces/classes to be extended with new methods. Implementations can be shared or an interface can have a dedicated implementation. • Lambdas allow for expression of criteria in a functional fashion. • Expression trees allow a specific implementation to capture a lambda as a concrete syntax tree rather than an executable block. This can be leveraged by implementations to represent criteria in a different language, e.g. in the form of a SQL where clause as is the case with e.g. Linq#LINQ_to_SQL • Anonymous types and type inference supports capturing and working with the result type of a query. A query may both join and project over query sources which may lead to a result type which cannot be named. • Query expressions to support a syntax familiar to SQL. • Nullable (lifted) types to allow for better match with query providers which support nullable types, like e.g. SQL. Lambdas and expression trees C# features a special type of in-line closures called lambdas. These are anonymous methods: they have a signature and a body, but no name. They are not methods in that they cannot form part of a class interface. They are mainly used to specify local function-valued arguments in calls to other methods, a technique mainly associated with functional programming. On top of that, lambda functions can double as a way to define special data structures called expression trees. Whether they are seen as an executable function or a data structure depends on compiler type inference and what type of variable or parameter they are assigned/cast to. Lambdas and expression trees play key roles in LINQ. Java does not feature lambdas or expression trees; its primary mechanism for inline scope capture and method definition is the anonymous inner class syntax. Partial methods Related to partial classes C# allows partial methods to be specified within partial classes. A partial method is an intentional declaration of a method with a number of restrictions on the signature. These restrictions ensure that if a definition is not actually provided by any class part, then the method and every call to it can be safely erased. This feature allows code to provide a large number of interception points (like the template method GoF design pattern) without paying any runtime overhead if these extension points are not being used by another class part at compile time. Java has no corresponding concept. Extension methods Using a special this designator on the first parameter of a method, C# allows the method to act as if it were a member method of the type of the first parameter. This extension of the foreign class is purely syntactical. The extension method needs to be static and defined within a purely static class. It must obey any restriction on such external static methods and thus cannot break object encapsulation. The "extension" is only active within scopes where the namespace of the static host class has been imported. Java has no corresponding concept.
- Page 29 and 30: Apache Harmony 25 Sun answered on a
- Page 31 and 32: Apache Harmony 27 Component Descrip
- Page 33 and 34: Apache Harmony 29 [10] Gilbert, Dav
- Page 35 and 36: Apache Shiro 31 Apache Shiro Develo
- Page 37 and 38: Java applet 33 there are frequently
- Page 39 and 40: Java applet 35 } } g.drawString("He
- Page 41 and 42: Java applet 37 The 2002 Sun - Micro
- Page 43 and 44: Java applet 39 [18] W3.org (http:/
- Page 45 and 46: Associació d'Usuaris de Java de Ca
- Page 47 and 48: Associació d'Usuaris de Java de Ca
- Page 49 and 50: Associació d'Usuaris de Java de Ca
- Page 51 and 52: Associació d'Usuaris de Java de Ca
- Page 53 and 54: Bean Scripting Framework 49 Bean Sc
- Page 55 and 56: CJAN 51 Ostensibly, CJAN.org failed
- Page 57 and 58: Chainsaw (log file viewer) 53 Chain
- Page 59 and 60: clone (Java method) 55 Alternatives
- Page 61 and 62: clone (Java method) 57 } } abstract
- Page 63 and 64: Comparison of Java and C Sharp 59 A
- Page 65 and 66: Comparison of Java and C Sharp 61 R
- Page 67 and 68: Comparison of Java and C Sharp 63 T
- Page 69 and 70: Comparison of Java and C Sharp 65 f
- Page 71 and 72: e="6.73"> = 42; = null; Comparison
- Page 73 and 74: Comparison of Java and C Sharp 69 L
- Page 75 and 76: Comparison of Java and C Sharp 71 I
- Page 77 and 78: Comparison of Java and C Sharp 73 t
- Page 79: Comparison of Java and C Sharp 75 N
- Page 83 and 84: Comparison of Java and C Sharp 79 T
- Page 85 and 86: Comparison of Java and C Sharp 81 C
- Page 87 and 88: Comparison of Java and C++ 83 C++ J
- Page 89 and 90: Comparison of Java and C++ 85 e="7.
- Page 91 and 92: Comparison of Java and C++ 87 the c
- Page 93 and 94: Comparison of Java and C++ 89 • D
- Page 95 and 96: Comparison of Java and C++ 91 Sever
- Page 97 and 98: Comparison of the Java and .NET pla
- Page 99 and 100: Comparison of the Java and .NET pla
- Page 101 and 102: Comparison of the Java and .NET pla
- Page 103 and 104: Comparison of the Java and .NET pla
- Page 105 and 106: Constant interface 101 } public sta
- Page 107 and 108: Java 4K Game Programming Contest 10
- Page 109 and 110: Java 4K Game Programming Contest 10
- Page 111 and 112: CookXml 107 Creators, setters, adde
- Page 113 and 114: Cougaar 109 Cougaar Cougaar is a Ja
- Page 115 and 116: Devoxx 111 Related links • Devoxx
- Page 117 and 118: Elastic Path 113 References [1] htt
- Page 119 and 120: Electronic Disturbance Theater 115
- Page 121 and 122: Event dispatching thread 117 • Sw
- Page 123 and 124: Facelets 119 The above code can be
- Page 125 and 126: final (Java) 121 } Sphere(double x,
- Page 127 and 128: FormEngine 123 FormEngine Developer
- Page 129 and 130: Generics in Java 125 Motivation for
Comparison of Java and C Sharp 76<br />
extended with a protected/public method whose signature is already in use by a derived class.<br />
Generator methods<br />
Any C# method declared as returning IEnumerable, IEnumerator and/or the generic versions of these interfaces can<br />
be implemented using yield syntax. This is a form of limited, compiler-generated continuations and can drastically<br />
reduce the code required to traverse or generate sequences, although that code is just generated by the compiler<br />
instead. The feature can also be used to implement infinite sequences, e.g. the sequence of Fibonacci numbers.<br />
Below is an example of a C# generator method that takes an enumerable input (possibly an array of ints) and yields<br />
the even numbers. The method will not iterate through all items on the invocation; rather it will return an iterator<br />
which will pull items from numbers only on demand.<br />
public static IEnumerable GetEven(IEnumerable numbers) {<br />
}<br />
foreach(int i in numbers) if (i % 2 == 0) yield return i;<br />
In Java, generators can be defined only using (possibly anonymous) classes, requiring more boilerplate code.<br />
Explicit interface implementation<br />
In either language if a method (or property in C#) is specified with the same name and signature in multiple<br />
interfaces, the members will clash when a class is designed which implements those interfaces. An implementation<br />
will by default implement a common method for all of the interfaces. If separate implementations are required<br />
(because the methods really do serve separate purposes, or because return values differ between the interfaces) C#'s<br />
explicit interface implementation will solve the problem, though allowing different results for the same method,<br />
depending on the current cast of the object. In Java there is no way to solve this problem other than cleaning the code<br />
and refactoring one or more of the interfaces to avoid name clashes.<br />
Closures<br />
A closure is an inline function which captures variables from its lexical scope.<br />
[14] [15]<br />
C# supports closures as anonymous methods or lambda expressions with full-featured closure semantics.<br />
In Java, anonymous inner classes remains the preferred way to emulate closures. Aside from being a more verbose<br />
construction, this approach also has some drawbacks compared to real closures, such as limited access to variables<br />
from the enclosing scopes (only final members can be referenced).<br />
When a reference to a method can be passed around for later execution, a problem arises about what to do when the<br />
method has references to variables/parameters in its lexical scope. C# closures can fully capture any<br />
variable/parameter from its lexical scope. In Java's anonymous inner classes only references to final members of the<br />
lexical scope are allowed, thus requiring the developer to artificially introduce extra levels of indirections and boxing<br />
primitive types if he wants to reference and update those from the inner class.<br />
While Java does not currently feature closures, it has been announced that some form of closures will be included in<br />
JDK 7. [16]