Title: Object Oriented Programming Introduction to .NET
1Object Oriented ProgrammingIntroduction to .NET
- Dr. Mike Spann
- m.spann_at_bham.ac.uk
2Contents
- 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
3Overview
- 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
4What 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
5What 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
6What is .NET?
Second
-
Tier
Second
-
Tier
(database or
other server)
Web Server
Client
Peer to peer
Client
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.NET the big picture
9The 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
10The 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
11The 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
12The Common Language Runtime (CLR)
13Common Intermediate Language, Metadata and JIT
Compilation
- 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
.EXE CIL Metadata
14Common Intermediate Language, Metadata and JIT
Compilation
15Common Intermediate Language, Metadata and JIT
Compilation
- For a simple Hello World program, the CIL looks
like high level assembler
.method private hidebysig static void Main() cil
managed .entrypoint // Code size 11
(0xb) .maxstack 8 IL_0000 ldstr
"Hello, world!" IL_0005 call void
mscorlibSystem.ConsoleWriteLine(string)
IL_000a ret // end of method HelloWorldMain
16Common 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
17Common Intermediate Language, Metadata and JIT
Compilation
18Common 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 wont be called by this run of your
application
19Common Intermediate Language, Metadata and JIT
Compilation
20Common 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
21Managed 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
22Managed 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
23Managed 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
24Managed 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 .dll file is created instead of a .exe file and
no main method is required - More of this when we look at developing C
programs
25Framework 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
26Framework Class Library (FCL)
- The various classes and types are grouped into a
hierarchy of namespaces - Similar to the package/sub-package arrangement of
Java APIs - 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.Forms namespace contains
classes for manipulating top level windows
27Framework 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
28Framework Class Library (FCL)
- Its 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(No Transcript)
30(No Transcript)
31Framework Class Library (FCL)
- Programming using FCL classes is much simpler
than traditional windows programming - Very intuitive
- Equivalent simplicity to Java graphics (Swing)
programming
32Framework 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)
33Visual 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
34Visual Studio.NET
35Visual Studio.NET
- We will look in more detail at how to develop
projects using VS in the next lecture - However, its important to emphasise that for
simple programs, its sometimes easier to use the
command based compiler - Even for developing Windows applications!
36Visual Studio.NET
using System.Windows.Forms using
System.Drawing  class MyFormForm public
static void Main() Application.Run(new
MyForm()) protected override void
OnPaint(PaintEventArgs e) e.Graphics.DrawSt
ring("Hello World!", new Font("Arial", 35),
Brushes.Blue, 10, 100)
37Visual Studio.NET
- We can use the command line compiler to compile
this program and create an .exe file - /Targetwinexe means create a GUI (Windows)
application - We run the program by just typing the name of the
.exe file (HelloGui)
C\gtcsc /Targetwinexe HelloGui.cs
38Visual Studio.NET
39.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
40.NET application scenarios
- Typically an active web-page would involve the
interaction between a browser, a web server and a
database
XHTML
ADO.NET
IE
ASP.NET
Database
Client user interface (Web form)
Web server
41.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)
42.NET application scenarios
- 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
using System class App public static void
Main() DateService webObj new
DateService() webObj.Url
"http//www.wintellect.com/Services/DateService.as
mx" String dateString
webObj.GetDateString() Console.WriteLine(da
teString)
lt_at_ WebService Language"C" Class"DateService"
gt using System using System.Web.Services public
class DateServiceWebService WebMethod
public String GetDateString()
return DateTime.Now.ToString("D")
SOAP/xml
Client
Web server
43Summary
- We have seen a fairly detailed overview of the
.NET framework - We have seen that its a robust environment for
application program development and execution - Its 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