Chair of Software Engineering

C# Programming in Depth Prof. Dr. Bertrand Meyer March 2007 – June 2007

Introducing C# and .NET Framework

Lisa (Ling) Liu Welcome

ƒ Course web page: http://se.inf.ethz.ch/teaching/ss2007/251-0290-00/index.html

ƒ Books: ¾ Judith Bishop & Nigel Horspool C# Concisely, Addison Wesley, 2004 ISBN 0 321 15418 5 ¾ Andrew Troelsen: Pro C# 2005 and the .NET 2.0 Platform, Third Edition, Apress, 2005, ISBN 1-59059-419-3

ƒ Test ¾ The exam for C# programming is to deliver the source code and documents for the given project specifition.

C# Programming lecture 1: Introduction 2 Overview

ƒ Why have this course? ƒ Who should/can take this course? ƒ What are the components of this course? ƒ Overview of .NET and C#

C# Programming lecture 1: Introduction 3 Why have this course?

ƒ .NET provides: ¾ Full interoperability with existing code ¾ Complete and total language integration ¾ A common runtime engine shared by all .NET-aware languages

C# Programming lecture 1: Introduction 4 Who should take this course?

ƒ The only requirement is object-oriented programming (OOP) experience

C# Programming lecture 1: Introduction 5 .NET framework architecture

.NET can be understood as a new runtime environment and a comprehensive base class library.

VB COBOL C++ C# ...

.NET-Aware Common Language Spcification Base Class Library

Common Language Runtime

Windows Linux

C# Programming lecture 1: Introduction 6 C# features

ƒ No pointers required ƒ Automatic memory management through garbage collection ƒ Formal syntactic constructs for enumerations, structures and class properties ƒ The C++ like ability to overload operators for a custom type, without the complexity ƒ Using a syntax very similar to C++ templates to build generics ƒ Full support for interface-based programming techniques ƒ Full support for aspect-oriented programming techniques via attributes

C# Programming lecture 1: Introduction 7 Important point of C#

It can only produce code that can execute within the .NET runtime ¾ Managed code: the term used to describe the code targeting .NET runtime ¾ Assembly: the binary units that contains the managed code

C# Programming lecture 1: Introduction 8 Your .NET Source Code Some .NET *.dll or *.exe Assembly from Some (CIL, Metadata and Manifest) .NET-Aware Language

.NET Execution Base Class Engine (mscoree.dll) Libraries Class Loader (mscorlib.dll and so forth)

Jitter

Platform-specific Instructions

Execute the Understanding .NET Framework member .NET assemblies

C# C# Source code .NET Compiler

CIL and Metada VB VB (*.dll or *.exe) Source code .NET Compiler

COBOL COBOL Source code .NET Compiler

C# Programming lecture 1: Introduction 10 Contents of .NET assemblies

ƒ IL/CIL code ƒ metadata ƒ

C# Programming lecture 1: Introduction 11 Benefit of CIL

//Calc.cs //Calc.vb //CIL code for C# Calc::add method //CIL code for VB Calc::add method .method public hidebysig instance int .method public instance int 32 Add public class Calc •32public Allow Add (int32 classyou x, toCalc int32 build y) cil applications managed using(int32 your x, languageint32 y) cil managed of choice { { Public{ Function Add (ByVal x As Integer, • A//Code single size code 8 (0x8) base running on numerous//CodeByVal operating size y As 9Integer) (0x9) systems As Integer .maxstackpublic int 2 Add (int x, int y) .maxstack 2 return.locals x+y; int ([0] int 32 Add) .locals{ int ([0] int 32 CS$1$0000) IL_0000: ldarg.1 End FunctionIL_0000: nop IL_0001: ldarg.2 return x+y; END ClassIL_0001: ldarg.1 IL_0002: add IL_0002: ldarg.2 } IL_0003: stloc.0 IL_0003: add.ovf }IL_0004: br.s IL_0006 IL_0004: stloc.0 IL_0006: ldloc.0 IL_0005: br.s IL_0007 IL_0007: ret IL_0007: ldloc.0 } //end of method Calc::Add IL_0008: ret } //end of method Calc::Add

C# Programming lecture 1: Introduction 12 JIT/Jitter (just-in-time) Compiler

ƒ .NET runtime environment leverages a JIT compiler for the underlying platform ƒ Cache the results in memory in a manner suited to the target operating system

C# Programming lecture 1: Introduction 13 Base class libraries

ƒ Encapsulate various primitives ƒ Provide support for a number of services required by most real-world applications ƒ Be broken into a number of discrete assemblies

C# Programming lecture 1: Introduction 14 Common Language Runtime (CLR)

.NET Execution Engine (mscoree.dll)

Physically represented Class Loader by a library named mscoree.dll (common object runtime execution engine) Jitter

ƒ Locate, load and manage .NET Platform-specific types on your behalf Instructions ƒ Take care of memory management Execute the ƒ Perform security checks member

C# Programming lecture 1: Introduction 15 The Base Class Library

Data Access Window Forms Security XML/SOAP

Threading File I/O Web Forms ……

The Common Language Runtime (CLR)

Common Type System Common Language Specification (CTS)

ƒ A formal specification that documents how types must be defined in order to be hosted by the CLR ƒ {class, structure, interface, enumeration, delegate} ƒ Intrinsic CTS data types (mscorlib.dll)

C# Programming lecture 1: Introduction 17 Common language specification

ƒ A set of rules that compiler builder must conform to, if they intend their products to function seamlessly within the .NET universe. ¾ Rule: CLS rules apply only to those parts of a type that are exposed outside the defining ¾ Using C# compiler to check your code for CLS compliance: [assembly: System.CLSCompliant[true]]

C# Programming lecture 1: Introduction 18 public class Calc { //Exposed unsigned data is not CLS compliant public int Add (ulong x, ulong y) { return x+y; } }

public class Calc { public int Add (int x, int y) { //As this ulong variable is only used internally //we are still CLS compliant ulong temp; …... return x+y; } } Assembly/Namespace/Type Distinction

ƒ A namespace is a group of related types contained in an assembly. ƒ A single assemble (such as mscorlib.dll) can contain any number of namespaces. ƒ Any language targeting the .NET runtime makes use of the same namespace and same types.

C# Programming lecture 1: Introduction 20 //Hello world in C# //Hello world in Manage Using System; Extensions for C++ public class MyApp #include “stdafx.h” { using namespace System; static void Main() { int main(array Console.WriteLine(“Hi from C#”); ^args) } { } Console::WriteLine(“Hi from managed C++”); //Hello world in VB .NET } Imports System Public Module MyApp Sub Main() Console.WriteLine(“Hi from C#”); End Sub End Module Referencing External Assemblies

ƒ Need to tell the C# compiler the name of the assembly containing the actual CIL definition for the referenced type.

C# Programming lecture 1: Introduction 22 Documents

ƒ ECMA-334: The C# Language Specification ƒ ECMA-335: The Common Language Infrastructure (CLI)

C# Programming lecture 1: Introduction 23