introduction to .netintroduction to - napierbill/agilent/introductiontodotnet... · introduction to...

126
W.Buchanan (1) Introduction to .NET Introduction to .NET Andrew Cumming, SoC Bill Buchanan, SoC

Upload: ngotu

Post on 14-Jul-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (1)

Intro

duct

ion

to .N

ET

Introduction to .NETIntroduction to .NET

Andrew Cumming, SoC

Bill Buchanan, SoC

Page 2: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (2)

Intro

duct

ion

to .N

ET

Course Outline (Day 1 to 3)

Course Outline• Day 1: Morning – Introduction to Object-Orientation, Introduction

to .NET, Overview of .NET Framework, .NET Components. C#.• Day 1: Afternoon – Data Types, Variables, Converting Data

Types, if, for, while, File Handling• Day 2: Morning – Arrays, System.Collections, ArrayLists,

Hashtables.• Day 2: Afternoon – Reference-Type Variables, Common

Reference Types, Object Hierarchy, Namespaces, Debugging.• Day 3: Morning – Methods, Exceptions, Namespaces, Modules/

Assemblies, Non-visual Components, Regular Expressions, Intellisense, Help, Delegates, Events.

• Day 3: Afternoon – Cerebus, Limit-check components, Power meter class.

Page 3: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (3)

Intro

duct

ion

to .N

ET

Course Outline (Day 4 and 5)

Course Outline• Day 4: Morning – Classes, Encapsulation, Object-Orientation,

Classes, Sealed Classes, Interfaces, Abstract Classes.• Day 4: Afternoon – Using Constructors, Initializing Memory,

Objects and Memory, Resource Management.• Day 5: Morning – Cerebus: 1mW Test.• Day 5: Afternoon – Operators, Operator Overloading, Attributes,

Custom Attributes, Retrieving Attribute Values.

Page 4: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (4)

Intro

duct

ion

to .N

ET

Introduction

Module 1• Introduction to Object-orientation.• .NET Framework.• Visual Studio Environment.• Benefits of C# over VB.• .NET Components.• .NET Languages.

Page 5: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (5)

Intro

duct

ion

to .N

ET

Introduction

An Introduction to Object-Orientation

Bill Buchanan

Page 6: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (6)

Intro

duct

ion

to .N

ET

Some Cups

Page 7: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (7)

Intro

duct

ion

to .N

ET

A Quick Introduction to Object-Orientation

Parameter Cup 1 Cup 2 Cup3Shape (Standard/Square/Mug) Standard Square MugColour (Red/Blue/Green) Blue Red GreenSize (Small/Medium/Large) Small Large SmallTransparency (0 to 100%) 100% 50% 25%Handle type (Small/Large) Small Small Large

In object-orientation:A collection of parameters defines a class.

Class for the cup is thus: Shape, Colour,Size, Transparency, HandleType.

In object-orientation: Objects arecreated from classes.

Page 8: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (8)

Intro

duct

ion

to .N

ET

Example C# Program using Object-Orientation

using System;

namespace ConsoleApplication2{

public class Cup {

public string Shape;public string Colour;public string Size;public int Transparency;public string Handle;

public void DisplayCup(){

System.Console.WriteLine("Cup is {0}, {1}", Colour, Handle);}

}

class Class1{

static void Main(string[] args){

Cup cup = new Cup();cup.Colour = "Red";cup.Handle = "Small";cup.DisplayCup();System.Console.ReadLine();

}}

}

Class definitions

Available variables(properties)

Method

Create new objectSet propertiesApply method

Page 9: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (9)

Intro

duct

ion

to .N

ET

Another example

using System;

namespace ConsoleApplication2{

public class Circuit {

public double Parallel(double r1, double r2){

return((r1*r2)/(r1+r2));}public double Series(double r1, double r2){

return(r1+r2);}

}

class Class1{

static void Main(string[] args){

double v1=100,v2=100;double res;

Circuit cir = new Circuit();

res=cir.Parallel(v1,v2);System.Console.WriteLine("Parallel resistance is {0} ohms",res);

res=cir.Series(100,100);System.Console.WriteLine("Series resistance is {0} ohms",res);

System.Console.ReadLine();}

}}

Class definitions

Page 10: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (10)

Intro

duct

ion

to .N

ET

Another example

using System;namespace ConsoleApplication2{

public class Complex{

public double real;public double imag;

public double mag(){

return (Math.Sqrt(real*real+imag*imag));}public double angle(){

return (Math.Atan(imag/real)*180/Math.PI); }

}class Class1{

static void Main(string[] args){

string str;double mag,angle;Complex r = new Complex();

System.Console.Write("Enter real value >>");str=System.Console.ReadLine();r.real = Convert.ToInt32(str);

System.Console.Write("Enter imag value >>");str=System.Console.ReadLine();r.imag = Convert.ToInt32(str);

mag=r.mag();angle=r.angle();

System.Console.WriteLine("Mag is {0} and angle is {1}",mag,angle);System.Console.ReadLine();

}}

}

⎟⎠⎞

⎜⎝⎛=

+=

+=

xyz

yxz

yxz

1

22

tan

j

Page 11: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (11)

Intro

duct

ion

to .N

ET

Introduction

.NET Framework

Bill Buchanan

Page 12: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (12)

Intro

duct

ion

to .N

ET

Traditional Windows Model

API(Application

ProgrammingInterface)

API(Application

ProgrammingInterface)

StaticLibrariesStatic

Libraries

CompilationCompilation LinkerLinkerSourceCode(C/C++/Delphi/VB)

gdi32.dllgdi32.dll ole32.dllole32.dll

OBJ file

EXE ProgramEXE

Program

Page 13: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (13)

Intro

duct

ion

to .N

ET

Traditional Windows Model

API(Application

ProgrammingInterface)

API(Application

ProgrammingInterface)

gdi32.dllgdi32.dll ole32.dllole32.dll

Creating windows. Windows support functions.Message processing.Menus.Resources.Dialog boxes.User input functions.Memory management.GDI (graphical device interface).Bitmaps, icons and metafiles.Printing and text output.Painting and drawing.File I/O.Clipboard. Support for public and private clipboards.Registry. Support for functions which access the Registry.Initialization files. Support for functions which access INI files.System information.String manipulation.Timers.Processes and threads.Error and exception processing.MDI (multiple document interface).Help files.File compression/decompression.DLLs.Network support (NetBios and Windows sockets 1.1 APIs).Multimedia support (sound APIs).OLE and DDE (dynamic data exchange).TrueType fonts.

EXE ProgramEXE

Program

Example C++ code calling an API#include <windows.h>int WINAPI WinMain(HINSTANCE hInstance,

HINSTANCE hPrev, LPSTR lpCmd, int nShow){char msg[128];

wsprintf(msg, "My name is Fred");MessageBox(GetFocus(), msg, "My first Window", MB_OK | MB_ICONINFORMATION);return(0);

}

Example C++ code calling an API#include <windows.h>int WINAPI WinMain(HINSTANCE hInstance,

HINSTANCE hPrev, LPSTR lpCmd, int nShow){char msg[128];

wsprintf(msg, "My name is Fred");MessageBox(GetFocus(), msg, "My first Window", MB_OK | MB_ICONINFORMATION);return(0);

}

Page 14: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (14)

Intro

duct

ion

to .N

ET

So what’s wrong with the old model?

EXE ProgramEXE

Program

Lack of support for different hardwarePrograms were compiled to x86 code.

Weak integration with Internet/WWWCode and WWW code where seenas separate entities.

Lack of security IntegrationMost programs where written with little careabout security

Code.asp<%

val1 = 10val2 = 20result = Cstr(val1) + Cstr(val2)response.write "<BR>Value is " & resultresult = val1 + val2response.write "<BR>Value is " & result

%>

Code.asp<%

val1 = 10val2 = 20result = Cstr(val1) + Cstr(val2)response.write "<BR>Value is " & resultresult = val1 + val2response.write "<BR>Value is " & result

%>

Difficult to integratedifferent languages

Poor version control for system components

Page 15: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (15)

Intro

duct

ion

to .N

ET

New .NET Platform

API(Application

ProgrammingInterface)

API(Application

ProgrammingInterface)

COM+(Distributed

Components)

COM+(Distributed

Components)

ASPWWW

Services

ASPWWW

Services

.NET Platform

ExecutableProgram

ExecutableProgram

VB .NETVB .NET C#C#

Visual Studio

VBVB

ASPASP

ASPASP

CC

C++C++

VBAVBA

Page 16: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (16)

Intro

duct

ion

to .N

ET

How are languages integrated?

CTS (Common

TypeSystem)

CTS (Common

TypeSystem)

CLS(CommonLanguage

Specification)

CLS(CommonLanguage

Specification)

VB .NETVB .NET C#C#

CompilerCompiler

FCL(Framework

Class Library)

FCL(Framework

Class Library)

CLR (Common Language Runtime)Allows the programs to run – similar to the Java Virtual Machine (JVM)

CLR (Common Language Runtime)Allows the programs to run – similar to the Java Virtual Machine (JVM)

Webcomponent

Webcomponent

MSIL (Microsoft Intermediate Language)

Page 17: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (17)

Intro

duct

ion

to .N

ET

.NET Framework Files

Volume in drive C has no label.Volume Serial Number is 1A83-0D9D

Directory of C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322

21/02/2003 08:24 7,680 Accessibility.dll21/02/2003 06:00 98,304 alink.dll20/02/2003 20:19 24,576 aspnet_filter.dll20/02/2003 20:19 253,952 aspnet_isapi.dll20/02/2003 20:19 40,960 aspnet_rc.dll20/02/2003 20:09 77,824 CORPerfMonExt.dll21/02/2003 11:21 626,688 cscomp.dll21/02/2003 08:24 12,288 cscompmgd.dll21/02/2003 08:24 33,792 CustomMarshalers.dll29/07/2002 12:11 219,136 c_g18030.dll21/02/2003 11:21 524,288 diasymreader.dll19/03/2003 02:52 245,760 envdte.dll20/02/2003 20:16 798,720 EventLogMessages.dll20/02/2003 20:06 282,624 fusion.dll21/02/2003 08:24 7,168 IEExecRemote.dll21/02/2003 08:24 32,768 IEHost.dll21/02/2003 08:24 4,608 IIEHost.dll21/02/2003 08:25 1,564,672 mscorcfg.dll20/02/2003 20:09 77,824 mscordbc.dll20/02/2003 20:09 233,472 mscordbi.dll20/02/2003 20:09 86,016 mscorie.dll20/02/2003 20:06 311,296 mscorjit.dll20/02/2003 20:09 98,304 mscorld.dll21/02/2003 08:26 2,088,960 mscorlib.dll20/02/2003 19:43 131,072 mscormmc.dll20/02/2003 20:06 65,536 mscorpe.dll20/02/2003 20:09 143,360 mscorrc.dll20/02/2003 20:09 81,920 mscorsec.dll20/02/2003 20:09 77,824 mscorsn.dll20/02/2003 20:07 2,494,464 mscorsvr.dll20/02/2003 20:09 9,216 mscortim.dll20/02/2003 20:08 2,482,176 mscorwks.dll21/02/2003 05:42 348,160 msvcr71.dll18/03/2003 20:03 544,768 msvcr71d.dll20/02/2003 20:18 20,480 mtxoci8.dll19/03/2003 02:50 196,608 office.dll20/02/2003 20:09 90,112 PerfCounter.dll21/02/2003 08:26 32,768 RegCode.dll

Contains most of the codethat is called by the program

ProgramProgram

Mscorlib.dllArrays,File I/O,System,Security

Mscorlib.dllArrays,File I/O,System,Security

Page 18: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (18)

Intro

duct

ion

to .N

ET

Visual Studio Environment

Visual Studio EnvironmentBill Buchanan

Page 19: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (19)

Intro

duct

ion

to .N

ET

Folder where the project is stored.

Name of the folderWhich contains the Project filesthe project is stored.

Page 20: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (20)

Intro

duct

ion

to .N

ET

Class file (.cs)

Code and

Text Editor

Editor's statement

completion

Solution explorer

Page 21: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (21)

Intro

duct

ion

to .N

ET

Example of Auto-Complete

Page 22: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (22)

Intro

duct

ion

to .N

ET

Page 23: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (23)

Intro

duct

ion

to .N

ET

Solution File (.sln)

Module01_01.slnMicrosoft Visual Studio Solution File, Format Version 8.00Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Module01_01",

"Module01_01.csproj", "{1EA384DD-927E-4B94-ABB8-BA08DFAE5AB3}"ProjectSection(ProjectDependencies) = postProjectEndProjectSection

EndProjectGlobal

GlobalSection(SolutionConfiguration) = preSolutionDebug = DebugRelease = Release

EndGlobalSectionGlobalSection(ProjectConfiguration) = postSolution

{1EA384DD-927E-4B94-ABB8-BA08DFAE5AB3}.Debug.ActiveCfg = Debug|.NET

{1EA384DD-927E-4B94-ABB8-BA08DFAE5AB3}.Debug.Build.0 = Debug|.NET

{1EA384DD-927E-4B94-ABB8-BA08DFAE5AB3}.Release.ActiveCfg = Release|.NET

{1EA384DD-927E-4B94-ABB8-BA08DFAE5AB3}.Release.Build.0 = Release|.NETEndGlobalSectionGlobalSection(ExtensibilityGlobals) = postSolutionEndGlobalSectionGlobalSection(ExtensibilityAddIns) = postSolutionEndGlobalSection

EndGlobal

Module01_01.slnMicrosoft Visual Studio Solution File, Format Version 8.00Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Module01_01",

"Module01_01.csproj", "{1EA384DD-927E-4B94-ABB8-BA08DFAE5AB3}"ProjectSection(ProjectDependencies) = postProjectEndProjectSection

EndProjectGlobal

GlobalSection(SolutionConfiguration) = preSolutionDebug = DebugRelease = Release

EndGlobalSectionGlobalSection(ProjectConfiguration) = postSolution

{1EA384DD-927E-4B94-ABB8-BA08DFAE5AB3}.Debug.ActiveCfg = Debug|.NET

{1EA384DD-927E-4B94-ABB8-BA08DFAE5AB3}.Debug.Build.0 = Debug|.NET

{1EA384DD-927E-4B94-ABB8-BA08DFAE5AB3}.Release.ActiveCfg = Release|.NET

{1EA384DD-927E-4B94-ABB8-BA08DFAE5AB3}.Release.Build.0 = Release|.NETEndGlobalSectionGlobalSection(ExtensibilityGlobals) = postSolutionEndGlobalSectionGlobalSection(ExtensibilityAddIns) = postSolutionEndGlobalSection

EndGlobal

Page 24: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (24)

Intro

duct

ion

to .N

ET

CSProf file

Module01_01.csprof<VisualStudioProject>

<CSHARPProjectType = "Local"ProductVersion = "7.10.3077"SchemaVersion = "2.0"

><Build>

<SettingsApplicationIcon = "App.ico"AssemblyKeyContainerName = ""AssemblyName = "Module01_01"AssemblyOriginatorKeyFile = ""DefaultClientScript = "JScript"DefaultHTMLPageLayout = "Grid"DefaultTargetSchema = "IE50"DelaySign = "false" OutputType = "Exe"PreBuildEvent = "" PostBuildEvent = ""RootNamespace = "Module01_01"RunPostBuildEvent = "OnBuildSuccess“ ><Config

Name = "Debug"AllowUnsafeBlocks = "false"

../><Config

Name = "Release"AllowUnsafeBlocks = "false"BaseAddress = "285212672"

../>

</Settings><References>

</Build><Files>

<Include><File RelPath = "App.ico“ BuildAction = "Content" /><File RelPath = "AssemblyInfo.cs" SubType = "Code"

BuildAction = "Compile" /><File

RelPath = "Class1.cs" SubType = "Code"BuildAction = "Compile" />

</Include></Files>

</CSHARP></VisualStudioProject>

Module01_01.csprof<VisualStudioProject>

<CSHARPProjectType = "Local"ProductVersion = "7.10.3077"SchemaVersion = "2.0"

><Build>

<SettingsApplicationIcon = "App.ico"AssemblyKeyContainerName = ""AssemblyName = "Module01_01"AssemblyOriginatorKeyFile = ""DefaultClientScript = "JScript"DefaultHTMLPageLayout = "Grid"DefaultTargetSchema = "IE50"DelaySign = "false" OutputType = "Exe"PreBuildEvent = "" PostBuildEvent = ""RootNamespace = "Module01_01"RunPostBuildEvent = "OnBuildSuccess“ ><Config

Name = "Debug"AllowUnsafeBlocks = "false"

../><Config

Name = "Release"AllowUnsafeBlocks = "false"BaseAddress = "285212672"

../>

</Settings><References>

</Build><Files>

<Include><File RelPath = "App.ico“ BuildAction = "Content" /><File RelPath = "AssemblyInfo.cs" SubType = "Code"

BuildAction = "Compile" /><File

RelPath = "Class1.cs" SubType = "Code"BuildAction = "Compile" />

</Include></Files>

</CSHARP></VisualStudioProject>

Page 25: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (25)

Intro

duct

ion

to .N

ET

Solution File (.sln)

Class1.csusing System;

namespace Module01_01{

/// <summary>/// Summary description for Class1./// </summary>

class Class1{/// <summary>/// The main entry point for the application./// </summary>static void Main(string[] args){//// TODO: Add code to start application here

System.Console.WriteLine("AgilentCourse");System.Console.ReadLine();}

}}

Class1.csusing System;

namespace Module01_01{

/// <summary>/// Summary description for Class1./// </summary>

class Class1{/// <summary>/// The main entry point for the application./// </summary>static void Main(string[] args){//// TODO: Add code to start application here

System.Console.WriteLine("AgilentCourse");System.Console.ReadLine();}

}}

Page 26: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (26)

Intro

duct

ion

to .N

ET

ICO file

Page 27: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (27)

Intro

duct

ion

to .N

ET

Solution File (.sln)

AssemblyInfo.csusing System.Reflection;using System.Runtime.CompilerServices;

//// General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information// associated with an assembly.//[assembly: AssemblyTitle("")][assembly: AssemblyDescription("")][assembly: AssemblyConfiguration("")][assembly: AssemblyCompany("")][assembly: AssemblyProduct("")][assembly: AssemblyCopyright("")][assembly: AssemblyTrademark("")][assembly: AssemblyCulture("")]

//// Version information for an assembly consists of the following four values://// Major Version// Minor Version // Build Number// Revision//// You can specify all the values or you can default the Revision and Build

Numbers // by using the '*' as shown below:

[assembly: AssemblyVersion("1.0.*")]

[assembly: AssemblyDelaySign(false)][assembly: AssemblyKeyFile("")][assembly: AssemblyKeyName("")]}

AssemblyInfo.csusing System.Reflection;using System.Runtime.CompilerServices;

//// General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information// associated with an assembly.//[assembly: AssemblyTitle("")][assembly: AssemblyDescription("")][assembly: AssemblyConfiguration("")][assembly: AssemblyCompany("")][assembly: AssemblyProduct("")][assembly: AssemblyCopyright("")][assembly: AssemblyTrademark("")][assembly: AssemblyCulture("")]

//// Version information for an assembly consists of the following four values://// Major Version// Minor Version // Build Number// Revision//// You can specify all the values or you can default the Revision and Build

Numbers // by using the '*' as shown below:

[assembly: AssemblyVersion("1.0.*")]

[assembly: AssemblyDelaySign(false)][assembly: AssemblyKeyFile("")][assembly: AssemblyKeyName("")]}

.NET uses assembly to represent a single unit. An assembly is a collection of files that appear as a single unit, such as a single DLL or an EXE.

Page 28: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (28)

Intro

duct

ion

to .N

ET

Types

Types

Namespace: ConsoleAppplication2

Page 29: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (29)

Intro

duct

ion

to .N

ET

Members

Methods Variable

Page 30: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (30)

Intro

duct

ion

to .N

ET

Property

Page 31: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (31)

Intro

duct

ion

to .N

ET

Object Browser

Page 32: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (32)

Intro

duct

ion

to .N

ET

System.Collections

Page 33: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (33)

Intro

duct

ion

to .N

ET

.NET Languages

.NET LanguagesWhat are the languages?

Page 34: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (34)

Intro

duct

ion

to .N

ET

Example of C# and VB.NET Code

‘ VB.NET Code Dim j As Integer

Dim prime As BooleanDim i As Integer

For i = 1 To 100prime = True

For j = 2 To (i / 2)If ((i Mod j) = 0) Then

prime = FalseEnd If

Next jIf (prime = True) Then

TextBox1.Text = TextBox1.Text & "," & Str(i)End If

Next i

‘ VB.NET Code Dim j As Integer

Dim prime As BooleanDim i As Integer

For i = 1 To 100prime = True

For j = 2 To (i / 2)If ((i Mod j) = 0) Then

prime = FalseEnd If

Next jIf (prime = True) Then

TextBox1.Text = TextBox1.Text & "," & Str(i)End If

Next i

// C# Codeint i, j;bool prime;

for (i=0;i<100;i++){

prime = true;

for (j=2;j<=i/2;j++){

if ((i%j)==0) prime=false;}if (prime==true) textBox1.Text+=" " + Convert.ToString(i);

}

// C# Codeint i, j;bool prime;

for (i=0;i<100;i++){

prime = true;

for (j=2;j<=i/2;j++){

if ((i%j)==0) prime=false;}if (prime==true) textBox1.Text+=" " + Convert.ToString(i);

}

Page 35: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (35)

Intro

duct

ion

to .N

ET

Example VB.NET Code

Public Class Form1Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()MyBase.New()

End Sub'Form overrides dispose to clean up the component list.Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing ThenIf Not (components Is Nothing) Then

components.Dispose()End If

End IfMyBase.Dispose(disposing)

End Sub'Required by the Windows Form DesignerPrivate components As System.ComponentModel.IContainer

Friend WithEvents TextBox1 As System.Windows.Forms.TextBoxFriend WithEvents Button1 As System.Windows.Forms.Button<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.TextBox1 = New System.Windows.Forms.TextBoxMe.Button1 = New System.Windows.Forms.ButtonMe.SuspendLayout()''TextBox1'Me.TextBox1.Location = New System.Drawing.Point(24, 16)Me.TextBox1.Multiline = TrueMe.TextBox1.Name = "TextBox1"Me.TextBox1.Size = New System.Drawing.Size(200, 168)Me.TextBox1.TabIndex = 0Me.TextBox1.Text = ""''Button1'Me.Button1.Location = New System.Drawing.Point(200, 192)Me.Button1.Name = "Button1"Me.Button1.Size = New System.Drawing.Size(80, 56)Me.Button1.TabIndex = 1Me.Button1.Text = "E&xit"''Form1'Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)Me.ClientSize = New System.Drawing.Size(292, 266)Me.Controls.Add(Me.Button1)Me.Controls.Add(Me.TextBox1)Me.Name = "Form1"Me.Text = "Form1"Me.ResumeLayout(False)

End Sub

#End Region

Public Class Form1Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()MyBase.New()

End Sub'Form overrides dispose to clean up the component list.Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing ThenIf Not (components Is Nothing) Then

components.Dispose()End If

End IfMyBase.Dispose(disposing)

End Sub'Required by the Windows Form DesignerPrivate components As System.ComponentModel.IContainer

Friend WithEvents TextBox1 As System.Windows.Forms.TextBoxFriend WithEvents Button1 As System.Windows.Forms.Button<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.TextBox1 = New System.Windows.Forms.TextBoxMe.Button1 = New System.Windows.Forms.ButtonMe.SuspendLayout()''TextBox1'Me.TextBox1.Location = New System.Drawing.Point(24, 16)Me.TextBox1.Multiline = TrueMe.TextBox1.Name = "TextBox1"Me.TextBox1.Size = New System.Drawing.Size(200, 168)Me.TextBox1.TabIndex = 0Me.TextBox1.Text = ""''Button1'Me.Button1.Location = New System.Drawing.Point(200, 192)Me.Button1.Name = "Button1"Me.Button1.Size = New System.Drawing.Size(80, 56)Me.Button1.TabIndex = 1Me.Button1.Text = "E&xit"''Form1'Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)Me.ClientSize = New System.Drawing.Size(292, 266)Me.Controls.Add(Me.Button1)Me.Controls.Add(Me.TextBox1)Me.Name = "Form1"Me.Text = "Form1"Me.ResumeLayout(False)

End Sub

#End Region

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim j As IntegerDim prime As BooleanDim i As Integer

For i = 1 To 100prime = True

For j = 2 To (i / 2)If ((i Mod j) = 0) Then

prime = FalseEnd If

Next jIf (prime = True) Then

TextBox1.Text = TextBox1.Text & "," & Str(i)End If

Next i

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Application.Exit()End Sub

End Class

Page 36: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (36)

Intro

duct

ion

to .N

ET

Example C# code

using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;

namespace WindowsApplication1{

public class Form1 : System.Windows.Forms.Form{

private System.Windows.Forms.TextBox textBox1;private System.Windows.Forms.Button button1;/// <summary>/// Required designer variable./// </summary>private System.ComponentModel.Container components = null;

public Form1(){

InitializeComponent();

}

protected override void Dispose( bool disposing ){

if( disposing ){

if (components != null) {

components.Dispose();}

}base.Dispose( disposing );

}

#region Windows Form Designer generated codeprivate void InitializeComponent(){

this.textBox1 = new System.Windows.Forms.TextBox();this.button1 = new System.Windows.Forms.Button();this.SuspendLayout();// // textBox1// this.textBox1.Location = new System.Drawing.Point(24, 16);this.textBox1.Multiline = true;this.textBox1.Name = "textBox1";this.textBox1.Size = new System.Drawing.Size(184, 152);this.textBox1.TabIndex = 0;this.textBox1.Text = "";// // button1// this.button1.Location = new System.Drawing.Point(200, 208);this.button1.Name = "button1";this.button1.Size = new System.Drawing.Size(72, 48);this.button1.TabIndex = 1;this.button1.Text = "E&xit";this.button1.Click += new System.EventHandler(this.button1_Click);//

using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;

namespace WindowsApplication1{

public class Form1 : System.Windows.Forms.Form{

private System.Windows.Forms.TextBox textBox1;private System.Windows.Forms.Button button1;/// <summary>/// Required designer variable./// </summary>private System.ComponentModel.Container components = null;

public Form1(){

InitializeComponent();

}

protected override void Dispose( bool disposing ){

if( disposing ){

if (components != null) {

components.Dispose();}

}base.Dispose( disposing );

}

#region Windows Form Designer generated codeprivate void InitializeComponent(){

this.textBox1 = new System.Windows.Forms.TextBox();this.button1 = new System.Windows.Forms.Button();this.SuspendLayout();// // textBox1// this.textBox1.Location = new System.Drawing.Point(24, 16);this.textBox1.Multiline = true;this.textBox1.Name = "textBox1";this.textBox1.Size = new System.Drawing.Size(184, 152);this.textBox1.TabIndex = 0;this.textBox1.Text = "";// // button1// this.button1.Location = new System.Drawing.Point(200, 208);this.button1.Name = "button1";this.button1.Size = new System.Drawing.Size(72, 48);this.button1.TabIndex = 1;this.button1.Text = "E&xit";this.button1.Click += new System.EventHandler(this.button1_Click);//

// Form1// this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);this.ClientSize = new System.Drawing.Size(292, 266);this.Controls.Add(this.button1);this.Controls.Add(this.textBox1);this.Name = "Form1";this.Text = "Form1";this.Load += new System.EventHandler(this.Form1_Load);this.ResumeLayout(false);

}#endregion/// </summary>[STAThread]static void Main() {

Application.Run(new Form1());}

private void Form1_Load(object sender, System.EventArgs e){

int i, j;bool prime;

for (i=0;i<100;i++){

prime = true;for (j=2;j<=i/2;j++){

if ((i%j)==0) prime=false;}if (prime==true) textBox1.Text+=" " + Convert.ToString(i);

}

}

private void button1_Click(object sender, System.EventArgs e){

Application.Exit();}

}}

Page 37: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (37)

Intro

duct

ion

to .N

ET

Why?

BenefitsWhy C#?

Page 38: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (38)

Intro

duct

ion

to .N

ET

C Programming … to …

#include <stdio.h>#include <math.h>int main(void){float a,b,c,real1,real2,imag;

puts("Program to determine roots of a quadratic equation");printf("Enter a,b and c >>>");scanf("%f %f %f",&a,&b,&c);printf("Equation is %.2fx*x + %.2fx + %.2f\n",a,b,c);if ((b*b)==(4*a*c)) { real1=-b/(2*a);

printf("Root is %.2f\n",real1);} else if ((b*b)>(4*a*c)) {

real1=(-b+sqrt( (b*b)-4*a*c )) /(2*a);real2=(-b-sqrt( (b*b)-4*a*c )) /(2*a);printf("Roots are %.2f, %.2f\n",real1,real2);

} else

{real1=-b/(2*a);imag=sqrt(4*a*c-b*b)/(2*a);printf("Roots are %.2f +/- j%.2f\n",real1,imag);

}return(0);

}

#include <stdio.h>#include <math.h>int main(void){float a,b,c,real1,real2,imag;

puts("Program to determine roots of a quadratic equation");printf("Enter a,b and c >>>");scanf("%f %f %f",&a,&b,&c);printf("Equation is %.2fx*x + %.2fx + %.2f\n",a,b,c);if ((b*b)==(4*a*c)) { real1=-b/(2*a);

printf("Root is %.2f\n",real1);} else if ((b*b)>(4*a*c)) {

real1=(-b+sqrt( (b*b)-4*a*c )) /(2*a);real2=(-b-sqrt( (b*b)-4*a*c )) /(2*a);printf("Roots are %.2f, %.2f\n",real1,real2);

} else

{real1=-b/(2*a);imag=sqrt(4*a*c-b*b)/(2*a);printf("Roots are %.2f +/- j%.2f\n",real1,imag);

}return(0);

}

Advantages:-Minimal language.-Standardized.-Flexible.

Disadvantages:-Weak checking for errors.-Focused on proceduresrather than data.

-Lack of support for graphics(such as Windows).

Advantages:-Minimal language.-Standardized.-Flexible.

Disadvantages:-Weak checking for errors.-Focused on proceduresrather than data.

-Lack of support for graphics(such as Windows).

CLanguage

CLanguage

Page 39: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (39)

Intro

duct

ion

to .N

ET

C Programming … to …

CLanguage

#include <iostream.h>class circuit{private:

float rtemp;public:

float parallel(float r1, float r2){

return((r1*r2)/(r1+r2));}float series(float r1, float r2){

return(r1+r2);}

};int main(void){circuit c1;float res;

res=c1.series(2000,1000);cout << "Series resistance is " << res << "ohms\n";res=c1.parallel(1000,1000);cout << "Parallel resistance is " << res << "ohms\n";return(0);

}

#include <iostream.h>class circuit{private:

float rtemp;public:

float parallel(float r1, float r2){

return((r1*r2)/(r1+r2));}float series(float r1, float r2){

return(r1+r2);}

};int main(void){circuit c1;float res;

res=c1.series(2000,1000);cout << "Series resistance is " << res << "ohms\n";res=c1.parallel(1000,1000);cout << "Parallel resistance is " << res << "ohms\n";return(0);

}

Advantages:-Standardized.-Flexible.-Object-oriented.-Improved error checking.-Improved Windows support

Disadvantages:-Still a hybrid language (C and/or C++).-Still too generic.-Lack of integration withother languages.

Advantages:-Standardized.-Flexible.-Object-oriented.-Improved error checking.-Improved Windows support

Disadvantages:-Still a hybrid language (C and/or C++).-Still too generic.-Lack of integration withother languages.

C++Language

C++Language

Object-orientation added

Page 40: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (40)

Intro

duct

ion

to .N

ET

C++Language

C Programming … to …

CLanguage

using System;

namespace ConsoleApplication2{

public class Circuit {public double Parallel(double r1, double r2){

return((r1*r2)/(r1+r2));}public double Series(double r1, double r2){

return(r1+r2);}}class Class1{static void Main(string[] args){double v1=100,v2=100;double res;

Circuit cir = new Circuit();res=cir.Parallel(v1,v2);System.Console.WriteLine("Parallel resistance is {0} ohms",res);res=cir.Series(100,100);System.Console.WriteLine("Series resistance is {0} ohms",res);System.Console.ReadLine();

}}

using System;

namespace ConsoleApplication2{

public class Circuit {public double Parallel(double r1, double r2){

return((r1*r2)/(r1+r2));}public double Series(double r1, double r2){

return(r1+r2);}}class Class1{static void Main(string[] args){double v1=100,v2=100;double res;

Circuit cir = new Circuit();res=cir.Parallel(v1,v2);System.Console.WriteLine("Parallel resistance is {0} ohms",res);res=cir.Series(100,100);System.Console.WriteLine("Series resistance is {0} ohms",res);System.Console.ReadLine();

}}

Advantages:-Fully object-oriented.-Robust.-Integrated with Windows.-Cross-platform.-Support for mobility.-Strong integration with

Disadvantages:-Massive programmingenvironment.

Advantages:-Fully object-oriented.-Robust.-Integrated with Windows.-Cross-platform.-Support for mobility.-Strong integration with

Disadvantages:-Massive programmingenvironment.

C#Language

C#Language

Windows/WWW/Java ideas

Page 41: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (41)

Intro

duct

ion

to .N

ET

Why C#?

The gap between C and VB is now closed as both provide an excellent environment for software development.

VB.NET is aimed at Microsoft Office and WWW-based Applications, as it integrates well with VBA and ASP. VB has traditionally supported unstructured code, but this has now changed.

C# is aimed at engineering applications, and allows for more flexibility, such as using pointers. There is also a great amount of code developed for many different applications, such as DSP, interfacing, and so on.

Page 42: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (42)

Intro

duct

ion

to .N

ET

Why?

Elements of a C# ProgramWhat goes where?

Page 43: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (43)

Intro

duct

ion

to .N

ET

C# Program Outline

using System;namespace ConsoleApplication2{

public class Complex{

public double real;public double imag;public int val { set {} get {} };public double mag(){

return (Math.Sqrt(real*real+imag*imag));}public double angle(){

return (Math.Atan(imag/real)*180/Math.PI); }

}class Class1{

static void Main(string[] args){

Complex r = new Complex();string str;double mag,angle;

System.Console.Write("Enter real value >> ");str=System.Console.ReadLine();r.real = Convert.ToInt32(str);System.Console.Write("Enter imag value >> ");str=System.Console.ReadLine();r.imag = Convert.ToInt32(str);mag=r.mag();angle=r.angle();System.Console.WriteLine("Mag is {0} and angle is {1}",mag,angle);System.Console.ReadLine();

}}

}

Main(). This is the entry point into the program, and defines the start and end of the program. It must be declared inside a class, and must be static.

Main(). This is the entry point into the program, and defines the start and end of the program. It must be declared inside a class, and must be static.

using. Imports types defined in other namespaces.

using. Imports types defined in other namespaces.

namespace. Defines a uniquename for the objects. In this case the objects would have the name of:ConsoleApplications2.Complex()ConsoleApplicaitons2.Class1()

namespace. Defines a uniquename for the objects. In this case the objects would have the name of:ConsoleApplications2.Complex()ConsoleApplicaitons2.Class1()

Page 44: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (44)

Intro

duct

ion

to .N

ET

C# Program Outline

using System;namespace ConsoleApplication2{

public class Complex{

public double real;public double imag;public int val { set {} get {} };public double mag(){

return (Math.Sqrt(real*real+imag*imag));}public double angle(){

return (Math.Atan(imag/real)*180/Math.PI); }

}class Class1{

static void Main(string[] args){

Complex r = new ConsoleApplication2.Complex();string str;double mag,angle;

System.Console.Write("Enter real value >> ");str=System.Console.ReadLine();r.real = Convert.ToInt32(str);System.Console.Write("Enter imag value >> ");str=System.Console.ReadLine();r.imag = Convert.ToInt32(str);mag=r.mag();angle=r.angle();System.Console.WriteLine("Mag is {0} and angle is {1}",mag,angle);System.Console.ReadLine();

}}

}

namespace. Defines a uniquename for the objects. In this case the objects would have the name of:ConsoleApplications2.Complex()ConsoleApplicaitons2.Class1()

namespace. Defines a uniquename for the objects. In this case the objects would have the name of:ConsoleApplications2.Complex()ConsoleApplicaitons2.Class1()

Page 45: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (45)

Intro

duct

ion

to .N

ET

System.Console.

System.Console.Write("Enter real value >> ");using. Imports types defined in other namespaces.

using. Imports types defined in other namespaces.

Page 46: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (46)

Intro

duct

ion

to .N

ET

System.Math.

Math.Sqrt(real*real+imag*imag)using. Imports types defined in other namespaces.

using. Imports types defined in other namespaces.

Page 47: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (47)

Intro

duct

ion

to .N

ET

Typical namespaces

System: Array, Boolean, Byte, Char, Convert, DateTime, Double, Enum, Int16, Int32, Int 64, Math, Random, String, VoidSystem.Collections:ArrayList, BitArray, Hashtable, Queue, Stack.System.IO:BinaryReader, BinaryWriter, File, Stream, StreamWriter, StreamReader

uses System;uses System.Collections;uses System.IO

Page 48: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (48)

Intro

duct

ion

to .N

ET

Why?

.NET ComponentsWhat are components?

Page 49: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (49)

Intro

duct

ion

to .N

ET

New .NET Platform

MS Outlookcomponent

MS Outlookcomponent

MS IEcomponent

MS IEcomponent

MS WordcomponentMS Word

component

Application uses componentsto gain access to services

Page 50: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (50)

Intro

duct

ion

to .N

ET

.NET Languages

Conclusions?

Page 51: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (1)

Intro

duct

ion

to .N

ET

3: Arrays and Collections3: Arrays and Collections

Andrew Cumming, SoC

Bill Buchanan, SoC

Page 52: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (2)

Intro

duct

ion

to .N

ET

Course Outline (Day 1 to 3)

Course Outline• Day 1: Morning – Introduction to Object-Orientation, Introduction

to .NET, Overview of .NET Framework, .NET Components. C#.• Day 1: Afternoon – Data Types, Variables, Converting Data

Types, if, for, while, File Handling• Day 2: Morning – Arrays, System.Collections, ArrayLists,

Hashtables.• Day 2: Afternoon – Reference-Type Variables, Common

Reference Types, Object Hierarchy, Namespaces, Debugging.• Day 3: Morning – Methods, Exceptions, Namespaces, Modules/

Assemblies, Non-visual Components, Regular Expressions, Intellisense, Help, Delegates, Events.

• Day 3: Afternoon – Cerebus, Limit-check components, Power meter class.

Page 53: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (3)

Intro

duct

ion

to .N

ET

Course Outline (Day 4 and 5)

Course Outline• Day 4: Morning – Classes, Encapsulation, Object-Orientation,

Classes, Sealed Classes, Interfaces, Abstract Classes.• Day 4: Afternoon – Using Constructors, Initializing Memory,

Objects and Memory, Resource Management.• Day 5: Morning – Cerebus: 1mW Test.• Day 5: Afternoon – Operators, Operator Overloading, Attributes,

Custom Attributes, Retrieving Attribute Values.

Page 54: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (4)

Intro

duct

ion

to .N

ET

Introduction

Module 3• Arrays.• Reading from CSV files.• System.Collections.• ArrayLists.• HashTables.

Page 55: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (5)

Intro

duct

ion

to .N

ET

System.Array object:

Methods include:

BinarySearch() Performs a binary on a one-dimensional arrayClear() Sets a range of elements to zero, False or NULL.Clone() Make a shallow copy of the array.Copy() Copies a range of values in an array.CopyTo() Copies one array to another.CreateInstance() Create a multidimensional array.GetLength() Return the number of elements in the array.IndexOf() Search for an object, and return the first index value of its place.Initialize() Set all the elements in the array to their default value.LastIndexOf() Returns the last object in the array.Reverse() Reverses the array.SetValue() Sets a specific array element to a value.Sort() Performs a sort on the array.

IsFixedSize Identifies if the array is a fixed size (get only)IsReadOnly Identifies if the array is read-only (get only)Length Identifies the length of the array (get only)

Page 56: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (6)

Intro

duct

ion

to .N

ET

Variables and Arrays

Element 0Element 0

Element 1Element 1

Element 2Element 2

Element 3Element 3

Element 4Element 4

Element n-2Element n-2

Element n-1Element n-1

ArrayNameVariables

ii

jj

kk

Referenceto the startof the arrayspace.

Allocated on the heap.

Page 57: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (7)

Intro

duct

ion

to .N

ET

Page 58: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (8)

Intro

duct

ion

to .N

ET

C# contains the methods of the System.Array object. It also inherits the syntax of C for representing an array index, which uses square brackets to identify the element, such as:

myArray[15];

for the element number 15. Note that C# starts its indexing at 0, thus myArray[15] is the 16th element of the array. To declare an array:

type[] arrayName;

For example to declare an array of doubles:

double[] inputValues;This declares the type of the array. To then to initialize it the following is used:

inputValues = new double[5];

which creates a new array with 5 elements (0 to 4).

Element 0

Element 1

Element 2

Element 3

Element 4

Element n-2

Element n-1

ArrayName

Page 59: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (9)

Intro

duct

ion

to .N

ET

namespace ConsoleApplication2{

class ArrayExample01{

static void getValues(double[] v){

int i;for (i=0;i<v.Length;i++){

System.Console.Write("Value >> ");v[i]=Convert.ToDouble(System.Console.ReadLine());

}}static double average(double[] v){

double total=0;int i;for (i=0;i<v.Length;i++) total+=v[i];return(total/v.Length);

}static void Main(string[] args){

int i;double[] inputValues;double av;

inputValues = new double[5];getValues(inputValues);av=average(inputValues);System.Console.WriteLine("Average is {0}",av);System.Console.ReadLine();

}}

}

Page 60: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (10)

Intro

duct

ion

to .N

ET

Using an array

Note that the declaration of the array type does not actually create the array, and the line:

double[] inputValues;double avav=inputValues[0];

will cause a build error of:

Use of unassigned local variable 'inputValues'

As the array can not been created yet, and just contains a null value.

Page 61: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (11)

Intro

duct

ion

to .N

ET

The arrays when they are initialized are set to a default value, such as zero for numeric data types, and null for strings. To initialise it with actual values, the required values are contained within curly brackets, such as:

double[] vals = {1.2,1.4,1.6,1.8,2.0};string[] menuItems = {"File", "Edit", "View", "Insert", "Tool", "Table"};

This performs both the declaration of the array and its initialization. Program 3.2 shows an example of the declaration of an array of strings.

Page 62: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (12)

Intro

duct

ion

to .N

ET

using System;namespace ConsoleApplication2{class ArrayExample01{

static void Main(string[] args){

int i;string[] ColourBands={"BLACK","BROWN","RED","ORANGE","YELLOW",

"GREEN"};for (i=0;i<ColourBands.Length;i++)System.Console.WriteLine("{0} is {1}",i,ColourBands[i]);System.Console.ReadLine();

}}

}

Page 63: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (13)

Intro

duct

ion

to .N

ET

using System;namespace ConsoleApplication2{

class ArrayExample01{

static double findSmallest(double[] v){

double smallest;smallest=v[0];for (int i=1; i<v.Length; i++)

if (v[i]<smallest) smallest = v[i];return(smallest);

}

static void Main(string[] args){

int i;double[] vals = {1.2,4.5,13.3,10.1,-4.3,52.5,6.2};double smallest;

smallest= findSmallest(vals);System.Console.WriteLine("Smallest value is {0}",smallest);System.Console.ReadLine();

}}

}

Page 64: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (14)

Intro

duct

ion

to .N

ET

foreach

foreach (type identifier in expression) statement

Thus:for (i=0;i<ColourBands.Length;i++)

System.Console.WriteLine("{0} is {1}",i,ColourBands[i]);

is replaced by (Program3_3_ArrayInitializationNumericForEach):

foreach (string band in ColourBands)System.Console.WriteLine("{0}",band);

For the example in Program 3.2 we can replace:

for (int i=1; i<v.Length; i++)if (v[i]<smallest) smallest = v[i];

with (Program3_4_ArrayInitializationStringsForEach):

foreach (double val in v)if (val<smallest) smallest = val;

Page 65: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (15)

Intro

duct

ion

to .N

ET

using System;namespace ConsoleApplication2{

class ArrayExample01{

static double findSmallest(double[] v){

double smallest;smallest=v[0];foreach (double val in v)

if (val<smallest) smallest = val;return(smallest);

}

static void Main(string[] args){

int i;double[] vals = {1.2,4.5,13.3,10.1,-4.3,52.5,6.2};double smallest;

smallest= findSmallest(vals);System.Console.WriteLine("Smallest value is {0}",smallest);System.Console.ReadLine();

}}

}

Page 66: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (16)

Intro

duct

ion

to .N

ET

Reading a CSV file

1,5,6,7,4,5,6,7,10,5,4,3,2,1

For this we can read the file one line at a time, and then split the string using the Split() method. This can be contained with the foreach statement, to parse each part of the split text. The code which fills the array v is:

do{

text=reader.ReadLine();if (text==null) break;foreach (string substring in text.Split(',')){

v[i]=Convert.ToDouble(substring);i++;

}} while (text != null);

Page 67: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (17)

Intro

duct

ion

to .N

ET

class ArrayExample02{

const int ARRAYLIMIT=100;static void fillData(double[] v){

int i=0;FileInfo theSourceFile = new FileInfo("..\\..\\test.csv");StreamReader reader = theSourceFile.OpenText();string text;do{

text=reader.ReadLine();if (text==null) break;foreach (string substring in text.Split(',')){

v[i]=Convert.ToDouble(substring);i++;

}} while (text != null);

reader.Close();}

}

Page 68: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (18)

Intro

duct

ion

to .N

ET

CSV

Often the CSV file is organised into rows with comma separated variables in each row. This is similar to a spreadsheet where the row is represented by a line, and the columns are delimited by commas. For example:

Fred,20Bert,10Colin,15Berty,26Freddy,22

We can then modify the reading method to parse the input line from the CSV file:

do{

text=reader.ReadLine();if (text==null) break;string[] str =text.Split(',');name[i]=str[0];age[i]=Convert.ToInt32(str[1]);i++;

} while (text != null);

Page 69: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (19)

Intro

duct

ion

to .N

ET

Typical Namespaces

• Microsoft.Win32• System• System.Collections• System.Configuration• System.Configuration.Assemblies• System.Diagnostics• System.Diagnostics.SymbolStore• System.Globalization• System.IO• System.IO.IsolatedStorage• System.Reflection• System.Reflection.Emit• System.Resources• System.Runtime• System.Runtime.CompilerServices• System.Runtime.InteropServices• System.Runtime.InteropServices.Expando• System.Runtime.Remoting• System.Runtime.Remoting.Activation• System.Runtime.Remoting.Channels• System.Runtime.Remoting.Contexts• System.Runtime.Remoting.Lifetime• System.Runtime.Remoting.Messaging• System.Runtime.Remoting.Metadata• System.Runtime.Remoting.Proxies• System.Runtime.Remoting.Services• System.Runtime.Serialization• System.Runtime.Serialization.Formatters• System.Security• System.Security.Cryptography

Page 70: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (20)

Intro

duct

ion

to .N

ET

System.CollectionsArrayListBitArrayCaseInsensitiveComparerCaseInsensitiveHashCodeProviderCollectionBaseComparerDictionaryBaseDictionaryEntryHashtableQueueReadOnlyCollectionBaseSortedListStack

Page 71: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (21)

Intro

duct

ion

to .N

ET

.NET Languages

Conclusions?

Page 72: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (1)

Intro

duct

ion

to .N

ET

Introduction to .NETIntroduction to .NET

Andrew Cumming, SoC

Bill Buchanan, SoC

Page 73: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (2)

Intro

duct

ion

to .N

ET

Course Outline (Day 1 to 3)

Course Outline• Day 1: Morning – Introduction to Object-Orientation, Introduction

to .NET, Overview of .NET Framework, .NET Components. C#.• Day 1: Afternoon – Data Types, Variables, Converting Data

Types, if, for, while, File Handling• Day 2: Morning – Arrays, System.Collections, ArrayLists,

Hashtables.• Day 2: Afternoon – Reference-Type Variables, Common

Reference Types, Object Hierarchy, Namespaces, Debugging.• Day 3: Morning – Methods, Exceptions, Namespaces, Modules/

Assemblies, Non-visual Components, Regular Expressions, Intellisense, Help, Delegates, Events.

• Day 3: Afternoon – Cerebus, Limit-check components, Power meter class.

Page 74: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (3)

Intro

duct

ion

to .N

ET

Course Outline (Day 4 and 5)

Course Outline• Day 4: Morning – Classes, Encapsulation, Object-Orientation,

Classes, Sealed Classes, Interfaces, Abstract Classes.• Day 4: Afternoon – Using Constructors, Initializing Memory,

Objects and Memory, Resource Management.• Day 5: Morning – Cerebus: 1mW Test.• Day 5: Afternoon – Operators, Operator Overloading, Attributes,

Custom Attributes, Retrieving Attribute Values.

Page 75: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (4)

Intro

duct

ion

to .N

ET

Introduction

Module 7• Classes and objects. Revision!• Using encapsulation.• Defining Object-Oriented Systems.• Inheritance.• Sealed classes.• Overriding classes.

Page 76: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (5)

Intro

duct

ion

to .N

ET

Introduction

An Introduction to Object-Orientation

Bill Buchanan

Page 77: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (6)

Intro

duct

ion

to .N

ET

Some Cups

Page 78: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (7)

Intro

duct

ion

to .N

ET

A Quick Introduction to Object-Orientation

Parameter Cup 1 Cup 2 Cup3Shape (Standard/Square/Mug) Standard Square MugColour (Red/Blue/Green) Blue Red GreenSize (Small/Medium/Large) Small Large SmallTransparency (0 to 100%) 100% 50% 25%Handle type (Small/Large) Small Small Large

In object-orientation:A collection of parameters defines a class.

Class for the cup is thus: Shape, Colour,Size, Transparency, HandleType.

In object-orientation: Objects arecreated from classes.

Page 79: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (8)

Intro

duct

ion

to .N

ET

Example C# Program using Object-Orientation

using System;

namespace ConsoleApplication2{

public class Cup {

public string Shape;public string Colour;public string Size;public int Transparency;public string Handle;

public void DisplayCup(){

System.Console.WriteLine("Cup is {0}, {1}", Colour, Handle);}

}

class Class1{

static void Main(string[] args){

Cup cup = new Cup();cup.Colour = "Red";cup.Handle = "Small";cup.DisplayCup();System.Console.ReadLine();

}}

}

Class definitions

Available variables(properties)

Method

Create new objectSet propertiesApply method

Page 80: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (9)

Intro

duct

ion

to .N

ET

Another example

using System;

namespace ConsoleApplication2{

public class Circuit {

public double Parallel(double r1, double r2){

return((r1*r2)/(r1+r2));}public double Series(double r1, double r2){

return(r1+r2);}

}

class Class1{

static void Main(string[] args){

double v1=100,v2=100;double res;

Circuit cir = new Circuit();

res=cir.Parallel(v1,v2);System.Console.WriteLine("Parallel resistance is {0} ohms",res);

res=cir.Series(100,100);System.Console.WriteLine("Series resistance is {0} ohms",res);

System.Console.ReadLine();}

}}

Class definitions

Page 81: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (10)

Intro

duct

ion

to .N

ET

Another example

using System;namespace ConsoleApplication2{

public class Complex{

public double real;public double imag;

public double mag(){

return (Math.Sqrt(real*real+imag*imag));}public double angle(){

return (Math.Atan(imag/real)*180/Math.PI); }

}class Class1{

static void Main(string[] args){

string str;double mag,angle;Complex r = new Complex();

System.Console.Write("Enter real value >>");str=System.Console.ReadLine();r.real = Convert.ToInt32(str);

System.Console.Write("Enter imag value >>");str=System.Console.ReadLine();r.imag = Convert.ToInt32(str);

mag=r.mag();angle=r.angle();

System.Console.WriteLine("Mag is {0} and angle is {1}",mag,angle);System.Console.ReadLine();

}}

}

⎟⎠⎞

⎜⎝⎛=

+=

+=

xyz

yxz

yxz

1

22

tan

j

Page 82: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (11)

Intro

duct

ion

to .N

ET

Introduction

Object Properties

Bill Buchanan

Page 83: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (12)

Intro

duct

ion

to .N

ET

Car properties

• Make. This could be Ford, Vauxhall, Nissan or Toyota.• Type. This could be Vectra, Astra, or Mondeo.• Colour. This could be colours such as Red, Green or

Blue.• Country of Manufacture. This could be countries such

as UK, Germany or France.

Page 84: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (13)

Intro

duct

ion

to .N

ET

Relationship Tree

using System;namespace sample01{

public class Car{

private string colour;private string type;private string make;private string country;private double cost; public string Colour{

get { return colour; }set { colour=value; }

}public string Type{

get { return type; }set { type=value; }

} public string Country{

get { return country; }set { country=value; }

}public string Make{

get { return make; }set { make=value; }

}public double Cost{

get { return cost; }set { cost=value; }

}}

using System;namespace sample01{

public class Car{

private string colour;private string type;private string make;private string country;private double cost; public string Colour{

get { return colour; }set { colour=value; }

}public string Type{

get { return type; }set { type=value; }

} public string Country{

get { return country; }set { country=value; }

}public string Make{

get { return make; }set { make=value; }

}public double Cost{

get { return cost; }set { cost=value; }

}}

class Class1{

static void Main(string[] args){

Car car1 = new Car();car1.Colour="Red";car1.Make="Ford";car1.Type="Mondeo";car1.Colour="UK";car1.Cost=15000;Console.WriteLine(

"Car is a " + car1.Make + " " + car1.Type);Console.ReadLine();

}}

}

Page 85: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (14)

Intro

duct

ion

to .N

ET

Read-only, write-only and read/write properties

public double Cost{

get { return cost; }}

public double Cost{

get { return cost; }}

public double Cost{

set { val=value; }}

public double Cost{

set { val=value; }}

public double Cost{

set { val=value; }get { return cost; }

}

public double Cost{

set { val=value; }get { return cost; }

}

Read/write

Read-only

Write-only

Page 86: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (15)

Intro

duct

ion

to .N

ET

Introduction

Object-Orientation

Bill Buchanan

Page 87: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (16)

Intro

duct

ion

to .N

ET

Let’s Look At Real Objects

Page 88: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (17)

Intro

duct

ion

to .N

ET

Classification

PlantsPlants

MineralsMinerals

MammalsMammalsSkin with fur/hair.Red blooded.Warm blooded.

Backbone

BirdsBirds

VertebratesVertebrates

AnimalsAnimals

InvertebratesInvertebrates

Page 89: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (18)

Intro

duct

ion

to .N

ET

Relationship Tree

MammalsMammals

CatsCats DogsDogs HorsesHorses

Increasingspecialisation

Increasinggeneralisation

Sharedbehavioursand characteristics

DomesticDomestic

TigerTiger

LionLion

Collection VertebratesVertebrates

Page 90: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (19)

Intro

duct

ion

to .N

ET

Relationship Tree

SystemSystem

WindowsWindows IOIO

ComponentModel

ComponentModel

DesignDesign

DrawingDrawing

FormsForms Increasingspecialisation

Increasinggeneralisation

ButtonButton

FormForm

ComboBox

ComboBoxLabelLabel

Class

containers

Page 91: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (20)

Intro

duct

ion

to .N

ET

Relationship Tree

SystemSystem

WindowsWindows

ComponentModel

ComponentModel

DesignDesign

FormsForms

ComboBox

ComboBoxLabelLabel

containers

using System.Collections;using System.Windows.Forms;using System.Data;namespace WindowsApplication3{

public class Form1 : System.Windows.Forms.Form{

private System.Windows.Forms.Button button1;private System.Windows.Forms.TextBox textBox1;public Form1(){

InitializeComponent();}private void InitializeComponent(){

this.button1 = new System.Windows.Forms.Button();this.textBox1 = new System.Windows.Forms.TextBox();this.button1.Location = new System.Drawing.Point(152, 192);this.button1.Name = "button1";this.button1.TabIndex = 0;this.button1.Text = "button1";this.textBox1.Location = new System.Drawing.Point(80, 64);this.textBox1.Name = "textBox1";this.textBox1.Text = "textBox1";this.Controls.Add(this.textBox1);this.Controls.Add(this.button1);this.Name = "Form1";this.Text = "Form1";this.Load += new System.EventHandler(this.Form1_Load);this.ResumeLayout(false);

}static void Main() {

Application.Run(new Form1());}private void Form1_Load(object sender, System.EventArgs e){

using System.Collections;using System.Windows.Forms;using System.Data;namespace WindowsApplication3{

public class Form1 : System.Windows.Forms.Form{

private System.Windows.Forms.Button button1;private System.Windows.Forms.TextBox textBox1;public Form1(){

InitializeComponent();}private void InitializeComponent(){

this.button1 = new System.Windows.Forms.Button();this.textBox1 = new System.Windows.Forms.TextBox();this.button1.Location = new System.Drawing.Point(152, 192);this.button1.Name = "button1";this.button1.TabIndex = 0;this.button1.Text = "button1";this.textBox1.Location = new System.Drawing.Point(80, 64);this.textBox1.Name = "textBox1";this.textBox1.Text = "textBox1";this.Controls.Add(this.textBox1);this.Controls.Add(this.button1);this.Name = "Form1";this.Text = "Form1";this.Load += new System.EventHandler(this.Form1_Load);this.ResumeLayout(false);

}static void Main() {

Application.Run(new Form1());}private void Form1_Load(object sender, System.EventArgs e){

Baseclass

Derivedclass

this meansthis object(which is Form1)

Page 92: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (21)

Intro

duct

ion

to .N

ET

Introduction

Inheritance

Bill Buchanan

Page 93: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (22)

Intro

duct

ion

to .N

ET

Inheritance

Inheritance – Derivescharacteristics andbehaviours from thoseabove us.

Page 94: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (23)

Intro

duct

ion

to .N

ET

Inheritance

SystemSystem

WindowsWindows

ComponentModel

ComponentModel

DesignDesign

FormsForms

ComboBox

ComboBoxLabelLabel

containers

using System;namespace ConsoleApplication1{

class People {

private string name;public string Name {

get {return name;}set {name=value;}

}public void ToLowerCase() {

name=name.ToLower();}

}class Profile : People { // Inherit from People

public void Display() {

Console.WriteLine("Name: "+Name);}

}class Test {

static void Main() {

Profile p1 = new Profile();p1.Name="Fred";p1.ToLowerCase();p1.Display();System.Console.ReadLine();

}}

}

using System;namespace ConsoleApplication1{

class People {

private string name;public string Name {

get {return name;}set {name=value;}

}public void ToLowerCase() {

name=name.ToLower();}

}class Profile : People { // Inherit from People

public void Display() {

Console.WriteLine("Name: "+Name);}

}class Test {

static void Main() {

Profile p1 = new Profile();p1.Name="Fred";p1.ToLowerCase();p1.Display();System.Console.ReadLine();

}}

}

Method derivedfrom People

Inherentfrom People

PeoplePeople

ProfileProfile

Name: fred

Page 95: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (24)

Intro

duct

ion

to .N

ET

Inheritance

SystemSystem

WindowsWindows

ComponentModel

ComponentModel

DesignDesign

FormsForms

ComboBox

ComboBoxLabelLabel

containers

using System;namespace ConsoleApplication1{

class People {

private string name;public string Name {

get {return name;}set {name=value;}

}public void ToLowerCase() {

name=name.ToLower();}

}class Profile : People { // Inherit from People

public void Display() {

Console.WriteLine("Name: "+Name);}

}class Test {

static void Main() {

Profile p1 = new Profile();Profile p2 = new Profile();p1.Name="Fred";p2.Name="Bert";p1.ToLowerCase();p1.Display();p2.Display();System.Console.ReadLine();

}}

}

using System;namespace ConsoleApplication1{

class People {

private string name;public string Name {

get {return name;}set {name=value;}

}public void ToLowerCase() {

name=name.ToLower();}

}class Profile : People { // Inherit from People

public void Display() {

Console.WriteLine("Name: "+Name);}

}class Test {

static void Main() {

Profile p1 = new Profile();Profile p2 = new Profile();p1.Name="Fred";p2.Name="Bert";p1.ToLowerCase();p1.Display();p2.Display();System.Console.ReadLine();

}}

}

Inherentfrom People

PeoplePeople

ProfileProfile

Name: fredName: Bert

Page 96: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (25)

Intro

duct

ion

to .N

ET

Sealed Classes

SystemSystem

WindowsWindows

ComponentModel

ComponentModel

DesignDesign

FormsForms

ComboBox

ComboBoxLabelLabel

containers

using System;namespace ConsoleApplication1{

sealed class People {

private string name;public string Name {

get {return name;}set {name=value;}

}public void ToLowerCase() {

name=name.ToLower();}

}class Profile : People { // Inherit from People

public void Display() {

Console.WriteLine("Name: "+Name);}

}class Test {

static void Main() {

Profile p1 = new Profile();p1.Name="Fred";p1.ToLowerCase();p1.Display();System.Console.ReadLine();

}}

}

using System;namespace ConsoleApplication1{

sealed class People {

private string name;public string Name {

get {return name;}set {name=value;}

}public void ToLowerCase() {

name=name.ToLower();}

}class Profile : People { // Inherit from People

public void Display() {

Console.WriteLine("Name: "+Name);}

}class Test {

static void Main() {

Profile p1 = new Profile();p1.Name="Fred";p1.ToLowerCase();p1.Display();System.Console.ReadLine();

}}

}

Inherentfrom People

PeoplePeople

ProfileProfile

'ConsoleApplication1.Profile' : cannot inherit from sealed class 'ConsoleApplication1.People'

Page 97: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (26)

Intro

duct

ion

to .N

ET

Introduction

Overriding Classes

Bill Buchanan

Page 98: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (27)

Intro

duct

ion

to .N

ET

Overriding classes

using System;namespace ConsoleApplication1{

class People {

private string name;

public string Name {

get {return name;}set {name=value;}

}public virtual void ToLowerCase() {

name=name.ToLower();}

}

class Profile : People { // Inherit from People

public void Display() {

Console.WriteLine("Name: "+Name);}public override void ToLowerCase(){

Console.WriteLine("Method has been overwritten");}

}

using System;namespace ConsoleApplication1{

class People {

private string name;

public string Name {

get {return name;}set {name=value;}

}public virtual void ToLowerCase() {

name=name.ToLower();}

}

class Profile : People { // Inherit from People

public void Display() {

Console.WriteLine("Name: "+Name);}public override void ToLowerCase(){

Console.WriteLine("Method has been overwritten");}

}

class Test {

static void Main() {

Profile p1 = new Profile();

p1.Name="Fred";

p1.ToLowerCase();p1.Display();

System.Console.ReadLine();}

}}

class Test {

static void Main() {

Profile p1 = new Profile();

p1.Name="Fred";

p1.ToLowerCase();p1.Display();

System.Console.ReadLine();}

}}

Method has been overwrittenName: Fred

Page 99: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (28)

Intro

duct

ion

to .N

ET

Methods for the default object

Object• Equals(). Determines if two objects are the same.• Finalize(). Cleans up the object (the destructor).• GetHashCode(). Allows an object to define its hash code.• GetType(). Determine the type of an object.• MemberwiseClone(). Create a copy of the object.• ReferenceEquals(). Determines whether two objects are of the same instance.• ToString(). Convert an object to a string.

Page 100: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (29)

Intro

duct

ion

to .N

ET

using System;namespace ConsoleApplication1{

class People {

private string name;public string Name {

get {return name;}set {name=value;}

}public virtual void ToLowerCase() {

name=name.ToLower();}

}class Profile : People { // Inherit from People

public void Display() {

Console.WriteLine("Name: "+Name);}public override string ToString(){

return("Cannot implement this method");}

}

using System;namespace ConsoleApplication1{

class People {

private string name;public string Name {

get {return name;}set {name=value;}

}public virtual void ToLowerCase() {

name=name.ToLower();}

}class Profile : People { // Inherit from People

public void Display() {

Console.WriteLine("Name: "+Name);}public override string ToString(){

return("Cannot implement this method");}

}

class Test {

static void Main() {

Profile p1 = new Profile();p1.Name="Fred";string str=p1.ToString();Console.WriteLine(str);System.Console.ReadLine();

}}

}

class Test {

static void Main() {

Profile p1 = new Profile();p1.Name="Fred";string str=p1.ToString();Console.WriteLine(str);System.Console.ReadLine();

}}

}

Page 101: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (30)

Intro

duct

ion

to .N

ET

Introduction

Abstract Classes

Bill Buchanan

Page 102: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (31)

Intro

duct

ion

to .N

ET

Abstract Classes

Here is thenew object, but you

can’t use it unless you implement your own

OpenTheBox()method!

Here is thenew object, but you

can’t use it unless you implement your own

OpenTheBox()method!

Okay. It’s worth it.

I’ll do that.

Okay. It’s worth it.

I’ll do that.

Page 103: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (32)

Intro

duct

ion

to .N

ET

An example

Page 104: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (33)

Intro

duct

ion

to .N

ET

Abstract Classes

using System;namespace ConsoleApplication1{

abstract class People {

private string name;public string Name {

get {return name;}set {name=value;}

}public void ToLowerCase() {

name=name.ToLower();}abstract public void DisplayLower();

}

class Profile : People { // Inherit from People

public void Display() {

Console.WriteLine("Name: "+Name);}

}

using System;namespace ConsoleApplication1{

abstract class People {

private string name;public string Name {

get {return name;}set {name=value;}

}public void ToLowerCase() {

name=name.ToLower();}abstract public void DisplayLower();

}

class Profile : People { // Inherit from People

public void Display() {

Console.WriteLine("Name: "+Name);}

}

class Test {static void Main() {

Profile p1 = new Profile();

p1.Name="Fred";

p1.ToLowerCase();p1.Display();p1.DisplayLower();

System.Console.ReadLine();}

}}

class Test {static void Main() {

Profile p1 = new Profile();

p1.Name="Fred";

p1.ToLowerCase();p1.Display();p1.DisplayLower();

System.Console.ReadLine();}

}}

'ConsoleApplication1.Profile' does not implement inherited abstract member 'ConsoleApplication1.People.DisplayLower()'

Page 105: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (34)

Intro

duct

ion

to .N

ET

using System;namespace ConsoleApplication1{

abstract class People {

private string name;

public string Name {

get {return name;}set {name=value;}

}public void ToLowerCase() {

name=name.ToLower();}abstract public void DisplayLower();

}class Profile : People { // Inherit from People

public void Display() {

Console.WriteLine("Name: "+Name);}public override void DisplayLower(){

Console.WriteLine("Name: "+Name.ToUpper());}

}Abstract Classes

class Test {static void Main() {

Profile p1 = new Profile();

p1.Name="Fred";

p1.ToLowerCase();p1.Display();p1.DisplayLower();

System.Console.ReadLine();}

}}

class Test {static void Main() {

Profile p1 = new Profile();

p1.Name="Fred";

p1.ToLowerCase();p1.Display();p1.DisplayLower();

System.Console.ReadLine();}

}}

Name: fredName: FRED

Page 106: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (1)

Intro

duct

ion

to .N

ET

Introduction to .NETIntroduction to .NET

Andrew Cumming, SoC

Bill Buchanan, SoC

Page 107: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (2)

Intro

duct

ion

to .N

ET

Course Outline (Day 1 to 3)

Course Outline• Day 1: Morning – Introduction to Object-Orientation, Introduction

to .NET, Overview of .NET Framework, .NET Components. C#.• Day 1: Afternoon – Data Types, Variables, Converting Data

Types, if, for, while, File Handling• Day 2: Morning – Arrays, System.Collections, ArrayLists,

Hashtables.• Day 2: Afternoon – Reference-Type Variables, Common

Reference Types, Object Hierarchy, Namespaces, Debugging.• Day 3: Morning – Methods, Exceptions, Namespaces, Modules/

Assemblies, Non-visual Components, Regular Expressions, Intellisense, Help, Delegates, Events.

• Day 3: Afternoon – Cerebus, Limit-check components, Power meter class.

Page 108: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (3)

Intro

duct

ion

to .N

ET

Course Outline (Day 4 and 5)

Course Outline• Day 4: Morning – Classes, Encapsulation, Object-Orientation,

Classes, Sealed Classes, Interfaces, Abstract Classes.• Day 4: Afternoon – Using Constructors, Initializing Memory,

Objects and Memory, Resource Management.• Day 5: Morning – Cerebus: 1mW Test.• Day 5: Afternoon – Operators, Operator Overloading, Attributes,

Custom Attributes, Retrieving Attribute Values.

Page 109: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (4)

Intro

duct

ion

to .N

ET

Introduction

Module 8• Interfaces.• Constructors and destructors.• Static and instance members.• Memory.

Page 110: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (5)

Intro

duct

ion

to .N

ET

Introduction

Interfaces

Bill Buchanan

Page 111: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (6)

Intro

duct

ion

to .N

ET

Interfaces

Here is thenew object, but you must implement it in every way that I have

defined!

Here is thenew object, but you must implement it in every way that I have

defined!

Pweh! You’rerather strict.

Pweh! You’rerather strict.

Page 112: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (7)

Intro

duct

ion

to .N

ET

Circuit Object

r1 r2

r1

r2

21 RRRT +=

21

21

RRRRRT +⋅

=Circuit

Page 113: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (8)

Intro

duct

ion

to .N

ET

Interface Definition

r1 r2

r1

r2

21 RRRT +=

21

21

RRRRRT +⋅

=interface NewCircuit{

double r1 { set; get;}double r2 { set; get;}double calcParallel();double calcSeries();

}

interface NewCircuit{

double r1 { set; get;}double r2 { set; get;}double calcParallel();double calcSeries();

}

Page 114: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (9)

Intro

duct

ion

to .N

ET

Derived Class Definition

r1 r2

r1

r2

21 RRRT +=

21

21

RRRRRT +⋅

=

public class Circuit: NewCircuit{

private double res1, res2;public double r1 {

set { res1=value;} get { return res1; }

}public double r2 {

set { res2=value;} get { return res2; }

}public double calcParallel(){

return((r1*r2)/(r1+r2));}public double calcSeries(){

return(r1+r2);}

}

interface NewCircuit{

double r1 { set; get;}double r2 { set; get;}double calcParallel();double calcSeries();

}

interface NewCircuit{

double r1 { set; get;}double r2 { set; get;}double calcParallel();double calcSeries();

}

Page 115: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (10)

Intro

duct

ion

to .N

ET

Derived Class Definition

r1 r2

r1

r2

21 RRRT +=

21

21

RRRRRT +⋅

=

public class Circuit: NewCircuit{

private double res1, res2;public double r1 {

set { res1=value;} get { return res1; }

}public double r2 {

set { res2=value;} get { return res2; }

}public double calcParallel(){

return((r1*r2)/(r1+r2));}public double calcSeries(){

return(r1+r2);}

}

public class Class1{

static void Main(){

Circuit cir = new Circuit();cir.r1=1000;cir.r2=1000;double res = cir.calcParallel();System.Console.WriteLine(

"Parallel Resistance is " + res);System.Console.ReadLine();

}}

Page 116: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (11)

Intro

duct

ion

to .N

ET

IOCollection. Defines size, enumerators and synchronization methods for all collections. The public properties are: Count, IsSynchronized and SyncRoot, and a method of CopyTo().

IComparer. Compares two objects. The associated method is Compare().

IEnumerator. Supports iteration over a collection. The properties are Current, and the methods are MoveNext() and Reset().

IList. Represents a collection of objects that are accessed by an index. The properties are IsFixedSize, IsReadOnly, Item, and the methods are: Add(), Clear(), Contains(), IndexOf(), Insert(), Remove() and RemoveAt().

Page 117: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (12)

Intro

duct

ion

to .N

ET

Page 118: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (13)

Intro

duct

ion

to .N

ET

The IOCollection defines:

The classes which implement IOCollection include:

• Array.• ArrayList.• Hastable.

Thus an array will have the following properties:

• Count. • IsSynchronized.• SyncRoot

Page 119: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (14)

Intro

duct

ion

to .N

ET

Page 120: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (15)

Intro

duct

ion

to .N

ET

Introduction

Constructors

Bill Buchanan

Page 121: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (16)

Intro

duct

ion

to .N

ET

Constructors and destructors

A constructor is a method that is called wheneverthe object is first created.

A destructor is a method that is called wheneverthe object is destroyed.

Page 122: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (17)

Intro

duct

ion

to .N

ET

class Profile{

string code, name, telephone;// Constructorpublic Profile(string c){

code=c;}public string Name{

set { name=value; }get { return name; }

}public string Telephone{

set { telephone=value; }get { return telephone; }

}public void Display(){

Console.WriteLine("Code: " + code + " Name: " + name + " Telephone: " + telephone);

Console.ReadLine();}

}

Page 123: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (18)

Intro

duct

ion

to .N

ET

class Profile{

string code, name, telephone;// Constructorpublic Profile(string c){

code=c;}public string Name{

set { name=value; }get { return name; }

}public string Telephone{

set { telephone=value; }get { return telephone; }

}public void Display(){

Console.WriteLine("Code: " + code + " Name: " + name + " Telephone: " + telephone);

Console.ReadLine();}

}

class Class1{

static void Main(string[] args){

Profile pf1=new Profile("00000");pf1.Name="Fred Smith";pf1.Telephone="123456"; pf1.Display();

}}

Page 124: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (19)

Intro

duct

ion

to .N

ET

Introduction

Static and Instance Members

Bill Buchanan

Page 125: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (20)

Intro

duct

ion

to .N

ET

Static and instance members

A class can either have static members of instance members. Instance members relate to the object created, while static members can be used by referring to the name of the method within the class. For example the following are calls to static members:

Convert.ToInteger("str");Console.WriteLine("Hello");

Whereas in the following, there are two instance methods (Read()and Write()), these operate on the created data type:

System.IO.StreamReader nn = new StreamReader();nn.Read();nn.Write();

Instance members thus operate on an instance of a class.

Page 126: Introduction to .NETIntroduction to - Napierbill/agilent/IntroductionToDotNet... · Introduction to .NETIntroduction to .NET Andrew Cumming, SoC ... int WINAPI WinMain ... Introduction

W.Buchanan (21)

Intro

duct

ion

to .N

ET

Using a static member to count the number of instances

using System;

class Profile{

static int numberProfile=0;string code, name, telephone;

// Constructorpublic Profile(string c){

numberProfile++;code=c;

}..

}public static void HowMany(){

Console.WriteLine("There are " + numberProfile + " profiles ");Console.ReadLine();

}}

class Class1{

static void Main(string[] args){

Profile pf1=new Profile("00000");pf1.Name="Fred Smith"; pf1.Telephone="123456"; Profile pf2=new Profile("00001");pf2.Name="Fred Cannon"; pf2.Telephone="223456"; Profile pf3=new Profile("00002");pf3.Name="Fred McDonald"; pf3.Telephone="323456";

Profile.HowMany();}

}