Background and general information

 

Lesson 1

.NET Framework Overview

  • Managed, type-safe environment.
  • Manages execution of application
    • Allocates memory for data and instructions
    • Controls access permissions
    • Initiates + controls execution
    • Reallocates resources no longer required
  • 2 main components
    • Common Language Runtime (CLR). Manages code execution, e.g.
      • compilation
      • memory allocation
      • thread management
      • garbage collection

The common type system (CTS) provides

      • strict type safety
      • enforces code access security
    • .NET framework class library
      • collection useful reusable object-oriented types that integrate with CLR
      • Extensible -> seamlessly integrate apps with .NET framework

 

 

.NET and Languages

  • Cross-language compatibility – VB.Net app may access C# DLL
  • OO inheritance compatibility – VB.Net class may derive from C# class
  • Compatibility through CLR
    • .NET app compiled to Microsoft Intermediate Language (MSIL or IL) which is interpreted by CLR
    • Common Language Specification specifies standards for .NET compilers -> ensures app can interact with framework and each other
  • CTS ensures type compatibility, VB integer and C# int represented in IL as System.Int32
  • Shipped languages; VB.NET, C#, C++ with managed extensions, Jscript
  • 3rd party; Fortran, Cobol, Pearl

 

.NET App Structure

  • Primary unit = assembly (self describing code, resources, data)
  • App = 1+ assemblies
  • Assembly manifest describes assembly:
    • Identity – name, version
    • Types
    • Other assemblies required
    • Security requirements
  • Assembly manifest lives within 1 assembly module or own separate file
  • Assembly contains 1+ modules containing code and metadata describing code
  • Modules contain types - template describing data encapsulation (fields, properties) and functionality (methods)
    • reference type = class
    • value type = structure

 

Compilation and Execution

  • 1 assembly contains executable file identified as apps entry point
  • Execution start -> first assembly loaded
  • CLR examines manifest
    • Determines requirements to run program
    • Compares requested permissions to systems security policy, only executes if requested permissions supported
  • First bit of code to execute is loaded by CLR and converted to native binary from IL by the Just-In-Time (JIT) compiler
  • Compiled code is executed and held in memory – each portion of code compiled only once during app execution

 

Lesson 2

.NET Base Class Library

  • Collection OO types and interfaces providing object models and services
  • Most of its types are extensible
  • Organised into hierarchical namespaces (logical grouping of types performing related functions)
  • Root namespace = system, additional namespaces accessed using ‘.’:

System

System.Data

System.Data.SqlClient

 

Namesapce

Description

System

Root. Contains low-level and primitive types

System.Collections

Types for arrays, lists, queues and stacks. Abstract classes to implement own collections

System.ComponentModel

Component creation and containment – attributes, type converters, license providers

System.Data

Database access and manipulations

System.Data.Common

Classes shared by .NET managed data providers

System.Data.OleDb

Managed provider for OLE DB

System.Data.SqlClient

Managed provider for SQL server

System.Drawing

GDI+ functionality

System.IO

File system IO

System.Math

Roots, trig, etc.

System.Reflection

Obtain info on and dynamically create types at run-time

System.Security

Permissions, cryptography and code access

System.Threading

Supports multi-threaded apps

System.Windows.Forms

Supports standard windows apps (forms and consoles)

Table 1 Representative .NET Namespaces

Reference and Value Types

  • Primary difference = variable data access
  • Value types held on stack. Memory reclaimed when variable goes out of scope
    • Examples: primitive types (int, bool, char), enums, user defined structs
  • Reference types – object on heap, variable pointing to that data on stack. If object left with no stack references then subject to garbage collection
    • Examples: user defined classes, interfaces, delegates, arrays

 

Using .NET Framework in App

  • Values
    • Create by declaring type and name

int myInteger;

    • Assign

myInteger = 42;

  • Reference
    • Creating = 2 steps. First declare variable of type

System.Windows.Forms.Form myForm;

    • Next instantiate

myForm = new System.Windows.Forms.Form

    • Instantiations calls constructor to initialise object (parms, if appropriate passed in parenthesis)

Widget mywidget;

myWidget = new Widget(“doodah”);

  • Both types must be initialised before use. Use of un-initialised variable = error)

 

Using Statement

  • Removes need to fully qualify type
  • Appears before other namespace elements, e.g. class or struct
  • If 2 types of same name exist in 2+ imported namespaces then

                                                               i.      fully qualified name must be used to avoid conflict

                                                             ii.      use alias to choose one name to refer to another class

using myAlias = MyNamespace.Widget

myNamespace.Widget widget = new myNamespace.Widget

myAlias anotherWidget = new myAlias

 

External Libraries

·        Access 3rd party libraries:

1.      From Solution Explorer right click projects References node

2.      Choose Add Reference. Add Reference dialog box appears

3.      Choose tab for library to reference. .NET libraries are on .NET tab. COM libraries on COM tab. Local Visual Studio projects on Projects tab

4.      Load library and double click to add to Selected components box. Click OK to confirm reference

 

Lesson 3

Classes and Structures

  • Class = template for object describing type and kind of data held
  • Member = item belonging to class
    • Fields and properties = data about object
    • Method = something object can do
    • Events = something that happens to object

 

public class Widget

{

     // Class implementation goes here

 

public int Spin; // Member field

 

private class Thing

{

     // Nested class code

}

}

 

public struct Vector

{

// Structure implementation goes here

 

public int Add( int first, int second)

{

     // Method implementation

     return first + second;

}

}

 

  • Nested types = type within type, e.g. AccountManager class manages Account objects. Account objects can not be created independently from AccountManager. Make Account class a nested class inside AccountManager
  • Structure = stack based. Light and fast access, but not designed to store large amounts of data. Use for transient data
  • Class = heap based. Slower to access but can hold more, potentially persistant data

 

Lesson 4

Methods

  • Not executed until called, except for:
    • Main method = on initiation of program execution
    • Constructor and destructor on object creation / destruction
  • Variables declared in method have method scope. Once method execution completes, variable destroyed (gone out of scope). Variables within sub-blocks, e.g. for-next loop, accessible only within sub-block

 

Parameters

  • Methods take 0+ parms
  • Parms have method scope
  • Default = pass by value (i.e. copy of data in variable made and passed to method)
  • Pass by reference (i.e. reference to actual object is passed and any changes made in method are reflected in caller) using ref keyword:

 

public int Demo2( int p1, ref int p2)

{

}

 

  • Output parameters pass value back from method (allow method to return more than one value). Indicated with out keyword. Always passed by reference and do not need initialisation

 

public void aWord( out sring Word )

{

Word = “Mambo”;

}

 

Constructors and Destructors

  • Constructor
    • run when instance of type created.
    • has same name as class
    • initialises class / structure data
    • does not return value
    • can be overridden
  • Destructor
    • last method run by class
    • has same name as class preceded by ~
    • cleans up when class destroyed
    • can not determine when called (due to garbage collection)

 

Lesson 5

Scope and Access Levels

  • Access Levels (public, private, internal) define type instantiation and access to members
  • Use to encapsulate data / methods and expose functionality to outside objects
  • Precede member declaration affects its scope (i.e. who can access it)
  • Precede type declaration determines scope and how type instantiated
  • Default access level for members  = private
  • Default access level for type = public

 

Access Modifier

Affect

public

Accessed from anywhere

private

Accessed from members within type defining it

internal

Accessed from within assembly

protected

Accessed from members within type or types inheriting from the owning type

protected internal

Accessed from within assembly or from types inheriting from the owning type

Table 2 Access Modifiers

·        Good practice to assign nested types private access modifier. Can assign other modifiers but behaviour will never be greater than access modifier of containing type

 

Static Members

·        Can have class members common to all instances of class via static keyword

 

public class Demo

{

public static int MyField;

}

·        Methods can also be shared. Shared methods belong to the type itself, not its instance. Can not access instance data from objects – only static variables, variables declared within method or parameters

·        Access static members via class name, not instance name.

 

Demo Object1 = new Demo();

Object1.MyField = 15; // Incorrect

Demo.MyField = 15; // Correct

 

·        Do not need to instantiate type before accessing static members

 

Lesson 6

Garbage Collection

·        .NET memory management scheme called garbage collection (GC)

·        Memory from object no longer required reclaimed without application action

·        When variable goes out of scope object it refers to is no longer referenced. GC continuously traces the reference tree in background to identify objects no longer referenced. When found, object deleted and memory reclaimed

·        GC = low priority thread. When memory limited GC thread priority boosted

·        Aims to maximise app performance and provide less error prone environment.

·        Nondeterministic

o       no control over when destructor called

o       do not place time dependent code in destructor

o       implement Dispose() to free expensive resources when no longer needed

 

Circular References

·        GC manages circular references (two object each referring to one-another)

·        If two objects only refer to one-another and have no other referees they will be marked for GC

Tuition

home
home
tuition
index
Last updated: 26th December 2006.
copyright © 2000 Greystoke Systems Ltd.
Web address: http://www.gsys.biz/Documents/Services/Tuition/MCP/70-316/ApplicationDeployment.htm