microsoft .net basics

Post on 14-Jan-2016

75 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

Microsoft .NET Basics. Daragh Byrne – EPCC. Purpose. Microsoft .NET Framework: Microsoft Intermediate Language (MSIL) Common Language Runtime (CLR) Class Libraries Language Compilers Distributed and Web-based computing .NET Programming with C#. Microsoft .NET Framework. .NET Framework. - PowerPoint PPT Presentation

TRANSCRIPT

http://www.epcc.ed.ac.uk/~ogsanet

ogsanet-queries@epcc.ed.ac.uk

February 24th-25th 2004

Microsoft .NET Basics

Daragh Byrne – EPCC

2

Purpose

Microsoft .NET Framework:– Microsoft Intermediate Language (MSIL)– Common Language Runtime (CLR)– Class Libraries– Language Compilers– Distributed and Web-based computing

.NET Programming with C#

3

Microsoft .NET Framework

4

.NET Framework

“Microsoft .NET is a set of Microsoft software technologies for connecting information, people,

systems and devices”

– http://www.microsoft.com/net/basics/whatis.asp

In real terms to the developer:– A new platform for building applications that run in stand-alone mode or

over the Internet

5

Evolution

Next Generation of COM:– Component oriented software is a good thing:

• Win32/C-style APIs are outdated• COM was step in right direction, but painful to program with• COM was restricted to VB, C++• Binary compatibility/portability an issue: x86 version of COM component needed to be

compiled for e.g. PowerPC• Memory management also a pain

Common Object Runtime:– An execution environment for components written in any language:

• Eventually became .NET with incorporation of Web Services• Standardised API

Web Services:– Interoperability is key in the connected world:

• Require open standards for interoperability and leveraging legacy code

6

What’s in the .NET Framework?

Microsoft Intermediate Language (MSIL):– Specification for a platform independent, low-level, stack-based assembly-like

language

Common Language Runtime (CLR):– A common runtime for all .NET applications, compiles and executes MSIL

Class libraries:– Common functionality that can be used by all languages

– Includes Windows Forms for GUI development

Language compilers:– C#, C++, VB…

Distributed computing:– Networking using sockets, Remoting, Web Services and Applications using

ASP.NET

7

Targeting .NET

Source Code (C#, VB.NET) MSIL

CLR

Native Code (x86 etc)

Compiled to

Runs on

Compiled to

8

Microsoft Intermediate Language (MSIL)

A machine-independent assembly language:– Similar in nature to Java byte-code

Target language for all .NET compilers JIT-compiled (Just-In-Time) by the CLR to native code:

– Very efficient late compilation approach

Collection of IL known as a managed Assembly:– Library or executable (JAR in Java-speak)

Can examine with ILDasm:– Disassembler

– Comes with the framework

Possible to implement interpreter/runtime on any platform:– Open standards

9

MSIL Example

Example: method body: // Code size 21 (0x15)

.maxstack 2 .locals init ([0] string CS$00000003$00000000)

IL_0000: ldarg.0 IL_0001: ldfld string NDoc.Core.HtmlHelp::_projectName IL_0006: ldstr ".hhk“ IL_000b: call string [mscorlib]System.String::Concat(string,

string) IL_0010: stloc.0 IL_0011: br.s IL_0013 IL_0013: ldloc.0 IL_0014: ret

Yuck! Thankfully we don’t have to deal with this:

– That’s what compilers are for!– You could do it though!

10

Common Language Runtime

The environment in which all .NET applications run Somewhat like the Java Virtual Machine:

– With explicit multi-language support– With explicit version control at assembly level– JIT-compiles to native code

Deals in the abstract with ‘types’: – classes, structs, interfaces etc.– Handles instances, interactions between instances

Provides runtime services for “Managed Code”:– Type control, exception handling, garbage collection threading etc. – Removes mundane/dangerous tasks from the programmer

11

Running a .NET Application

.NET Executable(Stored as Windows Portable

Executable file)mscoree.dll

Bind to runtime library

Execute MSIL entry point (verifies code,

starts compilation and execution)

12

Types and Assemblies

Fundamentally the CLR deals with instances of ‘types’:– Has a unified type system– Everything descends from System.Object type

Divided into value types or reference types:– Value types are primitives, structs, enums etc and live on the stack

• Derived from the System.ValueType type

– Reference types are instances of classes, interfaces, arrays, delegates that the programmer deals with via references

Assemblies are essentially collections of type definitions:– Including all metadata about those types

13

Type Metadata and the CLR

Every CLR type has metadata associated with it:– Field names and sizes, type name, type size etc

Used system-wide:– Serialization of objects to network, disk, in Web Services– Cross-language interoperability– Intellisense in Visual Studio– We use it in our Grid Services software

Possible to use Reflection API to access metadata at runtime:– Plug and play components, late binding

Possible to define application-specific metadata:– Very useful, more later

14

Metadata Addresses COM Shortcomings

Type system was fragmented External representation of a component had little

bearing on its internal structure:– Interface Definition could not tell you about internals– Needed to use things called Type Libraries to store metadata separately

.NET type system is common among all languages:– Common Type System– C++ string == C# string == VB.NET string

15

CLR Standards and Implementations

Open standard (ECMA) CLR will run on any Windows computer:

– 95/98, ME, 2000– Built in to XP

Based on open standards:– Ports to Linux underway:

• Mono, dotGNU

– Microsoft have a shared-source, cross-platform version known as Rotor:• Runs on FreeBSD

• http://msdn.microsoft.com/net/sscli

16

Class Library (1/2)

IO GUI Programming (naturally!) System Information Collections Components Application Configuration Connecting to Databases (ADO.NET) Tracing and Logging Manipulating Images/Graphics

17

Class Library (2/2)

Interoperability with COM Globalization and Internationalization Network Programming with Sockets Remoting Serialization XML Security and Cryptography Threading Web Services

18

Language Compilers

Over 20 different languages supported to date:– C#, VB, C++– Perl, Python, Scheme, F#, Fortran, J#, write your own!

All produce IL Cross-language compatibility is a feature of the

runtime:– Write component in VB and use from C++, C#, …– Must adhere to the Common Language Specification:

• Limits things you can use e.g. unsigned types, operator overloading

19

Web Application Development

ASP.NET provides a rich platform for developing Web applications and Web Services

A huge leap forward from traditional ASP:– Aimed towards enterprise class, industrial-strength Web applications– Fully integrated with all areas of .NET

Our software is based on this framework

20

Distributed Computing

Remoting and Web Services allow remote procedure calls

Remoting is used to make calls between .NET Application Domains:– Built-in to CLR

Web Services are used to provide cross-platform RPC in an interoperable manner:– ASP.NET and CLR support

21

Obtaining the Framework

Download the Framework SDK via– http://msdn.microsoft.com/netframework/

~110 Mb Support at http://msdn.microsoft.com Visual Studio .NET is available at a reduced rate for

academic institutions

22

.NET Programming with C#

23

C# Features (1/2)

Programming language of choice for the .NET platform:– Microsoft’s preferred language

Java-like, but has much in common with C++:– 70% Java, 10% C++, 5% VB, 15% new

Strongly-typed:– Enforced by the compiler and the runtime

– As are all .NET languages

Object-oriented:– Every object is an instance of a particular Type– Types are class, interface, enum, struct

Single implementation inheritance, multiple interface inheritance a la Java

24

C# Features (2/2)

Close coupling with managed code services:– Garbage collection, threading

Operator overloading allowed:– C++ heritage

Can access raw pointers using unsafe code blocks Properties are a first class language feature:

– Unlike Java where accessor methods must be coded– Syntactic sugar, but nice!

Supports strongly-typed callback mechanisms directly using events/delegates:– Unlike Java, where callback support is indirect (interface based,

anonymous inner classes etc)

25

Really New C# Features (compared to Java)

Supports call by reference:– Use of out and ref keywords

Supports stack-allocated objects (structs) – Value Types

Supports enumerations directly:– Can use as C/C++ style bit-mask/flags

Explicit versioning control:– More a feature of the framework but accessible using C#

True multi-dimensional arrays:– More efficient

Semi-deterministic finalization:– Using IDisposable

26

Namespaces

Means of dividing related classes logically Avoid name clashes Analogous to Java packages, C++ namespaces:

– MyCompany.MyApplication.Module

Declare using braces:– namespace MyNamespace { // classes etc }

Import namespace with using directive:– using System.Xml– Must include assembly where classes belonging to a namespace reside:

• /reference command line option on csc (C# compiler)

– Classes from a namespace do not have to all live in same assembly

System namespace is root of .NET framework classes

27

Sample Program//Person.cs:using System;using SomeLib;namespace MyApplication{ class Person {

private string name_;public string Name{ get { return name_; } set { if(value == null) throw new

ArgumentNullException(“name”); name_ = value; }}

public static void Main {

Person p = new Person(); p.Name = “Daragh”;

Console.WriteLine(p.Name); } }}

Compile as follows:– Produces Person.exe

C:/> csc Person.cs

/reference:SomeLib.dll

Execute:– C:/> person

output: Daragh

28

Using C#

Very intuitive at first if you are a Java programmer:– Some differences will soon be noticed

Command-line is good for learning:– csc.exe, vbc.exe, cl.exe

Best way to use is with Visual Studio .NET:– Nice for GUI apps, great designer for forms, Web applications– Integrates with source control (Source Safe)– Good for large multi-component projects– If you do not have it, there is always the command-line:

• Good to know your way around this way

29

Useful Things (1/2)

Boxing and unboxing:– Primitive (value) types can be treated as reference types without

explicitly wrapping them:• Java : Integer I = new Integer(5);• C# int i = 5 object o = i; o += 1; // i = 5, o = 6;

foreach• foreach(element e in array)• foreach(element e in somethingEnumerable)

30

Useful Things (2/2)

Exception safe casts using asEmployee e = new Employee()Person p = e as Person;if(p != null){...}

Properties are integral:– Don’t define field, accessor, setter– Looks like field to client:public int MyProperty{ get { // logic } set { myField_ = value; }} x.MyProperty = 2;

31

Attributes

Can add custom metadata to your types:

public class SomeType

{

[WebMethod]

public string SomeMethod() { …

}

}

top related