430 likes | 611 Views
Object Oriented Programming Introduction to .NET. Dr. Mike Spann m.spann@bham.ac.uk. Contents. Overview What is .NET? .NET – the big picture The Common Language Runtime Common Intermediate Language, Metadata and JIT Compilation Managed modules and assemblies Framework Class Library
E N D
Object Oriented ProgrammingIntroduction to .NET Dr. Mike Spann m.spann@bham.ac.uk
Contents Overview What is .NET? .NET – the big picture The Common Language Runtime Common Intermediate Language, Metadata and JIT Compilation Managed modules and assemblies Framework Class Library Visual Studio.NET .NET application scenarios
Overview • In this lecture, we will introduce the .NET programming environment in preparation for the course on the object oriented programming using C# • .NET is a multi-language programming platform but C# targets only .NET
What is .NET? • This is a tough question! • .NET is a complex software environment for providing services • From enabling the development of simple standalone (maybe Windows-based) applications running locally • To complex Web services distributed across the internet perhaps using mobile devices
What is .NET? • We live in a distributed world where services are no longer just provided locally • .NET can be used to develop robust software at any point in a distributed application environment • In the past different technologies and maybe even different programming languages were necessary at different parts of the application (client/server/database.....) • .NET is a common framework for all software development and execution
What is .NET? Second - Tier Second - Tier (database or other server) Web Server Client Client Peer to peer
.NET – the big picture • .NET has a number of distinct features : • CLR (Common Language Runtime) • FCL (Framework Class Library) • Metadata • CIL (Common Intermediate Language) • The first two are the most important from a programmers point of view • Any application that runs under the supervision of the CLR is known as a managed application
.EXE SomeSource.cs SomeSource.cs Meta - using System.Windows.Forms ; using System.Drawing ; data class MyForm:Form { public static void Main(){ IL Application.Run ( new MyForm ()); C# Compiler } or Visual Studio.NET protected override void OnPaint ( PaintEventArgs e){ e.Graphics.DrawString ( "Hello World!", new Font("Arial ", 35), Brushes.Blue , 10, 100); Managed Application JIT Compiler 10010100 10110000 (at runtime) 10000000 10111010 11011011 11010111 11000010 01110110 Machine Language Framework Class Library (Executed by CPU) (a huge reusable collection of types) Common Language Runtime Windows (or other operating system ) .NET – the big picture
The Common Language Runtime (CLR) • .NET code runs under the supervision of the CLR • For this reason such code is called Managed Code • The CLR supplies memory management, type safety, code verifiability, thread management and security • These are complex issues, especially security • However, the key point is we can safely plug in software components into our application from diverse and un-trusted sources
The Common Language Runtime (CLR) • The CLR manages memory for managed code • All allocations of objects and buffers made from a Managed Heap • Unused objects and buffers are cleaned up automatically through Garbage Collection • Some of the worst bugs in software development are not possible with managed code • Leaked memory or objects • References to freed or non-existent objects • Reading of uninitialized variables
The Common Language Runtime (CLR) • All programs running under CLR allocate memory from a managed heap • The managed heap is maintained by the CLR • It is used for all memory resources, such as data buffers, strings, collections, stacks and caches • Enables automatic garbage collection to be efficient as the CLR can keep a track of unused memory
Common Intermediate Language, Metadata and JIT Compilation .EXE = CIL + Metadata • A managed executable comprises instructions expressed in the common intermediate language (CIL) plus metadata • The key point is that the CIL is completely language independent (C#, C++, Java, Perl ....) • Metadata describes high level features of the CIL instructions, for example • Method argument types, return values • Class definitions • Provides type information and binding rules
SomeSource.cs SomeSource.cs using System.Windows.Forms ; using System.Drawing ; class MyForm:Form { public static void Main(){ Application.Run ( new MyForm ()); C# Compiler } protected override void OnPaint ( PaintEventArgs e){ e.Graphics.DrawString ( "Hello World!", SomeSources.exe new Font("Arial",35), Brushes.Blue , 10, 100); Metadata A Managed CIL Application Common Intermediate Language, Metadata and JIT Compilation
Common Intermediate Language, Metadata and JIT Compilation .method private hidebysig static void Main() cil managed { .entrypoint // Code size 11 (0xb) .maxstack 8 IL_0000: ldstr "Hello, world!" IL_0005: call void [mscorlib]System.Console::WriteLine(string) IL_000a: ret } // end of method HelloWorld::Main For a simple ‘Hello World’ program, the CIL looks like ‘high level’ assembler :
Common Intermediate Language, Metadata and JIT Compilation The Just in Time (JIT) compiler is part of the CLR The JIT compiler uses both the metadata and the IL from a managed executable to create machine language instructions at runtime These machine language instructions are executed natively
Common Intermediate Language, Metadata and JIT Compilation • For efficiency, the JIT compiler is only invoked the first time an object method is called • Subsequently execution jumps straight into native code • The system never wastes time JIT compiling methods that won’t be called by this run of your application
Assembly Language Compiler Native Code JIT Compiler Execution Common Intermediate Language, Metadata and JIT Compilation Compilation Code (IL) Source Code Metadata At installation or the first time each method is called
Common Intermediate Language, Metadata and JIT Compilation • However, in the process of JIT compiling the code some very important things happen. • Type-safety and security are verified • Code correctness is verified (no dangling memory references, or referencing unassigned data) • Code executes at native speed • In short, the combination of CLR verification and JIT compilation will increase the functionality and robustness traditional console or GUI applications • However, for widely distributed applications that make use of components from many sources, the advantages of managed code are a necessity
Managed modules and assemblies • .NET refers to the combination of the CIL and metadata as a managed module • However, the CLR cannot execute a managed module • The smallest unit of executable code is referred to as an assembly
Managed modules and assemblies • A managed module is made up of only a single file • However, a managed assembly can be made up of one or more files • These files can be any number of managed modules • Also assemblies can also include resource files (which can be any kind of file, including .jpg and .xml files, for example) • Assemblies also contain a manifest • Describes the files that make up the assembly
Managed modules and assemblies Managed Module IL and Metadata Managed Module IL and Metadata Managed Module IL and Metadata Resource File(.jpg, .html, etc.) AssemblyContains Manifest of files
Managed modules and assemblies • Typically re-useable components exist in assemblies external of the main executable assembly • Pretty much the same as linking in external libraries • A .dllfile is created instead of a .exe file and no main method is required • More of this when we look at developing C# programs
Framework Class Library (FCL) • The FCL is a massive collection of classes, structures, enumerated types and interfaces defined and implemented for reuse • For example, with the FCL we can use it to • Display windows • Manipulate files • Create container objects (such as arrays) • Do maths • The FCL also has advanced classes for creating web and distributed applications
Framework Class Library (FCL) • The various classes and types are grouped into a hierarchy of namespaces • Similar to the package/sub-package arrangement of Java API’s • Helps find specific classes for your application • For example the System.IO namespace is a good place to look in the documentation for classes related to input/output • The System.Windows.Formsnamespace contains classes for manipulating top level windows
Framework Class Library (FCL) • Some Important .NET Namespaces • System Core data/auxiliary classes • System.Collections Resizable arrays + other containers • System.Data ADO.NET database access classes • System.Drawing Graphical Output classes • System.IO Classes for file/stream I/O • System.Net Classes to wrap network protocols • System.Web HTTP support classes • System.Web.Services Classes for writing web services • System.Web.UI Core classes used by ASP.NET • System.Threading Classes to create/manage threads • System.Windows.Forms Classes for Windows GUI apps
Framework Class Library (FCL) • It’s important to be able to efficiently use the FCL documentation which comes with Visual Studio or the .NET SDK • We can access the documentation from the Visual Studio homepage • Each FCL class has its own page with a lot of information • The most useful is some example code • Code can be language filtered
Framework Class Library (FCL) • Programming using FCL classes is much simpler than ‘traditional’ windows programming • Very intuitive • Equivalent simplicity to Java graphics (Swing) programming
Framework Class Library (FCL) HWND hwndMain = CreateWindowEx( 0, "MainWClass", "Main Window", WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, (HWND)NULL, (HMENU)NULL, hInstance, NULL); ShowWindow(hwndMain, SW_SHOWDEFAULT); UpdateWindow(hwndMain); Displaying a window using the Windows API Dim form As New Form() form.Text = "Main Window" form.Show() Displaying a window using FCL (VB)
Visual Studio.NET • Visual Studio .NET is not part of the .NET Framework! • Visual Studio is an integrated development environment published by Microsoft for writing Windows programs • But .NET applications can be developed with a simple text editor and compiled and run with command line instructions! • Obviously VS requires the .NET framework to be installed
Visual Studio.NET • We will look in more detail at how to develop projects using VS in the next lecture • However, it’s important to emphasise that for simple programs, its sometimes easier to use the command based compiler • Even for developing Windows applications!
Visual Studio.NET using System.Windows.Forms; using System.Drawing; class MyForm:Form { public static void Main() { Application.Run(new MyForm()); } protected override void OnPaint(PaintEventArgs e) { e.Graphics.DrawString("Hello World!", new Font("Arial", 35), Brushes.Blue, 10, 100); } }
Visual Studio.NET C:\>csc /Target:winexe HelloGui.cs • We can use the command line compiler to compile this program and create an .exe file: • /Target:winexemeans create a GUI (Windows) application • We run the program by just typing the name of the .exe file (HelloGui)
.NET application scenarios • Applications can be developed in .NET for the following scenarios • Standalone applications • Console • Windows • Active Web Applications • Web forms • Server side processing
.NET application scenarios XHTML ADO.NET IE ASP.NET Database Client user interface (Web form) Web server Typically an active web-page would involve the interaction between a browser, a web server and a database
.NET application scenarios • Web service applications • Software components that perform a task in a distributed manner across the internet • Web services use standard protocols such as SOAP and XML to communicate data between machines across the internet • The .NET Framework can easily be used to create and expose web service applications on the Internet • It is also very easy to create web-service client software using C# (or any other .NET Language)
} } .NET application scenarios using System; class App{ public static void Main(){ DateService webObj = new DateService(); webObj.Url = "http://www.wintellect.com/Services/DateService.asmx"; String dateString = webObj.GetDateString(); Console.WriteLine(dateString); } } <%@ WebService Language="C#" Class="DateService" %> using System; using System.Web.Services; public class DateService:WebService{ [WebMethod] public String GetDateString() { return DateTime.Now.ToString("D"); SOAP/xml A web service is similar to an active web page but the client is another piece of software, rather than a human user using a browser Client Web server
Summary • We have seen a fairly detailed overview of the .NET framework • We have seen that it’s a robust environment for application program development and execution • It’s language neutral although C# only targets .NET • It removes many of the programmer headaches for the development of windows (component-based) applications as well as distributed applications • Much of the technicalities we have discussed are transparent to the programmer