1[1].philosophy of

Upload: srinidhi-ambalam

Post on 10-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 1[1].Philosophy of

    1/35

    C# Programming and.NET Concepts

    By

    S. Nandagopalan

    [email protected]

  • 8/8/2019 1[1].Philosophy of

    2/35

    S. Nandagopalan, B I T 2

    Books for Reference

    C# and the .NET Platform (2 Ed) By Andrew Troelsen

    Dreamtech Publications Microsoft Visual C# .NET

    By Mickey Williams

    Microsoft Press

  • 8/8/2019 1[1].Philosophy of

    3/35

    S. Nandagopalan, B I T 3

    Chapter - 1

    The Philosophy of .NET

  • 8/8/2019 1[1].Philosophy of

    4/35

    S. Nandagopalan, B I T 4

    Objectives

    Understanding the previous state ofaffairs

    The .NET Solution Building blocks of .NET Platform CLR, CTS, and CLS

    .NET Base Class Libraries

  • 8/8/2019 1[1].Philosophy of

    5/35

    S. Nandagopalan, B I T 5

    Understanding theprevious state of affairs

    As a C/Win32 API Programmer It is complex C is a short/abrupt language Manual memory management, ugly pointer

    arithmetic, ugly syntactic constructs Not a OO language

    As a C++/MFC Programmer Root is C C++ with MFC is still complex and error-prone

    As a VB 6 Programmer Not a complete OOP (Object-aware) Why? Doesnt support inheritance No multithreading No parameterized Classes Low-level API calls are complex

  • 8/8/2019 1[1].Philosophy of

    6/35

    S. Nandagopalan, B I T 6

    Previous state of affairs

    As a Java/J2EE Programmer Use of Java front-to-back during development

    cycle No language freedom!

    Pure Java is not suitable for graphic intensiveproblems (E.g. 3D game)

    No cross-language integration

    As a COM Programmer Complex creation of COM types Active Template Library (ATL) Forced to contend with brittle/fragile registration

    entries Deployment issues

  • 8/8/2019 1[1].Philosophy of

    7/35

    S. Nandagopalan, B I T 7

    .NET Solution

    Full interoperability with existing Win32 Code Existing COM binaries can interoperate with .NET binaries

    Complete and total language integration Supports cross-language inheritance, exception handling, and

    debugging

    Common runtime engine shared by all .NET awarelanguages

    A base class library Good object model used by all .NET aware languages

    No more COM plumbing!

    No IClassFactory, IUnKnown, IDispatch, etc. Truly simplified deployment model No need to register a binary unit into the system registry Allows multiple versions of same *.dll

  • 8/8/2019 1[1].Philosophy of

    8/35

    S. Nandagopalan, B I T 8

    .NET Framework

    Operating SystemOperating System

    Common Language RuntimeCommon Language Runtime

    Base Class LibraryBase Class Library

    ADO.NET and XMLADO.NET and XML

    ASP.NETASP.NET

    Web Forms Web ServicesWeb Forms Web Services

    WindowsWindows

    FormsForms

    Common Language SpecificationCommon Language Specification

    VBVB C++C++ C#C# JScriptJScript J#J#

    Vis

    ualStudio.NE

    T

    Vis

    ualStudio.NE

    T

  • 8/8/2019 1[1].Philosophy of

    9/35

    S. Nandagopalan, B I T 9

    Building Blocks of .NET

    CLR (Common Language Runtime) To locate, load, and manage .NET types Automatic memory management, language

    integration, and type safety

    CTS (Common Type System) Describes all possible data types and

    programming constructs supported by theruntime

    CLS (Common Language Specification) A set of rules that defines a subset of types and

    specifications

  • 8/8/2019 1[1].Philosophy of

    10/35

    S. Nandagopalan, B I T 10

    CLR(Common Language Runtime) CLR sits on top of OS (same as JVM of Java) CLR loads modules containing executables and executes

    them

    Code may be managed or unmanaged Managed code consists of instructions in pseudo random

    code called CIL (Common Intermediate Language). CILinstructions are JIT compiled into native machine code atruntime

    JIT compiled methods reside in cache until the applicationslife time

    Advantages of managed code: type safety, memory

    management, and code verification security CLR can translate code from C#, J#, C, C++, VB, and

    Jscript into CIL.

    CLR doesnt launch a new process for every application. Itlaunches one process and hosts individual applications inapplication domains

  • 8/8/2019 1[1].Philosophy of

    11/35

    S. Nandagopalan, B I T 11

    Common Language Runtime

    Base Class Libraries

    Encapsulates various primitives like: threads, fileIO, graphical rendering, and other interaction withHW devices

    It also provides: database manipulation, XMLintegration, Web-enabled front-end.

    CTS CLS

    Base Class Libraries

    Threading

    Data Access

    File IO XML/SOAP

    GUI

  • 8/8/2019 1[1].Philosophy of

    12/35

    S. Nandagopalan, B I T 12

    C#

    Almost same as Java

    No pointers required

    Automatic memory management (No delete)

    Enumeration, class, structure, etc.

    Operator overloading allowed

    Interface-based programming techniques

    Assign characteristics to types (same as COMIDL)

    C# can produce code that can run only on .NETenvironment (unlike COM server or Win32 API)

  • 8/8/2019 1[1].Philosophy of

    13/35

    S. Nandagopalan, B I T 13

    Understanding Assemblies

    Windows applications have dependencieson one or more DLLs

    These DLLs may contain COM classes

    registered in System registry When these components are updated,

    applications may break 'DLL hell'

    Solution: .NET Assemblies

    C# .NET compiler doesn't generatemachine code. It is compiled into "assembly"

  • 8/8/2019 1[1].Philosophy of

    14/35

    S. Nandagopalan, B I T 14

    Assembly

    Intermediate Language (IL/CIL): Same as first pass of compiler. It can't be executed (it

    is not in binary format)

    Metadata Describes the assembly contents No need for component registry Each assembly includes information about references

    to other assemblies E.g. If you have a class called Car in a given

    assembly, the type metadata describes Car's baseclass, which interfaces are implemented by Car,description of members of Car.

    C# source code C# .NET CompilerAssembly

    Metadata

    IL+ =

  • 8/8/2019 1[1].Philosophy of

    15/35

    S. Nandagopalan, B I T 15

    Assembly

    When CLR loads your application, it examines yourprogram's metadata to know which externalassemblies are required for execution

    Private assemblies

    Used by single application Is not shared Most preferred method

    Shared assemblies Intended for multiple applications Global Assembly Cache

    Manifest The metadata of assemblies: version, list of

    externally defined assemblies, etc.

  • 8/8/2019 1[1].Philosophy of

    16/35

    S. Nandagopalan, B I T 16

    Example of CIL CIL sits above a specific compiler

    (C#, J#, etc.)

    The associated compiler emits CILinstructions

    using System;namespace Calculator

    {public class CalcApp{

    public static void Main(string[] args){

    Calc c = new Calc();int ans = c.Add(10, 84);Console.WriteLine(ans);Console.ReadLine();

    }}

    public class Calc{

    public int Add(int x, int y){ return x + y; }

    }}

    All .NET aware languagesemit same CIL instructions

  • 8/8/2019 1[1].Philosophy of

    17/35

    S. Nandagopalan, B I T 17

    CIL of Add() Method

    .method public hidebysig instance int32 Add(int32 x,int32 y) cil managed

    {// Code size 8 (0x8).maxstack 2

    .locals init ([0] int32 CS$00000003$00000000)IL_0000: ldarg.1IL_0001: ldarg.2IL_0002: addIL_0003: stloc.0IL_0004: br.s IL_0006IL_0006: ldloc.0IL_0007: ret

    } // end of method Calc::Add

  • 8/8/2019 1[1].Philosophy of

    18/35

    S. Nandagopalan, B I T 18

    Manifest.assembly extern mscorlib{.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4...ver 1:0:5000:0

    }.assembly ConsoleApplication1

    {

    .hash algorithm 0x00008004

    .ver 1:0:2058:39833}.module ConsoleApplication1.exe// MVID: {51BE4F31-CBD0-4AE6-BC9D-F9A4976795FD}.imagebase 0x00400000

    .subsystem 0x00000003

    .file alignment 4096

    .corflags 0x00000001// Image base: 0x070b0000

    ExternalAssembly

  • 8/8/2019 1[1].Philosophy of

    19/35

    S. Nandagopalan, B I T 19

    CIL to Execution

    Jitter compiles CIL instructions on the fly intocorresponding machine code and cache it. This is usefulfor not recompiling, if the same method is called again

    CIL JIT

    Desktop

    Pocket PC

    Server

  • 8/8/2019 1[1].Philosophy of

    20/35

    S. Nandagopalan, B I T 20

    Common Type System (CTS)

    CTS is a formal specification thatdescribes how a given type must bedefined for CLR

    CTS Class Type

    CTS Structure Type

    CTS Interface Type

    CTS Enumeration type

    CTS Delegate type

  • 8/8/2019 1[1].Philosophy of

    21/35

    S. Nandagopalan, B I T 21

    CTS Class Type

    Same as C++ class

    Can contain members: methods,

    properties, events, etc. Support for abstract members that

    define a polymorphic interface forderived classes

    Multiple inheritance is not allowed

  • 8/8/2019 1[1].Philosophy of

    22/35

    S. Nandagopalan, B I T 22

    CTS Class Characteristics

    "sealed"? sealed classes can't functionas base classes

    Implement any interfaces? An

    interface is a collection of abstractmembers

    Abstract or Concrete? Abstract classes(to define common behaviors for derived)can't be created directly but concreteclasses can.

    Visibility? visibility attribute to knowwhether external assemblies can use it.

  • 8/8/2019 1[1].Philosophy of

    23/35

    CTS Structure types Same as C/C++ Derived from a common base class

    System.ValueType CTS Interface Type

    Same as pure abstract class of C++ A description of work that a derived class can

    perform Similar to a class, but can never be instantiated

    CTS Enumeration type To group name/value pairs under a specific name Default Storage: System.Int32 (could be changed)

    CTS Delegate type Same as C's function pointer (System.MulticastDelegate) Useful for event handling (ASP .NET)

  • 8/8/2019 1[1].Philosophy of

    24/35

    S. Nandagopalan, B I T 24

    Intrinsic CTS Data Types

    .NET Base Type C# TypeSystem.Byte Byte

    System.SByte sbyte

    System.Int16 short

    System.Int32 int

    System.Int64 long

    System.UInt64 ulong

    System.Single float

    System.Double double

    System.Object object

    System.String string

    System.Boolean bool

  • 8/8/2019 1[1].Philosophy of

    25/35

    S. Nandagopalan, B I T 25

    Common Language Specification

    (CLS)

    Set of guidelines that describe the minimaland complete set of features a given .NETaware compiler must support

    C# uses + for concatenation whereas VB.NET uses &

    C# allows operator overloading but VB.NET does not!

    The void functions may differ in syntax:

    ' VB .NET // C#Public Sub Foo() public void Foo()

    '. { . }End Sub

  • 8/8/2019 1[1].Philosophy of

    26/35

    S. Nandagopalan, B I T 26

    CLS Compliance

    C# Type CLS Compliance

    byte Yes

    sbyte No

    short Yes

    int Yes

    long Yes

    ulong No

    float Yes

    double Yes

    object Yes

    string Yes

    char Yes

    bool Yes

  • 8/8/2019 1[1].Philosophy of

    27/35

    S. Nandagopalan, B I T 27

    Example

    public class Calc{

    // CLS compliantpublic int Add(int x, int y){ return x + y; }

    // Not CLS compliantpublic ulong Add(ulong x, ulong y){ return x + y; }

    }

    Once a method is CLS compliant, then all the .NETaware languages can interact with thatimplementation

  • 8/8/2019 1[1].Philosophy of

    28/35

    CLR .NETSource

    Code

    Base ClassLibraries

    (mscorlib.dll)

    .NET Execution Engine

    Class Loader

    Jitter

    Platform

    Specific code

    Execute

    .NET Compiler

    DLL or EXE

    (CIL)

    mscoree.dll

    mscoree.dllMicroSoft Common

    Object Runtime Execution Engine

  • 8/8/2019 1[1].Philosophy of

    29/35

    S. Nandagopalan, B I T 29

    .NET Namespace

    MFC, Java, VB 6.0 have predefinedset of classes; C# doesn't

    C# uses namespace concept

    Any language targeting the .NETruntime makes use of the same

    namespaces and same types as C#

    System is the root namespace

  • 8/8/2019 1[1].Philosophy of

    30/35

    S. Nandagopalan, B I T 30

    Example in C#

    using System;

    public Class MyApp

    {

    public static void Main()

    {

    Console.WriteLine("Hello World");

    }

    }

    System Namespace

    Console class inSystem Namespace

  • 8/8/2019 1[1].Philosophy of

    31/35

    S. Nandagopalan, B I T 31

    Example in VB .NET

    Imports System

    Public Module MyApp

    Sub Main()Console.WriteLine("Hello World")

    End Sub

    End Module

  • 8/8/2019 1[1].Philosophy of

    32/35

    S. Nandagopalan, B I T 32

    Example in Managed C++

    #using

    using namespace System;

    void Main(){

    Console::WriteLine("Hello World");

    }

  • 8/8/2019 1[1].Philosophy of

    33/35

    S. Nandagopalan, B I T 33

    Sample .NET namespaces

    System primitive types, garbagecollection, etc

    System.Collections Container objects: ArrayList,Queue, etc.

    System.DataSystem.Data.Common

    System.Data.OleDb

    System.Data.SqlClient

    For Database manipulations ADO.NET

    System.IO file IO, buffering, etc.

    System.Drawing

    System.Drawing.2D

    GDI+ primitives, bitmaps, fonts,icons, etc.

    System.Threading Threads

  • 8/8/2019 1[1].Philosophy of

    34/35

    S. Nandagopalan, B I T 34

    Demo

    Console Application Windows Application

    Graphics

  • 8/8/2019 1[1].Philosophy of

    35/35

    S. Nandagopalan, B I T 35

    End of Chapter 1Chapter 1