Dotnet Overview

Embed Size (px)

Citation preview

  • 7/28/2019 Dotnet Overview

    1/31

    http://www.epcc.ed.ac.uk/[email protected]

    January 14th-15th 2004

    Microsoft .NET Basics

    Daragh Byrne

    EPCC

  • 7/28/2019 Dotnet Overview

    2/31

    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#

  • 7/28/2019 Dotnet Overview

    3/31

    3

    Microsoft .NET Framework

  • 7/28/2019 Dotnet Overview

    4/31

    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

  • 7/28/2019 Dotnet Overview

    5/31

    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

  • 7/28/2019 Dotnet Overview

    6/31

    6

    Whats in the .NET Framework?

    Microsoft Intermediate Language (MSIL):

    Specification for a platform independent, low-level, stack-based assembly-likelanguage

    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/28/2019 Dotnet Overview

    7/317

    Targeting .NET

    Source Code (C#, VB.NET) MSIL

    CLR

    Native Code (x86 etc)

    Compiled to

    Runs on

    Compiled to

  • 7/28/2019 Dotnet Overview

    8/318

    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 frameworkPossible to implement interpreter/runtime on any platform:

    Open standards

  • 7/28/2019 Dotnet Overview

    9/319

    MSIL Example

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

    .maxstack 2

    .locals init ([0] string CS$00000003$00000000)IL_0000: ldarg.0IL_0001: ldfld string NDoc.Core.HtmlHelp::_projectNameIL_0006: ldstr ".hhkIL_000b: call string [mscorlib]System.String::Concat(string,

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

    Yuck!

    Thankfully we dont have to deal with this: Thats what compilers are for!

    You coulddo it though!

  • 7/28/2019 Dotnet Overview

    10/3110

    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

  • 7/28/2019 Dotnet Overview

    11/3111

    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)

  • 7/28/2019 Dotnet Overview

    12/3112

    Types and Assemblies

    Fundamentally the CLR deals with instances oftypes: 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, delegatesthat the programmer deals with via references

    Assemblies are essentially collections of typedefinitions: Including all metadata about those types

  • 7/28/2019 Dotnet Overview

    13/31

    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 atruntime: Plug and play components, late binding

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

  • 7/28/2019 Dotnet Overview

    14/31

    14

    Metadata Addresses COM Shortcomings

    Type system was fragmented

    External representation of a component had littlebearing 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

  • 7/28/2019 Dotnet Overview

    15/31

    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

  • 7/28/2019 Dotnet Overview

    16/31

    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

  • 7/28/2019 Dotnet Overview

    17/31

    17

    Class Library (2/2)

    Interoperability with COM

    Globalization and Internationalization

    Network Programming with Sockets

    Remoting

    Serialization

    XML

    Security and Cryptography

    Threading

    Web Services

  • 7/28/2019 Dotnet Overview

    18/31

    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 theruntime: 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

  • 7/28/2019 Dotnet Overview

    19/31

    19

    Web Application Development

    ASP.NET provides a rich platform for developing Webapplications 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

  • 7/28/2019 Dotnet Overview

    20/31

    20

    Distributed Computing

    Remoting and Web Services allow remote procedure

    calls

    Remoting is used to make calls between .NETApplication Domains:

    Built-in to CLRWeb Services are used to provide cross-platform RPC

    in an interoperable manner: ASP.NET and CLR support

  • 7/28/2019 Dotnet Overview

    21/31

    21

    Obtaining the Framework

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

    ~110 Mb

    Support at http://msdn.microsoft.comVisual Studio .NET is available at a reduced rate for

    academic institutions

  • 7/28/2019 Dotnet Overview

    22/31

    22

    .NET Programming with C#

  • 7/28/2019 Dotnet Overview

    23/31

    23

    C# Features (1/2)

    Programming language of choice for the .NET platform:

    Microsofts 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 interfaceinheritance a la Java

  • 7/28/2019 Dotnet Overview

    24/31

    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)

    R ll N C# F t ( d t

  • 7/28/2019 Dotnet Overview

    25/31

    25

    Really New C# Features (compared to

    Java)Supports call by reference:

    Use of out and ref keywords

    Supports stack-allocated objects (structs) ValueTypes

    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

  • 7/28/2019 Dotnet Overview

    26/31

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

    Systemnamespace is root of .NET framework classes

  • 7/28/2019 Dotnet Overview

    27/31

    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.exeC:/> csc Person.cs

    /reference:SomeLib.dll

    Execute: C:/>person

    output: Daragh

  • 7/28/2019 Dotnet Overview

    28/31

    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

  • 7/28/2019 Dotnet Overview

    29/31

    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 = 5object o = i;

    o += 1;// i = 5, o = 6;

    foreach foreach(element e in array)

    foreach(element e in somethingEnumerable)

  • 7/28/2019 Dotnet Overview

    30/31

    30

    Useful Things (2/2)

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

    Properties are integral: Dont define field, accessor, setter

    Looks like field to client:public int MyProperty{

    get { // logic }set { myField_ = value; }

    }x.MyProperty = 2;

  • 7/28/2019 Dotnet Overview

    31/31

    31

    Attributes

    Can add custom metadata to your types:

    public class SomeType

    {

    [WebMethod]

    public string SomeMethod(){

    }

    }