Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Comparison of C Sharp and Java
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Traditional way==== <syntaxhighlight lang="java" style="font-size:90%"> // Initialize the engine var invocable = new ScriptEngineManager().getEngineByName("jruby"); var rubyFile = new FileReader("Deepthought.rb"); engine.eval(fr); </syntaxhighlight> <syntaxhighlight lang="java" style="font-size:90%"> // create a new instance of "Deepthought" calculator var calcClass = engine.eval("Deepthought"); var calc = invocable.invokeMethod(calcClass, "new"); // set calculator input values invocable.invokeMethod(calc, "a=", 6); invocable.invokeMethod(calc, "b=", 7); // calculate the result var answer = invocable.invokeMethod(calc, "Calculate"); </syntaxhighlight> |valign=top|<syntaxhighlight lang="csharp" style="font-size:90%"> // Initialize the engine var runtime = ScriptRuntime.CreateFromConfiguration(); dynamic globals = runtime.Globals; runtime.ExecuteFile("Deepthought.rb"); </syntaxhighlight> <syntaxhighlight lang="csharp" style="font-size:90%"> // create a new instance of "Deepthought" calculator var calc = globals.Deepthought.@new(); // set calculator input values calc.a = 6; calc.b = 7; // calculate the result var answer = calc.Calculate(); </syntaxhighlight> |- valign=top | Notes for the Java implementation: * Ruby accessors names are generated from the attribute name with a {{code|{{=}}}} suffix. When assigning values, Java developers must use the Ruby accessor method name. * Dynamic objects from a foreign language are not first-class objects in that they must be manipulated through an API. | Notes for the C# implementation: * Objects returned from properties or methods of {{mono|dynamic}} objects are themselves of {{mono|dynamic}} type. When type inference (the {{mono|var}} keyword) is used, the variables calc and answer are inferred dynamic/late-bound. * Dynamic, late-bounds objects are first-class citizens that can be manipulated using C# syntax even though they have been created by an external language. *{{mono|new}} is a reserved word. The {{code|@}} prefix allows keywords to be used as identifiers. |}
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)