2. A Broad Overview
.NET Platform JVM Platform
IL
Intermediate
Language
Bytecode
C# F# VB.NET Java Groovy Scala
CLR
(Common Language Runtime)
Win Win Mac UnixLinux
csc javac
groovyc
fsc
vbc
scalac
JRE
(Java Runtime Environment)
Mono
3. Technology Stack
Java C#
Data Access
Client Side GUI
Web Side GUI
Web Scripting
Web Hosting
Remote Invocation
Messaging
Native
Directory Access
Componentization
JDBC ADO.NET
AWT/Swing WinForms, WPF
JSP, JavaFX, JSF ASP.NET, WebForms
Servlets, Filters
ISAPI, HttpHandler,
HttpModule
Tomcat, Jetty, Weblogic
etc...
IIS
RMI, Netty, AKKA Remoting, now part of WCF
JMS, AKKA MSMQ
JNI PInvoke
JNDI Active Directory (Ldap/ADSI)
EJB (Entity/Session), Spring
COM+, Managed Extensibility
Framework (MEF)
4. Language Comparison
Java C#
Program Entry Point
Namespace
Including Classes
Inheritance
Overriding
Accessing Parent Ctor
Accessing Parent Method
Visibility
main(String ...args)
Main() or
Main(string [] args)
package namespace
import using
class (extends),
interface (implements)
class and interface (:)
virtual by default
non-virtual by default
use virtual keyword
super(...) : base(...)
super.method(...) base.Method(...)
private, package,
protected, public
private, protected, internal,
internal protected, public
5. Language Comparison
Java C#
Abstract Class
Non-Extensible Class
Non-Writable Field
Non-Extensible Method
Constant
Checking Instance Type
Enums
for-each construct
Switch-Case
abstract class X { ... } abstract class X { ... }
final class X { ... } sealed class X { ... }
final readonly
final sealed
static final const
instanceof is
enum, can have fields, methods and
implement interfaces and are typesafe
enum, cannot have methods,
fields or implement
interfaces, not typesafe
for (item : collection)
{ ... }
foreach(item in collection)
{ ... }
numeric types (int,
float...) enums, and now
strings (in Java 7)
numeric types, enums and
strings
6. Language Comparison
Java C#
Method Parameters
Variable Arguments
Exceptions
ADT Meta Type
Meta Information
Static class
Properties
Non-Deterministic Object
Cleanup
Object References are
passed by Value only
Object reference are passed
by Value(default), ref & out
method(type... args) Method(params type[] args)
Checked and Unchecked
(enforced by javac, not by JIT
compiler)
All Unchecked Exceptions
Class
Class klass = X.class;
Type
Type type = typeof(X);
@Annotation [Attribute]
Simulated by private Ctor
and static methods
Static class and ctor with
static methods
getProperty(),
setProperty()
Property { get; set; }
compiler generated
get_Property() and
set_Property() methods
finalize() destructor ~X()
7. Language Comparison
Java C#
Deterministic Object Cleanup
Generics
Class Loading
synchronized block
synchronized method
Thread Local Storage
Smallest Deployment Unit
Signing
AutoCloseable or Closeable
try-with-resources (Java 7)
try ( ... ) { ... }
IDisposable
using ( ... ) { ... }
<T>, <T extends Type>, <?>
Type Erasure
<T>, where T : type, new()
Preserves Type Info
Class.forName(“fqn”)
ClassLoader.getResources()
Activator.CreateInstance<T>()
Assembly.Load()
synchronized (this) { ... } lock (this) { ... }
synchronized method()
{ ... }
[MethodImpl(MethodImplOptions.Synchronized)]
void Method() { ... }
Thread relative static fields
Thread relative static fields
[ThreadStatic] and Data Slots
Jar
EXE/DLL
Private Assembly, Shared Assembly
Jar Signing Assembly Signing
9. Value Type
or
Ref Type?
Value Type
use ==
Reference Type use
ReferenceEquals
Specific to C#: Equality
• ForValue Types, default Equals implementation uses
reflection on fields.
• Override Equals for Reference Types
• Use the same strategy forValue Types, it is more
performant and consistent as well.
10. Equality Similarities
• Define Equals(object other) and hashCode as a
part of the contract
• Define both in terms of immutable fields
• It should be
• Reflexive: x.Equals(x) => True
• Symmetric: if x.Equals(y) then y.Equals(x) => True
• Transitive: x.Equals(y) and y.Equals(z), then x.Equals(z) =>
True
11. Specific to C#: Equality
• Use Exact object argument - Equals for use in
Collections and for performance.
• IEquatable<T>
• PreserveValue Semantics forValue Types
• Overload == operator
17. Specific to C#: Explicit
Interfaces01. interface Greet1 // v1.0
02. {
03. string Greet();
04. }
05. interface Greet2 //v2.0
06. {
07. string Greet();
08. }
09. public class Greeter : Greet1, Greet2
10. {
11. //Explicit Implementations
12. string Greet1.Greet() //Note the Visibility here
13. {
14. return “Hello from 1”;
15. }
16. string Greet2.Greet() //public not allowed for explicit
17. {
18. return “Hello from 2”;
19. }
20. public static void Main()
21. {
22. Greet1 greeter1 = new Greeter();
23. greeter1.Greet(); // Hello from 1
24. Greet2 greeter2 = new Greeter();
25. greeter2.Greet(); // Hello from 2
26. Greeter greeter = new Greeter();
27. greeter. // No Greeters to Greet unless I cast
28. }
29. }
18. Specific to C#: Explicit
Interfaces01. interface Greet1 // v1.0
02. {
03. string Greet();
04. }
05. interface Greet2 : Greet1 // v2.0
06. {
07. new string Greet();
08. }
09. public class Greeter : Greet2
10. {
11. //Explicit Implementations
12. string Greet1.Greet() //Note the Visibility here
13. {
14. return “Hello from 1”;
15. }
16. string Greet2.Greet() //public not allowed for explicit
17. {
18. return “Hello from 2”;
19. }
20. public static void Main()
21. {
22. Greet1 greeter1 = new Greeter();
23. greeter1.Greet(); // Hello from 1
24. Greet2 greeter2 = new Greeter();
25. greeter2.Greet(); // Hello from 2
26. Greeter greeter = new Greeter();
27. greeter. // No Greeters to Greet unless I cast
28. }
29. }
01. interface Greet1 // v1.0
02. {
03. string Greet();
04. }
05. interface Greet2 //v2.0
06. {
07. string Greet();
08. }
09. public class Greeter : Greet1, Greet2
10. {
11. //Explicit Implementations
12. string Greet1.Greet() //Note the Visibility here
13. {
14. return “Hello from 1”;
15. }
16. string Greet2.Greet() //public not allowed for explicit
17. {
18. return “Hello from 2”;
19. }
20. public static void Main()
21. {
22. Greet1 greeter1 = new Greeter();
23. greeter1.Greet(); // Hello from 1
24. Greet2 greeter2 = new Greeter();
25. greeter2.Greet(); // Hello from 2
26. Greeter greeter = new Greeter();
27. greeter. // No Greeters to Greet unless I cast
28. }
29. }
19. Specific to C#: Explicit
Interfaces01. interface Greet1 // v1.0
02. {
03. string Greet();
04. }
05. interface Greet2 : Greet1 // v2.0
06. {
07. new string Greet();
08. }
09. public class Greeter : Greet2
10. {
11. //Explicit Implementations
12. string Greet1.Greet() //Note the Visibility here
13. {
14. return “Hello from 1”;
15. }
16. string Greet2.Greet() //public not allowed for explicit
17. {
18. return “Hello from 2”;
19. }
20. public static void Main()
21. {
22. Greet1 greeter1 = new Greeter();
23. greeter1.Greet(); // Hello from 1
24. Greet2 greeter2 = new Greeter();
25. greeter2.Greet(); // Hello from 2
26. Greeter greeter = new Greeter();
27. greeter. // No Greeters to Greet unless I cast
28. }
29. }
01. interface Greet1 // v1.0
02. {
03. string Greet();
04. }
05. interface Greet2 //v2.0
06. {
07. string Greet();
08. }
09. public class Greeter : Greet1, Greet2
10. {
11. //Explicit Implementations
12. string Greet1.Greet() //Note the Visibility here
13. {
14. return “Hello from 1”;
15. }
16. string Greet2.Greet() //public not allowed for explicit
17. {
18. return “Hello from 2”;
19. }
20. public static void Main()
21. {
22. Greet1 greeter1 = new Greeter();
23. greeter1.Greet(); // Hello from 1
24. Greet2 greeter2 = new Greeter();
25. greeter2.Greet(); // Hello from 2
26. Greeter greeter = new Greeter();
27. greeter. // No Greeters to Greet unless I cast
28. }
29. }
Explicitly State the interface for which the
implementation is
20. Similarities
• Immutable Strings
• Serialization
• Boxing
• ConvertValue Type to a Reference Type
• Unboxing
• Convert Reference Type to aValue Type
23. Specific to C#
• Collection Initializer
• Coroutines (more precisely Generators)
• yield break
• yield return
01. class Fibonacci : IEnumerable<int> {
02. private readonly int howMany;
03. private int firstSeed, secondSeed = 1;
04.
05. public Fibonacci(int howMany)
06. {
07. this.howMany = howMany;
08. }
09.
10. public IEnumerator<int> GetEnumerator()
11. {
12. if (howMany < 0)
13. {
14. yield break;
15. }
16. for (var i = 0; i <= howMany; i++)
17. {
18. yield return firstSeed;
19. var sum = firstSeed + secondSeed;
20. firstSeed = secondSeed;
21. secondSeed = sum;
22. }
23. }
24.
25. IEnumerator IEnumerable.GetEnumerator()
26. {
27. return GetEnumerator();
28. }
29. }
01. public static void Main() {
02. foreach(int fiboSeq in new Fibonacci(5)) {
03. Console.Out.WriteLine("{0}", fiboSeq);
04. }
05. }
Output:
0
1
1
2
3
5
24. Covariance &
Contravariance
• Covariance
• Pass collection of sub-class to a collection of base class
• Contravariance
• Pass collection of base class to a collection of sub-class
• Invariance
• Neither of the above applies
25. Arrays and Generic
Collections
• Arrays are Covariant in C# and Java
• There is a hole in the type system and a runtime patch is
applied.
• Generics are Invariant in Java.
• In C#, use leniency offered by IEnumerable if you
need Covariance.
• Only interfaces and delegates can be covariant (out) or
contravariant (in)
30. Java Example
01. abstract class Animal {
02. public abstract String speak();
03. }
04.
05. class Cat extends Animal {
06. public String speak() {
07. return “Meow!”;
08. }
09. }
10.
11. class Dog extends Animal {
12. public String speak() {
13. return “Bark!”;
14. }
15. }
16.
17. class Printer {
18. public static print(Animal [] animals) {
19. animals[0] = new Dog();
20. for (int i = 0; i < animals.length; i++) {
21. System.out.println(animals[i].speak();
22. }
23. }
24.
25. public static print(List<Animal> animals) {
26. for(Animal animal : animals) {
27. System.out.println(animal.speak());
28. }
29. }
30. }
31. Java Example
01. class TestCollections {
02. public static void main(String []args) {
03. Cat cat = new Cat();
04. Animal animal = cat;
05. animal.speak();
06.
07. animal = new Dog();
08. animal.speak();
09.
10. Animal [] animals = new Animal [] { cat, dog };
11. Cat [] cats = new Cat[] { cat };
12. animals = cats;
13. print(animals); //Exposes Hole in Type System
14.
15. // Fails to compile as Generic Collections in
16. // Java are Invariant
17. List<Animal> animals = new ArrayList<Dog>();
18.
19. List<Dog> dogs = new ArrayList<Dog>();
20. dogs.add(dog);
21. dogs.add(dog);
22. print(dogs);
23. }
24. }
01. abstract class Animal {
02. public abstract String speak();
03. }
04.
05. class Cat extends Animal {
06. public String speak() {
07. return “Meow!”;
08. }
09. }
10.
11. class Dog extends Animal {
12. public String speak() {
13. return “Bark!”;
14. }
15. }
16.
17. class Printer {
18. public static print(Animal [] animals) {
19. animals[0] = new Dog();
20. for (int i = 0; i < animals.length; i++) {
21. System.out.println(animals[i].speak();
22. }
23. }
24.
25. public static print(List<Animal> animals) {
26. for(Animal animal : animals) {
27. System.out.println(animal.speak());
28. }
29. }
30. }
32. Specific to C#
• Anonymous Types
• Anonymous Methods/Delegates
• Pass Methods as Data
• Action: No return values
• Func: Non-void return values
• Predicate: A Func that always returns a bool
• Generally above suffice, but if not,then use Custom Delegates
• Delegate Chaining
• Compiler Eye Candy: +=, -= for Combine(), Remove()
• Lambdas
33. Specific to C#
• Events
• Syntactic sugar over delegates, but with visibility
• Events can only be invoked from within the class that declared it, whereas a
delegate field can be invoked by whoever has access to it.
• Events can be included in interfaces, delegates cannot be.
• Built-in EventHandler and EventArgs
• Compiler generated add_ and remove_ methods for
the event.
• dynamic
• ExpandoObject
34. Dynamic Runtime Library
CLR
(Common Language Runtime)
DLR
(Dynamic Language Runtime)
C#
VB.NET
Python
Ruby
• Allows you to talk with
implementations in other
languages
• C#/VB.NET with Python, Ruby
• Also with Silverlight etc..
Dynamically Typed
Statically Typed
01. using IronPython.Hosting;
02. using Microsoft.Scripting.Hosting;
03.
04. var python = Python.CreateRuntime();
05. dynamic script = python.UseFile(“Calculator.py”);
06.
07. // Get PythonType
08. dynamic Calculator = script.GetVariable(“Calculator”);
09. dynamic calc = Calculator(); // Instantiate Object
10.
11. //Invoke method
12. dynamic result = calc.add(2, 3); // 5
35. Specific to C#
• Avoid Configuration hell
• Single Configuration File (App.config)
• Asynchronous Programming
• BeginInvoke and EndInvoke
• Wait with EndInvoke
• Wait with WaitHandle
• Poll for Async call completion
• Execute a callback upon completion
• Task Parallel Library (TPL)
36. Specific to Java
• Static Imports
• Instance and Static Initializers
• Interfaces Containing Fields
• Anonymous Classes
• Proxy Support through Interceptor
37. Specific to Java
• Asynchronous Programming
• Future Task
• Poll Completion with isDone().
• Execute callback upon completion
• Using ListenableFuture and FutureCallback - Google Guava Library
• Or roll your own result completion callback.
• Akka Actors