Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C...

36
Java2C# Antonio Cisternino Part I

Transcript of Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C...

Page 1: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

Java2C#

Antonio Cisternino

Part I

Page 2: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

Premessa

C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole:

C# = vedi definito C# = C ++++

C# = C ++ Do diesis

Page 3: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

Visual Studio .NET

Gli studenti di informatica e di ingegneria informatica hanno diritto ad una copia di VS.NET

Distribuzione (modello biblioteca):Firmare alla consegnaPrendere i dischiRiportare i dischi!Firmare la restituzione

Page 4: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

Foreword: .NET and Java

.NET comes after Java and many features have been borrowed from it as well from other systems

Java has done the same in the past borrowing from C++, Smalltalk, and many others (thus try to understand the differences and improvements!)

Frameworks tend to collect a set of techniques developed by researchers and organize them in a meaningful way

With .NET the computer science onion grows with an additional layer: on top of the OS lies the abstract machine and the framework

Page 5: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

Outline

Introduction to .NET and CLR Overview of C# wrt Java Java as subset of C# CLR type system

Page 6: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

Outline

Introduction to .NET and CLR Overview of C# wrt Java Java as subset of C# CLR type system

Page 7: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

Microsoft .NET

‘Dot NET’ is a brand name to indicate a set of technologies. It is a platform to develop applications.

Two essential elements of the platform are: The Common Language Runtime (CLR) A class library (Framework) which complements the

CLR Framework and CLR offer a set of services and

an execution environment to .NET programs

Page 8: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

Common Language Runtime

Goal of .NET initiative is to support program interoperability at all levels

CLR is a runtime support designed to be used by many languages

Main services supported by .NET are: A common type systemA garbage collection systemAn execution environment

Programming in different languages is like composing pieces in different keys, particularly if you work at the keyboard. If you have learned or written pieces in many keys, each key will have its own special emotional aura. Also, certain kinds of figurations “lie in the hand” in one key but are awkward in another. So you are channeled by your choice of key. In some ways, even enharmonic keys, such as C-sharp and D-flat, are quite distinct in feeling. This shows how a notational system can play a significant role in shaping the final product.

(Gödel, Escher, Bach: an eternal golden braid, Hofstadter, 1980, Chapter X)

Page 9: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

Common Language Runtime

CLR supports the execution of programs in Common Intermediate Language (CIL) format

CIL is a language of a stack based machine The core of CLR is the Execution Engine (EE) EE is responsible of compiling CLI code into

machine code using a JIT

Page 10: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

Common Language Runtime

CLR could be roughly seen as a shared back-end of multiple compilers

The overall model is: compile a language into CIL (compiler) compile CIL into executable (runtime) Execute the compiled program

The model is different from Java where an interpreter is needed and JIT an optimization

Page 11: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

How CLR works

C#

C++

ML

VB

CIL

x86

Unmanaged

Managed

JIT

Managed x86

GC

CLR

Security

BCL

Loader

Page 12: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

CLR and progr. languages

If a language compiles to CIL may offer types to other programs

A language may expose CIL types written in other languages

Together with CLR Microsoft deploys a new language called C#

Both CLI and C# are ECMA standards and a reference implementation has been released by Microsoft (http://msdn.microsoft.com)

Page 13: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

Outline

Introduction to .NET and CLR

Overview of C# wrt Java Java as subset of C# CLR type system

Page 14: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

C# Language

C# is a language derived from C++ Java has influenced the language

although most influence come from CLR It has been designed together with CLR

thus it exploits all its services It is more expressive than Java Interoperability is built-in into language

Page 15: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

C# Overview

It is object oriented It supports interfaces + single inheritance Type system is common rooted (in object) Objects on the heap are garbage collected It allows stack-based allocation of quasi-objects It exposes metadata extensions as attributes It introduces delegate type and events

Page 16: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

C# Overview (cont.)

It allows to control over virtual methods Methods parameters could be labeled in/out/ref Classes may have a destructor like C++ Explicit release of resources is handled through

interfaces Better control on name clashes It supports properties It allows overloading of operators

Page 17: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

C# Overview (cont.)

Arrays can be multidimensional Alias for types are allowed XML documentation is supported Access to memory through pointers in unsafe

mode A preprocessor-like syntax is supported Through platform invoke it is possible to access

external functions stored in dlls

Page 18: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

C# and Java

‘Italic’ features are shared with Java Java is essentially a subset of C# with four

important exceptions:Throws clause on methods Inner classesThread synchronizationClass loading

Page 19: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

C# and Java (cont.)

Inner classes in C# are allowed only as namespace, state must be explicitly passed as arguments

Threads are exposed through the Monitor class Class loading model has same expressivity on both

sides IMPORTANT: C# and CLR have been designed together

but are separate thus functionality are better separated than in Java/JVM

Thus understanding C# benefits from some understanding of the underlying CLR

Page 20: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

C# and Java (cont.)

C# allows defining multiple classes in the same file: the unit of deployment is the Assembly (very different from JAR!)

Types are organized in namespaces that are similar to Java packages

Namespaces are defined as blocks rather than statements at the beginning of the file

A single file may contain definitions related to different namespaces

Page 21: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

Outline

Introduction to .NET and CLR Overview of C# wrt Java

Java as subset of C# CLR type system

Page 22: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

Java in C#

The syntax of C# is rather closed to Java because both derive from C++

The most noticeable syntax difference in the shared constructs are extends and implements keywords not present in C#

package nearly maps into namespace C# adopts Pascal convention (capital first)

Page 23: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

Hello world

import java.lang;package it.unipi.di.hello;public class Hello { public static void main(String[] args) { System.out.println("Hello world"); }}

using System;namespace it.unipi.di.hello {public class Hello { public static void Main(string[] args) { Console.WriteLine ("Hello world"); }}}

Page 24: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

Inheritance and interfaces

Java

class A {}

interface B {}

interface C {}

class D extends A

implements B, C

{}

C#

class A {}

interface B {}

interface C {}

class D : B, C, D

{}

Page 25: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

Methods

JavaString f(int i){ while (true) { for (int j = 0; j < 10; j++) …; } return "f";}

C#string f(int i) { while (true) { for (int j = 0; j < 10; j++) …; } return "f";}

Page 26: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

Exception handling

Javatry { foo();}catch (IOException f) {

} catch(Exception e){ … // Not use e} finally {}

C#try { foo();} catch (IOException f) {

} catch { … // General handler} finally {}

Page 27: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

Other stuffs

Local variable declarations used in Java can be used in C#

Protection mechanisms (private, protected, public, internal/package) are the same

Java values are included in C# (C# allows custom defined values): numbers and references

Separation between CLR and C# is better defined than Java and JVM: there is the notion of “standard library” defined within the standard

Page 28: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

Outline

Introduction to .NET and CLR Overview of C# wrt Java Java as subset of C#

CLR type system

Page 29: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

Type system

One way to address interoperability (perhaps the most important) within CLR is sharing the same type representation for multiple languages

C# exposes most of the CLR type system CLR type system is really sophisticated

with respect to the one exposed by JVM

Page 30: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

Java type system

Object

interface T

intBase types

Class

String

T[]

class T

Page 31: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

CLR type system

Object

interface T

int Base types

Type

String

Array

class T

ValueType

T[]

Delegate Delegate T

Enum Enum T

Struct T

Page 32: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

Type system features

Type system is common rooted! The class System.Int32 is inherited from Object

The trick to avoid inefficiencies in treating values as objects is called boxing/unboxing

Value types can be defined although with some restriction: i.e. no inheritance

Page 33: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

Type system features

There are several type constructors: array, delegates, enumerations

Enumerations are equivalent to integer but type information is preserved at runtime

Type descriptions are accessible by programs through reflection

Through class Type and related classes it is possible to inspect type’s structure

Page 34: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

C# and the CLR type system

C# standard defines many base types (int, string, object, and so on) that are mapped into CLR types; for instance: int System.Int32double System.Doublestring System.String

All CLR types are accessible from C# and vice-versa.

Page 35: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

Type constructors in C#

Type constructors are:Array of type T T[]Delegate delegate ...Enum enum { … }Value types struct T { … }Classes class T { … } Interfaces interface T { … }

Page 36: Java2C# Antonio Cisternino Part I. Premessa C# si pronuncia circa come “see sharp”!!! Non C cancelletto Non C gratella Non C diesis Gioco di parole: C#

Next lecture

Array types Enum types Value types

differences from classes boxing and unboxing

Delegate types Base structure Multicast delegates Event model using delegates

Event type