Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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# = vedi definito C# = C ++++ C# = C ++ Do diesis 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 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 Outline Introduction to .NET and CLR Overview of C# wrt Java Java as subset of C# CLR type system Outline Introduction to .NET and CLR Overview of C# wrt Java Java as subset of C# CLR type system 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 Common Language Runtime in different languages issupport like composing Programming Goal of .NET initiative is to pieces in different keys, particularly if you work at the program atwritten all levels keyboard. interoperability If you have learned or pieces in many keys,is each key will have its own designed special emotional aura. CLR a runtime support to be Also, certain kinds of figurations “lie in the hand” in one used byaremany languages key but awkward in another. So you are channeled by your choice of supported key. In some ways, even enharmonic Main services by .NET are: keys, such as C-sharp and D-flat, are quite distinct in feeling. A common type how system This shows a notational system can play a rolecollection in shaping system the final product. significant A garbage An (Gödel, Escher, Bach: an eternal golden braid, Hofstadter, 1980, Chapter X) execution environment 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 Common Language Runtime CLR could be roughly seen as a shared backend 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 How CLR works C# Unmanaged GC x86 C++ Managed x86 Security Managed ML CIL VB BCL Loader JIT CLR … 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) Outline Introduction to .NET and CLR Overview of C# wrt Java Java as subset of C# CLR type system 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 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 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 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 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 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 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 Outline Introduction to .NET and CLR Overview of C# wrt Java Java as subset of C# CLR type system 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) Hello world import using System; java.lang; package namespace it.unipi.di.hello; it.unipi.di.hello { public class Hello { public static void main(String[] Main(string[] args) { System.out.println("Hello Console.WriteLine ("Hello world"); } }} } 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 {} 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"; } 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 { } 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 Outline Introduction to .NET and CLR Overview of C# wrt Java Java as subset of C# CLR type system 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 Java type system T[] Object Class interface T String class T int Base types CLR type system Array Object Type interface T String ValueType T[] Struct T int Base types class T Enum Enum T Delegate Delegate T 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 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 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: System.Int32 double System.Double string System.String int All CLR types are accessible from C# and vice-versa. Type constructors in C# Type constructors are: of type T T[] Delegate delegate ... Enum enum { … } Value types struct T { … } Classes class T { … } Interfaces interface T { … } Array Next lecture Array types Enum types Value types Delegate types differences from classes boxing and unboxing Base structure Multicast delegates Event model using delegates Event type