Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com Pick up your free drink below...

Post on 27-Dec-2015

214 views 1 download

Transcript of Michael Olivero Microsoft Student Ambassador for FIU mike@mike95.com Pick up your free drink below...

Michael OliveroMicrosoft Student Ambassador for FIUmike@mike95.com

Pick up your free drink below to your left. 1 per person please.

Knowledge PrerequisitesKnowledge Prerequisites

Attendees should meet the following Attendees should meet the following prerequisites:prerequisites: Be familiar with the Microsoft Visual Studio Be familiar with the Microsoft Visual Studio

EnvironmentEnvironment Have some experience in an Object oriented Have some experience in an Object oriented

language such as Java or C++language such as Java or C++ Have a moderate understanding of SQL syntaxHave a moderate understanding of SQL syntax Have some experience building HTML pagesHave some experience building HTML pages

Over view of SemesterOver view of Semester

How many are new?How many are new? Introduction to .NET FrameworkIntroduction to .NET FrameworkSetting up IIS on Windows XPSetting up IIS on Windows XPASP.NETASP.NETSetting up SQL ServerSetting up SQL ServerAdvanced ASP.NETAdvanced ASP.NETOther topicsOther topics

Topics Covered TodayTopics Covered Today

.NET Framework & CLR.NET Framework & CLR .NET Base Class Library.NET Base Class Library Using Classes & StructuresUsing Classes & Structures Constructors, Methods, In/OutConstructors, Methods, In/Out Scope & Access LevelsScope & Access Levels Garbage CollectionGarbage Collection Give AwayGive Away

.NET Framework & CLR.NET Framework & CLR

After this lesson you will be able to After this lesson you will be able to describe:describe: The elements of the .NET frameworkThe elements of the .NET framework Parts of an assembly & identify what is Parts of an assembly & identify what is

contained in each partcontained in each part How a .NET application is compiled & How a .NET application is compiled &

executed executed

Overview of .NET FrameworkOverview of .NET Framework

It is a managed type-safe It is a managed type-safe environmentenvironment

Consists of two main componentsConsists of two main components Common Language RuntimeCommon Language Runtime .NET Framework class library.NET Framework class library

Overview of .NET FrameworkOverview of .NET Framework

Common Language RuntimeCommon Language Runtime An environment that manages codeAn environment that manages code

Machine Code compilation, memory Machine Code compilation, memory allocation, thread management, and allocation, thread management, and Garbage collectionGarbage collection

Common type system (CTS)Common type system (CTS)Enforces strict type-safetyEnforces strict type-safetyEnsures code is executed in safe Ensures code is executed in safe

environment by enforcing code access environment by enforcing code access securitysecurity

Overview of .NET FrameworkOverview of .NET Framework

.NET Framework class library.NET Framework class library Collection of useful and reusable typesCollection of useful and reusable types Types are objected-oriented and fully Types are objected-oriented and fully

extensibleextensible Permits you to add rich functionality to Permits you to add rich functionality to

your applicationyour application We will discuss this in more detail on We will discuss this in more detail on

later slideslater slides

Languages and the .NET Languages and the .NET Framework Framework .NET Framework is cross-language compatible.NET Framework is cross-language compatible

Components can interact no matter what Components can interact no matter what language they were originally built with.language they were originally built with.

Language interoperability is OOP compatibleLanguage interoperability is OOP compatible .NET Languages (VB, C#) are compiled into .NET Languages (VB, C#) are compiled into

Intermediate Language (IL) or (MSIL)Intermediate Language (IL) or (MSIL) Common Language Specification (CLS) Common Language Specification (CLS)

defines standard for IL where all compilers defines standard for IL where all compilers should adhere to.should adhere to.

Easier reuse & integration Easier reuse & integration

CLS ensures type compatibilityCLS ensures type compatibility Because all compiled code is in IL, all Because all compiled code is in IL, all

primitive types are represented as .NET primitive types are represented as .NET types.types.

An int in C# and an int in VB are both An int in C# and an int in VB are both represented by an Int32 in ILrepresented by an Int32 in IL

This allows easy communication This allows easy communication between components without costly between components without costly conversion / check code.conversion / check code.

Structure of .NET Application Structure of .NET Application

Assembly – primary unit of .NET appAssembly – primary unit of .NET app A self describing collection of code, resources, A self describing collection of code, resources,

and metadata.and metadata. Parts of AssemblyParts of Assembly

Assembly manifestAssembly manifestContains information about what is within the Contains information about what is within the

assembly. It provides:assembly. It provides: Identity information (Name & version)Identity information (Name & version) List of types exposed by assemblyList of types exposed by assembly List of dependent assembliesList of dependent assemblies List of code access security instructions, including List of code access security instructions, including

permissions required by assemblypermissions required by assembly

Structure of .NET Application Structure of .NET Application

One manifest per assemblyOne manifest per assembly Assembly contains 1 or more modulesAssembly contains 1 or more modules Each Module contains 1 or more types composed Each Module contains 1 or more types composed

of of Reference types (classes)Reference types (classes) Value types (structures)Value types (structures) More of this on a later slide.More of this on a later slide.

Inspection of AssemblyInspection of AssemblyDEMODEMO IL = Microsoft's Intermediate Language (i.e. generic asm)IL = Microsoft's Intermediate Language (i.e. generic asm) ILDasm = IL DisassemblerILDasm = IL Disassembler

c:\> ildasm hello.exe

Compilation & Execution Compilation & Execution

!NOT! compiled to binary machine !NOT! compiled to binary machine codecode

IL compiled to binary machine code IL compiled to binary machine code with JIT (Just In Time) compilation.with JIT (Just In Time) compilation.

As IL code is encountered, JIT As IL code is encountered, JIT compiles to machine code.compiles to machine code.

Each portion compiled only once.Each portion compiled only once.

Summary Summary .NET Framework.NET Framework

Two Core partsTwo Core parts Common Language RuntimeCommon Language Runtime

Provides core servicesProvides core services Base Class LibraryBase Class Library

Exposes rich set of pre-developed classesExposes rich set of pre-developed classes CLSCLS

defines minimum standard for ILdefines minimum standard for IL Type compatibilityType compatibility

AssemblyAssembly Primary unit of .NET applicationPrimary unit of .NET application Consists ofConsists of

Assembly Manifest, one or more modules, and code for Assembly Manifest, one or more modules, and code for application.application.

Topics Covered TodayTopics Covered Today

.NET Framework & CLR.NET Framework & CLR .NET Base Class Library.NET Base Class Library

Using Classes & StructuresUsing Classes & Structures

Constructors, Methods, In/OutConstructors, Methods, In/Out

Scope & Access LevelsScope & Access Levels

Garbage CollectionGarbage Collection

Give AwayGive Away

.NET Base Class Library.NET Base Class Library

After this lesson you will be able to After this lesson you will be able to describe:describe: The .NET base class libraryThe .NET base class library Difference between value & reference Difference between value & reference

typestypes Create a reference to a namespaceCreate a reference to a namespace Create an instance of .NET framework Create an instance of .NET framework

class & value typeclass & value type

.NET Base Class Library.NET Base Class Library Provides much of the services you will need when writing Provides much of the services you will need when writing

your applicationyour application Organized using NamespacesOrganized using Namespaces

Logical groupings of related classesLogical groupings of related classes Namespaces organized hierarchically, the root namespace is Namespaces organized hierarchically, the root namespace is

SystemSystem Examples:Examples:

System;System; System.Data;System.Data; System.Data.SQLClient;System.Data.SQLClient; http://www.mike95.com/Microsoft/S1/Namespaces.htmlhttp://www.mike95.com/Microsoft/S1/Namespaces.html

Reference & Value TypesReference & Value Types

Memory is of two forms: stack & heapMemory is of two forms: stack & heap Stack is local memory – used for value Stack is local memory – used for value

types.types. Heap is general memory – used for Heap is general memory – used for

reference types.reference types. A reference type is allocated in the heap, A reference type is allocated in the heap,

but it’s reference variable is allocated on but it’s reference variable is allocated on the stack.the stack.

Using .NET Framework TypesUsing .NET Framework Types

Value TypesValue Types Visual Basic .NET SyntaxVisual Basic .NET Syntax

Dim myInteger As Integer = 42Dim myInteger As Integer = 42 Visual C# SyntaxVisual C# Syntax

int myInteger = 42;int myInteger = 42;

Using .NET Framework TypesUsing .NET Framework Types

Declaration for Reference TypesDeclaration for Reference Types Visual Basic .NETVisual Basic .NET

Dim myForm As System.Windows.Forms.FormDim myForm As System.Windows.Forms.Form

Visual C#Visual C# System.Windows.Forms.Form myForm;System.Windows.Forms.Form myForm;

InitializationInitialization myForm = new System.Windows.Form.Form()myForm = new System.Windows.Form.Form()

Using .NET Framework TypesUsing .NET Framework Types

All variables are assigned default valuesAll variables are assigned default values Numeric are assigned zeroNumeric are assigned zero Boolean are assigned FalseBoolean are assigned False Reference are assigned nullReference are assigned null However, you should not rely on these However, you should not rely on these

initial values.initial values.

Automatic Boxing / UnBoxingAutomatic Boxing / UnBoxing

Values types can be used Values types can be used interchangeably with related interchangeably with related reference types.reference types.

Ex: A value type can be treated as a Ex: A value type can be treated as a reference and vice versa.reference and vice versa.

int age = 5;int age = 5;

System.Console.WriteLine( age.ToString() );System.Console.WriteLine( age.ToString() );

Using .NET Framework TypesUsing .NET Framework Types

Using namespacesUsing namespaces Imports System.Windows.Forms (VB.NET)Imports System.Windows.Forms (VB.NET) using System.Windows.Forms; (C#)using System.Windows.Forms; (C#)

Creating aliases to namespaces or Creating aliases to namespaces or classes (C# only)classes (C# only) Using myAlias = MyNameSpace.MyClassUsing myAlias = MyNameSpace.MyClass

Referencing External LibrariesReferencing External Libraries

Importing external libraries into your Importing external libraries into your projectsprojects Must create a referenceMust create a reference

Quick DemoQuick Demo

Importing namespacesImporting namespaces Where they are specifiedWhere they are specified

Importing and using external Importing and using external assemblies using referencesassemblies using references

Topics Covered TodayTopics Covered Today

.NET Framework & CLR.NET Framework & CLR .NET Base Class Library.NET Base Class Library Using Classes & StructuresUsing Classes & Structures

Constructors, Methods, In/OutConstructors, Methods, In/Out

Scope & Access LevelsScope & Access Levels

Garbage CollectionGarbage Collection

Give Away – Visual Studio .NETGive Away – Visual Studio .NET

Using Classes and StructuresUsing Classes and Structures

After this lesson you will be able to:After this lesson you will be able to: Create a new class or structureCreate a new class or structure Create an instance of a class or structureCreate an instance of a class or structure Explain difference between a class / Explain difference between a class /

structurestructure Create a nested typeCreate a nested type

Creating ClassesCreating Classes

Use the class keyword as in Java & C++Use the class keyword as in Java & C++ class Treeclass Tree

{{ //class member implementation //class member implementation}}

Creating StructuresCreating Structures

Use the struct keyword as in C++Use the struct keyword as in C++ struct Tree struct Tree

{    {     //  Tree  implementation goes here //  Tree  implementation goes here } }

Structure  Tree  Structure  Tree  '  Tree  implementation goes here'  Tree  implementation goes here

End StructureEnd Structure

Adding MembersAdding Members

class Treeclass Tree{{ int Age;int Age;}}

class Treeclass Tree Age as IntegerAge as IntegerEnd ClassEnd Class

Nested TypesNested Types

class Treeclass Tree{{    // Tree class code goes here    // Tree class code goes here        class Branchclass Branch    {    {       // Branch class code goes here.       // Branch class code goes here.        }}} }

Instantiating User-defined TypesInstantiating User-defined Types

Must use new keyword with classesMust use new keyword with classesStructure types are allocated on the Structure types are allocated on the

stackstack

Topics Covered TodayTopics Covered Today

.NET Framework & CLR.NET Framework & CLR .NET Base Class Library.NET Base Class Library Using Classes & StructuresUsing Classes & Structures Using Methods, In/Out, ConstructorUsing Methods, In/Out, Constructor

Scope & Access LevelsScope & Access Levels

Garbage CollectionGarbage Collection

Give Away – Visual Studio .NETGive Away – Visual Studio .NET

Using MethodsUsing Methods

After this lesson you will be able to:After this lesson you will be able to: Create a new methodCreate a new method Specify return types for your methodSpecify return types for your method Specify input & output parametersSpecify input & output parameters Create a constructor & destructorCreate a constructor & destructor

Adding MethodsAdding Methods

C# – fuctionsC# – fuctionsPublic void NoReturnMethod()Public void NoReturnMethod(){{

}}

Public int ADD( int A, int B )Public int ADD( int A, int B ){{ return A + B; return A + B;}}

Calling Methods in VBCalling Methods in VB

VB.NET requires parenthesis on all VB.NET requires parenthesis on all calls (including subs)calls (including subs)

Example:Example:DisplayInfo( “Hello” )DisplayInfo( “Hello” )vs.vs.DisplayInfo “Hello”DisplayInfo “Hello”

Method VariablesMethod Variables Variables declared within methods have method Variables declared within methods have method

scopescope Variables within for or while loop have scope only Variables within for or while loop have scope only

within loopwithin loop In general, variable scope is determined by It’s In general, variable scope is determined by It’s

nearest set of braces for C#nearest set of braces for C# VB has “static” declarationVB has “static” declaration

Persists in memory for multiple function callsPersists in memory for multiple function callsSample use: You would use a Static variable Sample use: You would use a Static variable

for a method that needed to keep track of how for a method that needed to keep track of how many times it had been called.many times it had been called.

C# has different meaning for staticC# has different meaning for static

ParametersParameters

Three options:Three options: pass-by-value (default)pass-by-value (default) pass-by-referencepass-by-reference pass-by-result ("copy-out")pass-by-result ("copy-out")

public class App{

public static void Main() { int i = 99; Foo(i); System.Console.WriteLine(i); // i = 99 }

private static void Foo(int value) { value = value + 1; System.Console.WriteLine(i); //value = 100 }}

Case 1: pass-by-value Case 1: pass-by-value with a value type with a value type Bits are copied…Bits are copied…

Stack

i 99stack frame for Main

stack frame for Foovalue 100

public class App{

public static void Main() { int i = 99; Foo(ref i); System.Console.WriteLine(i); // i = 100 }

private static void Foo(ref int value) { value = value + 1; }}

Case 2: pass-by-ref with a Case 2: pass-by-ref with a value typevalue typeReference is passed…Reference is passed…

Stack

i 99stack frame for Main

stack frame for Foovalue

Case 3: pass-by-value Case 3: pass-by-value with a reference typewith a reference typeReferenceReference is copied… is copied…

public class App{

public static void Main() { Tree aTree; aTree = new Tree(); aTree.Age = 99; Foo2(aTree); System.Console.WriteLine(aTree.Age);// 100 }

private static void Foo2(Tree A) { A.Age = A.Age + 1; // 100 A = new Tree(); }}

Stack

aTree Tree Data

aTreeA

Tree

public class App{

public static void Main() { Tree aTree; aTree = new Tree(); aTree.Age = 99; Foo2(ref aTree); System.Console.WriteLine(aTree.Age);// 100 }

private static void Foo2(ref Tree A) { A.Age = A.Age + 1; // 100 A = new Tree(); }}

Case 4: pass-by-ref with a Case 4: pass-by-ref with a reference typereference typeReference to reference Reference to reference

is passed…is passed…

Stack

aTree Tree

A

Tree

Out ParametersOut Parameters C# OnlyC# Only Useful if you want method to return more than one valueUseful if you want method to return more than one value Example:Example:

char FirstChar;char FirstChar;char SecondChar;char SecondChar;SplitString( “AB”, out FirstChar, out SecondChar );SplitString( “AB”, out FirstChar, out SecondChar );

Void SplitString( string input, char out FC, char out SC )Void SplitString( string input, char out FC, char out SC ){{

FC = input[0];FC = input[0];SC = input[1];SC = input[1];

}}

DEMODEMO Struct vs. ClassStruct vs. Class OUT paramOUT param

Constructors / DestructorsConstructors / Destructors

ConstructorsConstructors First method called when type is createdFirst method called when type is created

VB is always “Sub New()”VB is always “Sub New()” C# is a method with same name as classC# is a method with same name as class

Used to initialize values in classUsed to initialize values in class Never returns a valueNever returns a value Can call other methodsCan call other methods

DestructorsDestructors Last method run by a classLast method run by a class

VB is Sub Finalize()VB is Sub Finalize() C# is method with same name as class prefixed by (~)C# is method with same name as class prefixed by (~)

Topics Covered TodayTopics Covered Today

.NET Framework & CLR.NET Framework & CLR .NET Base Class Library.NET Base Class Library Using Classes & StructuresUsing Classes & Structures Using Methods, In/Out, ConstructorUsing Methods, In/Out, Constructor Scope & Access LevelsScope & Access Levels

Garbage CollectionGarbage Collection

Give Away – Visual Studio .NETGive Away – Visual Studio .NET

Scope & Access LevelsScope & Access Levels

After this lesson you will be able to:After this lesson you will be able to: Describe meaning of each access levelDescribe meaning of each access level Explain scopeExplain scope

Scope & Access Levels (2)Scope & Access Levels (2)

There are 4 keywords for type accessThere are 4 keywords for type access PublicPublic PrivatePrivate ProtectedProtected Internal Internal (special)(special)

Internal can be used by itself, or in Internal can be used by itself, or in conjunction with Protected.conjunction with Protected.

Member Access ModifiersMember Access Modifiers PublicPublic

Allows access from anywhereAllows access from anywhere PrivatePrivate

Allows access only from members within the type that defines Allows access only from members within the type that defines itit

ProtectedProtected Equivalent to Private with the addition of access through Equivalent to Private with the addition of access through

inherited objectsinherited objects Internal (Friend in VB)Internal (Friend in VB)

Access from within all objects in AssemblyAccess from within all objects in Assembly Protected InternalProtected Internal

Union of Protected & Internal: all objects from assembly + Union of Protected & Internal: all objects from assembly + members within the type that defines class + inherited class members within the type that defines class + inherited class members.members.

Access Modifiers for Access Modifiers for Nested TypesNested Types protected class Treeprotected class Tree

{{private class Branchprivate class Branch{{}}

}} Usually nested type is used by class that Usually nested type is used by class that

contains it; thus typically private.contains it; thus typically private. Rarely would the type be made publicRarely would the type be made public Will always be bounded by the access Will always be bounded by the access

qualifier of surrounding typequalifier of surrounding type

Topics Covered TodayTopics Covered Today

.NET Framework & CLR.NET Framework & CLR .NET Base Class Library.NET Base Class Library Using Classes & StructuresUsing Classes & Structures Using Methods, In/Out, ConstructorUsing Methods, In/Out, Constructor Scope & Access LevelsScope & Access Levels Garbage Collection (3)Garbage Collection (3)

Give Away – Visual Studio .NETGive Away – Visual Studio .NET

Garbage CollectionGarbage Collection

After this lesson you will be able to:After this lesson you will be able to: Describe how garbage collection manages Describe how garbage collection manages

the reclamation of unused memorythe reclamation of unused memory

Garbage Collection Garbage Collection Automatic memory managementAutomatic memory management When object is no longer reference, it is marked for When object is no longer reference, it is marked for

garbage collectiongarbage collection Always runningAlways running Low priority thread under normal circumstancesLow priority thread under normal circumstances

Moves up in priority temporarily if memory Moves up in priority temporarily if memory becomes limitedbecomes limited

The garbage collector continuously traces reference The garbage collector continuously traces reference tree and disposes of objects containing circular tree and disposes of objects containing circular references in addition to disposing of unreferenced references in addition to disposing of unreferenced objects.objects.

Garbage Collection Garbage Collection

How it worksHow it works

Topics Covered TodayTopics Covered Today

.NET Framework & CLR.NET Framework & CLR .NET Base Class Library.NET Base Class Library Using Classes & StructuresUsing Classes & Structures Using Methods, In/Out, ConstructorUsing Methods, In/Out, Constructor Scope & Access LevelsScope & Access Levels Garbage Collection (3)Garbage Collection (3) Give AwaysGive Aways Imagine CUP Programming CompetitionImagine CUP Programming Competition