Contents - Cultural View

Contents - Cultural View Contents - Cultural View

culturalview.com
from culturalview.com More from this publisher
14.07.2013 Views

Comparison of Java and C Sharp 66 size="7.15"> define a delegate terface SomeDelegate { boolean Invoke(String arg); a target class ass Target { public boolean TargetMethod(String arg) { // do something return true; } usage id DoSomething() { > // construct a target with the target method final Target target = new Target(); // capture the delegate for later invocation SomeDelegate dlg = new SomeDelegate() { public boolean Invoke(String arg) { return target.TargetMethod(arg); } }; // invoke the delegate boolean result = dlg.Invoke("argumentstring"); Lifted (nullable) types // define a delegate private delegate bool SomeDelegate(string resou } // a target class class Target { public bool TargetMethod(string arg) { // do something return true; } } // usage void DoSomething() { // construct a target with the target metho var target = new Target(); // capture the delegate for later invocatio SomeDelegate dlg = target.TargetMethod; // invoke the delegate bool result = dlg("argumentstring"); C# allows value/primitive/simple types to be "lifted" to allow the special null value in addition to the type's native values. A type is lifted by adding a ? suffix to the type name. Conversions are implicitly defined to convert between values of the base and the lifted type. The lifted type can be compared against null or it can be tested for HasValue. Also, lifted operators are implicitly and automatically defined based on their non-lifted base, where - with the exception of some boolean operators - a null argument will propagate to the result. Java does not support type lifting as a concept, but the fixed set of built-in primitive types all have corresponding (boxed) wrapper types which do support the null value by virtue of being reference types. However, Java operators such as + are not defined for these companion wrapper types. Instead Java will fall back to the primitive operator and will attempt to unbox the operands before invocation. If one or both of the operands are null, this unboxing will cause a NullPointerException to be thrown. The following example illustrates how the lifted C# operator propagates the null value of the operand where the corresponding code in Java will throw an exception. Java C#

e="6.73"> = 42; = null; Comparison of Java and C Sharp 67 int? a = 42; int? b = null; ill generate a runtime NullPointerException // c will receive the null value e the * operator will attempt to unbox the null value // because * is lifted and one of the operands = a * b; int? c = a * b; Not all C# lifted operators have been defined to propagate null unconditionally if one of the operands is null. Specifically, the boolean operators have been lifted to support ternary logic thus keeping impedance with SQL. Late-bound (dynamic) type C# features a late bound dynamic type which supports no-reflection dynamic invocation, interoperability with dynamic languages as well as ad-hoc binding to (for example) document object models. The dynamic type resolves member access dynamically at runtime as opposed to statically/virtual a compile time. The member lookup mechanism is extensible with traditional reflection as a fall-back mechanism. There are several use cases for the dynamic type in C#: • Less verbose use of reflection: By casting an instance to the dynamic type, members such as properties, methods, events etc. can be directly invoked on the instance without using the reflection API directly. • Interoperability with dynamic languages: The dynamic type comes with a hub-and-spoke support for implementing dynamically typed objects and common runtime infrastructure for efficient member lookup. • Creating dynamic abstractions on the fly: For instance, a dynamic object could provide simpler access to document object models such as XML- or XHTML documents. Java does not support a late-bound type. The use cases for C# dynamic type have different corresponding constructs in Java: • For dynamic late-bound by-name invocation of preexisting types, reflection should be used. • For interoperability with dynamic languages, some form of interoperability API specific to that language will have to be used. The Java Virtual Machine platform does have multiple dynamic languages implemented on top if it, but there is no common standard for how to pass objects between languages. Usually this will involve some form of reflection or reflection-like API. As an example of how to use JavaFX objects from Java, see How to Use JavaFX in Your Swing Application [1] . • For creating and interacting with objects entirely at runtime, e.g. interaction with a document object model abstraction, a specific abstraction API will have to be used. Pointers C# allows use of pointers and corresponding pointer arithmetic. Pointers and pointer-arithmetic are potentially unsafe in a managed environment as they can be used to bypass the strict rules for object access. C# addresses that concern by requiring that code blocks or methods that use the feature be marked with the unsafe keyword, so that all clients of such code can be aware that the code may be less secure than otherwise. The compiler requires the /unsafe switch to allow compilation of a program that uses such code, and assemblies containing unsafe code may only execute if explicitly granted security permissions. Generally, unsafe code is either used to allow better interoperability with unmanaged APIs or system calls (which are inherently "unsafe"), or for performance reasons. Java does not permit pointers or pointer-arithmetic within the Java runtime environment and native interop is handled externally through JNI or other mechanisms.

e="6.73"><br />

= 42;<br />

= null;<br />

Comparison of Java and C Sharp 67<br />

<br />

int? a = 42;<br />

int? b = null;<br />

ill generate a runtime NullPointerException<br />

// c will receive the null value<br />

e the * operator will attempt to unbox the null value // because * is lifted and one of the operands<br />

= a * b; <br />

int? c = a * b;<br />

<br />

Not all C# lifted operators have been defined to propagate null unconditionally if one of the operands is null.<br />

Specifically, the boolean operators have been lifted to support ternary logic thus keeping impedance with SQL.<br />

Late-bound (dynamic) type<br />

C# features a late bound dynamic type which supports no-reflection dynamic invocation, interoperability with<br />

dynamic languages as well as ad-hoc binding to (for example) document object models. The dynamic type resolves<br />

member access dynamically at runtime as opposed to statically/virtual a compile time. The member lookup<br />

mechanism is extensible with traditional reflection as a fall-back mechanism.<br />

There are several use cases for the dynamic type in C#:<br />

• Less verbose use of reflection: By casting an instance to the dynamic type, members such as properties, methods,<br />

events etc. can be directly invoked on the instance without using the reflection API directly.<br />

• Interoperability with dynamic languages: The dynamic type comes with a hub-and-spoke support for<br />

implementing dynamically typed objects and common runtime infrastructure for efficient member lookup.<br />

• Creating dynamic abstractions on the fly: For instance, a dynamic object could provide simpler access to<br />

document object models such as XML- or XHTML documents.<br />

Java does not support a late-bound type. The use cases for C# dynamic type have different corresponding constructs<br />

in Java:<br />

• For dynamic late-bound by-name invocation of preexisting types, reflection should be used.<br />

• For interoperability with dynamic languages, some form of interoperability API specific to that language will<br />

have to be used. The Java Virtual Machine platform does have multiple dynamic languages implemented on top if<br />

it, but there is no common standard for how to pass objects between languages. Usually this will involve some<br />

form of reflection or reflection-like API. As an example of how to use JavaFX objects from Java, see How to Use<br />

JavaFX in Your Swing Application [1] .<br />

• For creating and interacting with objects entirely at runtime, e.g. interaction with a document object model<br />

abstraction, a specific abstraction API will have to be used.<br />

Pointers<br />

C# allows use of pointers and corresponding pointer arithmetic. Pointers and pointer-arithmetic are potentially<br />

unsafe in a managed environment as they can be used to bypass the strict rules for object access. C# addresses that<br />

concern by requiring that code blocks or methods that use the feature be marked with the unsafe keyword, so that all<br />

clients of such code can be aware that the code may be less secure than otherwise. The compiler requires the /unsafe<br />

switch to allow compilation of a program that uses such code, and assemblies containing unsafe code may only<br />

execute if explicitly granted security permissions. Generally, unsafe code is either used to allow better<br />

interoperability with unmanaged APIs or system calls (which are inherently "unsafe"), or for performance reasons.<br />

Java does not permit pointers or pointer-arithmetic within the Java runtime environment and native interop is<br />

handled externally through JNI or other mechanisms.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!