Presentation is loading. Please wait.

Presentation is loading. Please wait.

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#

Similar presentations


Presentation on theme: "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#"— Presentation transcript:

1 Java2C# Antonio Cisternino Part I

2 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

3 Visual Studio.NET Gli studenti di informatica e di ingegneria informatica hanno diritto ad una copia di VS.NET Distribuzione (modello biblioteca):  Firmare alla consegna  Prendere i dischi  Riportare i dischi!  Firmare la restituzione

4 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

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

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

7 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

8 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 system  A garbage collection system  An 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)

9 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

10 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

11 How CLR works C# C++ ML VB … CIL x86 Unmanaged Managed JIT Managed x86 GC CLR Security BCL Loader

12 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)

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

14 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

15 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

16 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

17 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

18 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 classes  Thread synchronization  Class loading

19 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

20 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

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

22 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)

23 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"); }} }

24 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 {}

25 Methods Java String 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"; }

26 Exception handling Java try { foo(); }catch (IOException f) { } catch(Exception e){ … // Not use e } finally { } C# try { foo(); } catch (IOException f) { } catch { … // General handler } finally { }

27 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

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

29 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

30 Java type system Object interface T int Base types Class String T[] class T

31 CLR type system Object interface T int Base types Type String Array class T ValueType T[] DelegateDelegate T EnumEnum T Struct T

32 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

33 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

34 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.Int32  double  System.Double  string  System.String All CLR types are accessible from C# and vice-versa.

35 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 { … }

36 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


Download ppt "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#"

Similar presentations


Ads by Google