visual programming cpe 411 c# review

29
Dr. Natheer Khasawneh. Visual Programming CPE 411 C# Review

Upload: lynna

Post on 30-Jan-2016

30 views

Category:

Documents


0 download

DESCRIPTION

Visual Programming CPE 411 C# Review. .NET Framework. Common Intermediate Language (CIL) Common Language Runtime (CLR) Just-In-Time (JIT) Compiler Common Language Specification Framework Class Library (FCL). Compiler. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Visual Programming CPE 411  C# Review

Dr. Natheer Khasawneh.

Visual ProgrammingCPE 411

C# Review

Page 2: Visual Programming CPE 411  C# Review

Dr. Natheer Khasawneh.

.NET Framework

• Common Intermediate Language (CIL)• Common Language Runtime (CLR) • Just-In-Time (JIT) Compiler• Common Language Specification• Framework Class Library (FCL)

Page 3: Visual Programming CPE 411  C# Review

Dr. Natheer Khasawneh.

Compiler

• Software that translate high-level language (C++, Java, C#, etc) to machine language.

• Compiler X can covert high-level Y to machine language Z.

Page 4: Visual Programming CPE 411  C# Review

Dr. Natheer Khasawneh.

Complier Example

Intel x86 machine code

Motorola 68000 machine code

C++ Code

C++ Complier for Intel

C++ Complier for Motorola

Page 5: Visual Programming CPE 411  C# Review

Dr. Natheer Khasawneh.

Problem

• A big problem facing developers is the many different types of processors that run code.

• Windows, Macintosh, and Unix machines use a wide variety of hardware, as do personal digital assistants, cell phones, large computers, and other platforms.

• One way to make a program work on each of these devices is to translate the program to the native instruction

Page 6: Visual Programming CPE 411  C# Review

Dr. Natheer Khasawneh.

•So if we have 3 programming languages and 3 devices, how many compilers do we need?

•So, how they solved this?!

Page 7: Visual Programming CPE 411  C# Review

Dr. Natheer Khasawneh.

Two Steps Compilation Process

• Compilation is done in two steps:– At compile time: compile each language (C#, C++, etc) to

Common Intermediate Language (CIL)

– At runtime: Common Language Runtime (CLR) uses a Just In Time (JIT) compiler to compile the CIL code to the native code for the device used

RunTime

CompileTime

Page 8: Visual Programming CPE 411  C# Review

Dr. Natheer Khasawneh.

Common Intermediate Language (CIL)

• Much like the native languages of devices.

• CIL was originally known as Microsoft Intermediate Language (MSIL).

• CIL is a CPU- and platform-independent instruction set.

• It can be executed in any environment supporting the .NET framework

• Hello World Example in CIL

Page 9: Visual Programming CPE 411  C# Review

Dr. Natheer Khasawneh.

Common Language Runtime (CLR)

• The Common Language Runtime (CLR) manages the execution of code.

• CLR uses Just-In-Time (JIT) compiler to compile the CIL code to the native code for device used.

• Through the runtime compilation process CIL code is verified for safety during runtime, providing better security and reliability than natively compiled binaries.

• Native image generator compilation (NGEN) can be used to produces a native binary image for the a specific environment. What is the point?

Page 10: Visual Programming CPE 411  C# Review

Dr. Natheer Khasawneh.

.NET Framework Visual Studio .NET

Operating SystemOperating System

Common Language RuntimeCommon Language Runtime

.NET Framework (Base Class Library).NET Framework (Base Class Library)

ADO .NET and XMLADO .NET and XML

ASP .NETASP .NETWeb Forms Web ServicesWeb Forms Web Services

Mobile Internet ToolkitMobile Internet Toolkit

WindowsWindowsFormsForms

Common Language SpecificationCommon Language Specification

C++C++ C#C# VBVB PerlPerl J#J# ……V

isua

l Stu

dio

.NE

TV

isua

l Stu

dio

.NE

T

Page 11: Visual Programming CPE 411  C# Review

Dr. Natheer Khasawneh.

Compilation Process

• So if we have 3 programming languages and 3 devices, how many compilers do we need?

CLR

VBSource code

VB Compiler

C++C#

CIL CILCIL

Operating System Services

Common Language Runtime JIT Compiler

C# Compiler C++ Compiler

Native code

ManagedCode

ManagedCode

ManagedCode

UnmanagedCode

CLR Services

NGEN

Executes under the management of a virtual machine.

Page 12: Visual Programming CPE 411  C# Review

Dr. Natheer Khasawneh.

Page 13: Visual Programming CPE 411  C# Review

Dr. Natheer Khasawneh.

CIL

• C# compiler translates C# source code into CILC# source

CIL.locals init ([0] class Calc c, [1] int32 sum)newobj instance void Calc::.ctor()stloc.0 // c = ptr to new objectldloc.0ldc.i4.2 // pass second argldc.i4.4 // pass first argcallvirt instance int32 Calc::Add(int32,int32)stloc.1 // sum = retval

Calc c = new Calc();int sum = c.Add(2, 4);

C# compiler

Page 14: Visual Programming CPE 411  C# Review

Dr. Natheer Khasawneh.

Platform and Language Independent

• What we have described so far will lead us to Platform independent environment. How?

• Can we use compiled classes written in X language in a program written in Y language?

• VB.NET + C#.NET code

Page 15: Visual Programming CPE 411  C# Review

Dr. Natheer Khasawneh.

Language interoperability

• All .NET languages can interoperate

C# callingVB.NET

class Hello{ static void Main() { System.Console.WriteLine(Greeting.Message()); }}

Class Greeting Shared Function Message() As String Return "hello" End FunctionEnd Class

Page 16: Visual Programming CPE 411  C# Review

Dr. Natheer Khasawneh.

CLR

Execution engine

• Common Language Runtime (CLR) is the execution engine– loads IL

– compiles IL

– executes resulting machine code

ILRuntimecompiler

Executemachine code

Page 17: Visual Programming CPE 411  C# Review

Dr. Natheer Khasawneh.

Cache

JIT runtime compile

• CIL is compiled into machine code at runtime by the CLR– compiles methods as needed– called just in time (JIT) compile

• JIT compilation model:– first time method is called the IL is compiled and optimized– compiled machine code is cached in transient memory– cached copy used for subsequent calls

CIL code F() G() H()

JIT runtimecompiler

Execute

machine code for F()

Page 18: Visual Programming CPE 411  C# Review

Dr. Natheer Khasawneh.

NGEN install time compile

• Can compile CIL into machine code when app installed– use native image generator ngen.exe– can speed startup time since code pre-compiled

– but cannot do as many optimizations

– original IL must still be available for type informationCLR

IL ngen Executenativeimagecache

machine code

Page 19: Visual Programming CPE 411  C# Review

Dr. Natheer Khasawneh.

C#

VB.NET

Language variability

• Not all .NET languages have exactly the same capabilities– differ in small but important ways

class Hello{ static void Main() { int i; uint u; }}

Class Greeting Shared Sub Main() Dim i as Integer End SubEnd Class

signed integer

unsigned integer

signed integer only

Page 20: Visual Programming CPE 411  C# Review

Dr. Natheer Khasawneh.

Common Language Specification

• Common Language Specification (CLS) defines type subset– required to be supported by all .NET languages

– limiting code to CLS maximizes language interoperability

– code limited to CLS called CLS compliant

public class Calculator{ public uint Add(uint a, uint b) { return a + b; }}

not CLS compliantto use uint in publicinterface of public class

Page 21: Visual Programming CPE 411  C# Review

Dr. Natheer Khasawneh.

Framework Class Library (FCL)

• Namespace: A collection of classes and their methods. Example: System.Windows. Forms

• The .NET Framework class library is a library of classes, interfaces, and value types that are included in the Windows Software Development Kit (SDK).

• Namespaces are stored in DLL files called assemblies

• .NET applications must have “references” to these DLLs so that their code can be linked in

• Included in a C# program with the using keyword– If not included, you must give the fully qualified name of

any class method or property you use

• System.Windows.Forms.MessageBox.Show(…)

Page 22: Visual Programming CPE 411  C# Review

Dr. Natheer Khasawneh.

Some Important .Net Namespaces in FCL

• System Core data/auxiliary classes• System.Collections Resizable arrays + other containers• System.Data ADO.NET database access classes• System.Drawing Graphical Output classes (GDI+)• System.IO Classes for file/stream I/O• System.Net Classes to wrap network protocols• System.Threading Classes to create/manage threads• System.Web HTTP support classes• System.Web.Services Classes for writing web services• System.Web.UI Core classes used by ASP.NET• System.Windows.Forms Classes for Windows GUI apps• See online help on ‘Class Library’ or use MSDN (3CDs)

Page 23: Visual Programming CPE 411  C# Review

Dr. Natheer Khasawneh.

Required CLR

• CLR and .NET Framework required to run .NET app– will be incorporated into Windows and service packs

– developers install as part of .NET Framework SDK

– users can run dotnetredist.exe

Page 24: Visual Programming CPE 411  C# Review

Dr. Natheer Khasawneh.

Introduction to C#

• A new component & object oriented language– Emphasis on the use of classes

• Power of C plus ease of use of Visual Basic• Combines the best aspects of C++ and Java

– Conceptually simpler and more clear than C++– More structured than Visual Basic– More powerful than Java

• Syntax very similar to C/C++– No header files

• Managed pointers only– “Almost no pointers” “almost no bugs”

Page 25: Visual Programming CPE 411  C# Review

Dr. Natheer Khasawneh.

C# Classes

• Can contain:– “Fields”: Data members (like C++ variables)– “Methods”: Code members (like C++ functions)– “Properties”: In-between members that expose data (get , set)

Page 26: Visual Programming CPE 411  C# Review

Dr. Natheer Khasawneh.

Properties

• To user program they look like data fields• Within the class they look like code methods• Often provide controlled access to private data fields

– Validity checks can be performed– Values can be obtained or set after validity checks

• Properties use Accessor methods get()and set()• get() to retrieve the value of a data field• set() to change the value of a data field

Page 27: Visual Programming CPE 411  C# Review

Dr. Natheer Khasawneh.

Example: Square Classpublic class Square{ private int side_length; // A Field public int Side_length // A Property { get { return side_length; } set { side_length = value; } } public int area() // A Method { return (side_length * side_length); } public Square(int side) // The Constructor method { side_length = side; }}

csc /t:library Square.cs

Page 28: Visual Programming CPE 411  C# Review

Dr. Natheer Khasawneh.

Instantiating and Using the Square Classclass SquareTest{ static void Main() { string mySide = System.Console.ReadLine(); int mySideInt = int.Parse(mySide); Square sq = new Square(mySideInt); // Construct a Square object called sq // of side_length = 10 // Instantiates the object and invokes // the class constructor int x = sq.Side_length; // Retrieve object’s Side_Length Property sq.Side_length = 15; // Change object’s Side_length Property int sq_area = sq.area(); // Define an integer variable and use // the class area() method to compute // the area of the square System.Console.WriteLine("Area= " + sq_area.ToString()); // Display result in a Console // Note use of ToString() method // to con vert an integer to a string. // WriteLine() is a static method of Console // class }}

csc /r:Square.dll SquareTest.cs

Page 29: Visual Programming CPE 411  C# Review

Dr. Natheer Khasawneh.

Summary

• Understand the concept behind the .NET Framework (CIL, CLR, JIT, FCL, NGEN, Managed Code, CLS)

• Create classes. Compile classes into assemblies• Use classes.• Object-Oriented concepts (abstraction, inheritance,

and polymorphism)• Work hard!