C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented...

36
C# Programming C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft .NET Introducing Microsoft .NET Overview of C# Overview of C# Using Value-Type variables Using Value-Type variables Statements and Exceptions Statements and Exceptions Methods and Parameters Methods and Parameters Strings Arrays and Collections Strings Arrays and Collections C# and Object Oriented Programming C# and Object Oriented Programming Using Reference-Type Variables Using Reference-Type Variables Creating and Destroying Objects Creating and Destroying Objects inheritance in C# inheritance in C# Aggregations, Namespaces and Advance Scope Aggregations, Namespaces and Advance Scope Operators, Delegates and Events Operators, Delegates and Events Properties and Indexers Properties and Indexers

Transcript of C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented...

Page 1: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

C# Programming C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft .NET Introducing Microsoft .NET Overview of C# Overview of C# Using Value-Type variables Using Value-Type variables Statements and Exceptions Statements and Exceptions Methods and Parameters Methods and Parameters Strings Arrays and Collections Strings Arrays and Collections C# and Object Oriented Programming C# and Object Oriented Programming Using Reference-Type Variables Using Reference-Type Variables Creating and Destroying Objects Creating and Destroying Objects inheritance in C# inheritance in C# Aggregations, Namespaces and Advance Scope Aggregations, Namespaces and Advance Scope Operators, Delegates and Events Operators, Delegates and Events Properties and Indexers Properties and Indexers

Page 2: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

Fundamentals of Object-Fundamentals of Object-Oriented ProgrammingOriented Programming

What is OOPWhat is OOP Objects vs. Classes Objects vs. Classes Instantiation Instantiation Encapsulation Encapsulation Inheritance Inheritance Polymorphism Polymorphism

Page 3: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

What is OOPWhat is OOP

Object-oriented programming (OOP) Object-oriented programming (OOP) is a programming language model is a programming language model organized around "objects" rather organized around "objects" rather than "actions" and data rather than than "actions" and data rather than logic. logic.

The first step in OOP is to identify all The first step in OOP is to identify all the objects you want to manipulate the objects you want to manipulate and how they relate to each other and how they relate to each other

Page 4: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

Objects vs. ClassesObjects vs. Classes

Class HouseOfFunA blue print of the HouseOfFun house.

Object MyHouseAn Instance of HouseOfFun

Object YelloHouse[0] An Instance of HouseOfFun

Object MyHouseAn Instance of HouseOfFun

Object YelloHouse[1]An Instance of HouseOfFun

Page 5: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

Objects vs. ClassesObjects vs. Classespublic class HouseOfFun{

public HouseOfFun(string city, int rooms, int buildYear)

{this.city = city;this.rooms = rooms;this.buildYear = buildYear;

}protected string city;protected int rooms;protected int buildYear;

public int calcHouseAge(){

// calculation just for this samplereturn (DateTime.Now.Year -

buildYear);}

}

Page 6: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

InstantiationInstantiation

class mainClass{

static void Main( ){

HouseOfFun myHouse = new HouseOfFun("Tel Aviv",2,1963);

Console.WriteLine(myHouse.calcHouseAge());}

}

Page 7: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

EncapsulationEncapsulation

The ability of an object to hide its The ability of an object to hide its internal data and methods, making internal data and methods, making only the intended parts of the object only the intended parts of the object programmatically accessible. programmatically accessible.

C# C# PublicPublic Private Private protected protected

Page 8: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

InheritanceInheritance

Subclass that is defined can inherit the Subclass that is defined can inherit the definitions of one or more general definitions of one or more general classes.classes.

An object in a subclass need not carry An object in a subclass need not carry generic definition of data and methods generic definition of data and methods

speeds up program development;speeds up program development; ensures an inherent validity (what ensures an inherent validity (what

works and is consistent about the class works and is consistent about the class will also work for the subclass).will also work for the subclass).

Page 9: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

PolymorphismPolymorphism

Polymorphism gives you the ability Polymorphism gives you the ability to group objects that have a common to group objects that have a common base class and treat them base class and treat them consistently.consistently.

Polymorphism allows you to extend Polymorphism allows you to extend or enhance your system without or enhance your system without modifying or breaking existing code.modifying or breaking existing code.

Page 10: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

Encapsulation, Inheritance, Encapsulation, Inheritance, Polymorphism Polymorphism

Sample page 1/3: class with virtual Sample page 1/3: class with virtual methodmethodclass Teacher

{public Teacher(string firstName,string lastName,int

practiceYears,double payRate){

this.firstName = firstName;this.lastName = lastName;this.practiceYears = practiceYears;this.payRate = payRate;

}protected string firstName;protected string lastName;protected int practiceYears;protected double payRate;

public virtual double CalculatePayment(int hourseWorked){

// do somethingreturn 1,000,000.00; // bogus value

}}

Page 11: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

Encapsulation, Inheritance, Encapsulation, Inheritance, Polymorphism - 2/3Polymorphism - 2/3

FullTimeTeacher is derived from FullTimeTeacher is derived from TeacherTeacherclass FullTimeTeacher:Teacher

{public FullTimeTeacher(string firstName,string lastName,int practiceYears,double payRate):base (firstName,lastName, practiceYears,payRate){}

public override double CalculatePayment(int hourseWorked) { //do something }}class PartTimeTeacher:Teacher{

public FullTimeTeacher(string firstName,string lastName,int practiceYears,double payRate):base (firstName,lastName, practiceYears,payRate){}

public override double CalculatePayment(int hourseWorked) { //do something }}

Page 12: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

Encapsulation, Inheritance, Polymorphism 3/3Encapsulation, Inheritance, Polymorphism 3/3

- teachers group hold FullTimeTeacher & - teachers group hold FullTimeTeacher & PartTimeTeacherPartTimeTeacher

- CalculatePayment code stays the same - CalculatePayment code stays the same class PolymorphismSample{

protected Teacher[] teachers; protected void LoadTeachers(){

//in a real world situation we will use the database teachers = new Teacher[2];teachers[0] = new FullTimeTeacher("Marta",

"Kohen", 25, 2500.00);teachers[2] = new PartTimeTeacher("Bar",

"Shalom", 40, 50.00);} protected void CalculatePayment(){

foreach(Teacher tcr in teachers){

tcr.CalculatePayment(40);}

} }

Page 13: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

Introducing Introducing Microsoft .NET Microsoft .NET

The .NET Framework The .NET Framework The Common Language Runtime The Common Language Runtime The .NET Framework Class Libraries The .NET Framework Class Libraries Microsoft Intermediate Language Microsoft Intermediate Language

and the Jitters and the Jitters Unified Type System Unified Type System Metadata and Reflection Metadata and Reflection

Page 14: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

The .NET FrameworkThe .NET Framework

ASP.NET Windows Forms

Data and XML

Base Classes

Common Language Runtime

Page 15: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

The Common Language The Common Language RuntimeRuntime

Common Type System

GC, Stack Walk, Code Manager

Class Loader & Memory Layout

Intermediate Languageto

Native Code Compilers

ExecutionSupport

Security

Page 16: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

The .NET Framework Class The .NET Framework Class LibrariesLibraries

.Net Class Libraries provides language interoperability.

Sample:

The Class shown in this class Browser

System.Data.OleDb.OleDbConnection

can be used by all the many .Net languages

Page 17: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

Microsoft Intermediate Microsoft Intermediate Language and the JittersLanguage and the Jitters

Page 18: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

Unified Type SystemUnified Type System

Page 19: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

Metadata and ReflectionMetadata and Reflection

Page 20: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

Overview of C#Overview of C#

Structure of a c# program Structure of a c# program Basic Input/Output Operations Basic Input/Output Operations Recommended Practices Recommended Practices Compiling Running and Debugging Compiling Running and Debugging

Page 21: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

Using Value-Type Using Value-Type variablesvariables

Common Type System Common Type System Naming Variables Naming Variables Using Built-in Data Types Using Built-in Data Types Compound Assignment Compound Assignment Increment and Decrement Increment and Decrement Creating User Defined Data Types Creating User Defined Data Types Converting Data Types Converting Data Types

Page 22: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

Statements and Statements and ExceptionsExceptions

Selection Statements Selection Statements Iteration Statements Iteration Statements Jump Statements Jump Statements Handling Basic Exceptions Handling Basic Exceptions Raising Exceptions Raising Exceptions

Page 23: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

Methods and ParametersMethods and Parameters

Methods Methods Parameters Parameters Overload Methods Overload Methods

Page 24: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

Strings Arrays and Strings Arrays and CollectionsCollections

Strings Strings Creating Arrays Creating Arrays Using Arrays Using Arrays Collections Collections .NET Framework Arrays .NET Framework Arrays .Net Framework Collections .Net Framework Collections working with Strings, Enumerators working with Strings, Enumerators

and Collections and Collections

Page 25: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

C# and Object Oriented C# and Object Oriented ProgrammingProgramming

Creating and using Classes Creating and using Classes

Page 26: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

Using Reference-Type Using Reference-Type VariablesVariables

Reference-Type Variables Reference-Type Variables Common Reference Types Common Reference Types The Object Hierarchy The Object Hierarchy Namespaces in the .Net Framework Namespaces in the .Net Framework Data Conversions Data Conversions Type-Safe Casting Type-Safe Casting

Page 27: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

Creating and Destroying Creating and Destroying ObjectsObjects

Constructors Constructors Initializing Data Initializing Data Objects and Memory Objects and Memory Destructors Destructors

Page 28: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

inheritance in C# inheritance in C#

Deriving Classes Deriving Classes Implementing Methods Implementing Methods Sealed Classes Sealed Classes Interfaces Interfaces Abstract Classes Abstract Classes

Page 29: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

Aggregations, Namespaces Aggregations, Namespaces and Advance Scopeand Advance Scope

Using internal classes, Methods and Using internal classes, Methods and Data Data

Using Aggregation Using Aggregation Using Namespaces Using Namespaces Using Modules and Assemblies Using Modules and Assemblies

Page 30: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

Operators, Delegates and Operators, Delegates and EventsEvents

Operators Operators Operator Overloading Operator Overloading Delegates Delegates Events Events When to use Delegates, Events and When to use Delegates, Events and

Interfaces Interfaces

Page 31: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

Properties and IndexersProperties and Indexers

properties properties Indexers Indexers

Page 32: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

ReviewReview

Page 33: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

C# Advance C# Advance

Attributes Attributes The SDK Tools The SDK Tools

Page 34: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

AttributesAttributes

Overview of attributes Overview of attributes Defining Custom Attributes Defining Custom Attributes Retrieving Attributes Values Retrieving Attributes Values

Page 35: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

The SDK ToolsThe SDK Tools

Configuration and Deployment Tools Configuration and Deployment Tools Debugging Tools Debugging Tools Security Tools and Utilities Security Tools and Utilities General Tools General Tools

Page 36: C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.

ReviewReview