1 / 43

Object Oriented Programming Introduction to .NET

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

Download Presentation

Object Oriented Programming Introduction to .NET

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Oriented ProgrammingIntroduction to .NET Dr. Mike Spann m.spann@bham.ac.uk

  2. 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

  3. 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

  4. 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

  5. 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

  6. What is .NET? Second - Tier Second - Tier (database or other server) Web Server Client Client Peer to peer

  7. .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

  8. .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

  9. 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

  10. 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

  11. 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

  12. The Common Language Runtime (CLR)

  13. 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

  14. 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

  15. 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 :

  16. 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

  17. Common Intermediate Language, Metadata and JIT Compilation

  18. 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

  19. 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

  20. 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

  21. 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

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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. Framework Class Library (FCL) • Programming using FCL classes is much simpler than ‘traditional’ windows programming • Very intuitive • Equivalent simplicity to Java graphics (Swing) programming

  30. 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)

  31. 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

  32. Visual Studio.NET

  33. 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!

  34. 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); } }

  35. 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)

  36. Visual Studio.NET

  37. .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

  38. .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

  39. .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)

  40. } } .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

  41. 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

More Related