Writing Object Oriented Software with C#. C# and OOP C# is designed for the.NET Framework  The.NET Framework is Object Oriented In C#  Your access to.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance.
Portability and Safety Mahdi Milani Fard Dec, 2006 Java.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
C# Structs, operator overloading & attributes. Structs ~ Structures Structs are similar to classes: they represent data structures with data and functions.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Advanced Object-Oriented Programming Features
OOP Project Develop an Application which incorporates the following OO Mechanisms and Principals: – Classes Visibility Constructor(s) Class Variable (if.
C# Programming: From Problem Analysis to Program Design1 10 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
ASP.NET Programming with C# and SQL Server First Edition
VB Classes ISYS 512. Adding a Class to a Project Project/Add Class –*** MyClass is a VB keyword. Steps: –Adding properties Declare Public variables in.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C++ fundamentals.
Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 20 Microsoft’s Approach 3 – C# Rob Pooley
C# Event Processing Model Solving The Mystery. Agenda Introduction C# Event Processing Macro View Required Components Role of Each Component How To Create.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
OOP Languages: Java vs C++
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
BIM313 – Advanced Programming Techniques Object-Oriented Programming 1.
Visual Programming Fall 2012 – FUUAST Topic: Development environment.
Tutorial C#. PLAN I. Introduction II. Example of C# program :Hello World III. How to use C# language GUI elements Primitives Types Expressions and operators.
C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Programming Pillars Introduction to Object- Oriented Programming.
Programming Languages and Paradigms Object-Oriented Programming.
1 Classes and Controls CE-105 Spring 2007 By: Engr. Faisal ur Rehman.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
E FFECTIVE C# 50 Specific Ways to Improve Your C# Second Edition Bill Wagner محمد حسین سلطانی.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Object Oriented Software Development
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
C# Programming: From Problem Analysis to Program Design1 10 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
Introducing Allors Applications, Tools & Platform.
Module 8: Delegates and Events. Overview Delegates Multicast Delegates Events When to Use Delegates, Events, and Interfaces.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Neal Stublen Tonight’s Agenda  Indexers  Delegates and events  Operator overloading  Class inheritance  Q&A.
Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's.
Introduction to Object-Oriented Programming Lesson 2.
Interfaces An interface is like an extreme case of an abstract class – However, an interface is not a class – It is a type that can be satisfied by any.
Object-Based Programming in VB.NET. Must Understand Following: Encapsulation Information hiding Abstract Data Type Class, Instance, Reference Variable.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
2.7 Inheritance Types of inheritance
Module 5: Common Type System
Methods Attributes Method Modifiers ‘static’
Object-Orientated Programming
CS360 Windows Programming
Tutorial C#.
Overloading and Constructors
Lecture 22 Inheritance Richard Gesick.
Fundaments of Game Design
CIS 199 Final Review.
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Writing Object Oriented Software with C#

C# and OOP C# is designed for the.NET Framework  The.NET Framework is Object Oriented In C#  Your access to the OS is through objects  You have the ability to create first class objects  The FCL is designed for extension and integration by your code

Defining Classes class Name:BaseType{ // Members } Namespace NameName{ class Name:BaseType{ } class MyType{ public static String someTypeState; public Int32 x; public Int32 y; }

Accessibility In C#, private is the default accessibility Accessibilities options  public – Accessible to all  private – Accessible to containing class  protected – Accessible to containing or derived classes  internal – Accessible to code in same assembly  protected internal – means protected or internal Classes can be marked as public or internal  By default they are private  Accessible only to code in the same source module

Type Members in C# Fields  The state of an object or type Methods  Constructors  Functions  Properties (smart fields) Members come in two basic forms  Instance – per object data and methods Default  Static – per type data and methods Use the static keyword

Methods Declared inline with type definition No inline keyword, methods are inlined when appropriate by the JIT compiler class MyType{ public Int32 SomeMethod(){ return x; } public static void StaticMethod(){ // Do something }

Properties Methods that look like fields (smart fields) Can have read-only or write-only properties class Point{ Int32 x; Int32 y; public Int32 X{ get{return x;} set{x = value;} } public Int32 Y{ get{return y;} set{y = value;} }

Demo Classes and Properties Point-0.cs

Instance Constructors Constructors are used to initialize fields You can implement simpler constructors in terms of more complex ones with the this keyword (suggested) You can indicate which base constructor to call  Use the base keyword class Point{ Int32 x; Int32 y; public Point():this(0, 0){} public Point(Int32 x, Int32 y){ this.x = x; this.y = y; }

Type (static) Constructors Type constructors are used to initialize static fields for a type Only one static constructor per type  Called by the Common Language Runtime  Guaranteed to be called before any reference to the type or an instance of the type  Must have no parameters Use the static keyword to indicate a type constructor

Derivation and Object All types in the system are derived from Object You can specify a base class  Without a base class the compiler assumes Object Object reference variables are used as generic references  Collection classes in the Framework Class Library Object implements useful methods like  ToString(), GetType()  ReferenceEquals()

Polymorphism and Virtual Functions Use the virtual keyword to make a method virtual In derived class, override method is marked with the override keyword Example  ToString() method in Object class  Example derived class overriding ToString() public virtual string ToString(); class SomeClass:Object{ public override String ToString(){ return “Some String Representing State”; } Polymorphism.cs

C# and Events C# has built in support for events Great for dealing with objects in an event-driven operating system Improved performance and flexibility over an all- virtual-function solution More than one type can register interest in a single event A single type can register interest in any number of events

Handling an Event using System; using System.Windows.Forms; class MyForm:Form{ MyForm(){ Button button = new Button(); button.Text = "Button"; button.Click += new EventHandler(HandleClick); Controls.Add(button); } void HandleClick(Object sender, EventArgs e){ MessageBox.Show("The Click event fired!"); } public static void Main(){ Application.Run(new MyForm()); } EventHand.cs

Demo EventHand.cs

Defining an Event Based on a callback mechanism called a delegate class EventInt{ Int32 val; public Int32 Value{ get{return val;} set{ if(Changed != null) Changed(value, val); val = value; } public event Callback Changed; public delegate void Callback(Int32 newVal, Int32 oldVal); } EventInt.cs

Callback Methods (Delegates) using System; delegate void MyDelegate(String message); class App{ public static void Main(){ MyDelegate call = new MyDelegate(FirstMethod); call += new MyDelegate(SecondMethod); call("Message A"); call("Message B"); } static void FirstMethod(String str){ Console.WriteLine("1st method: "+str); } static void SecondMethod(String str){ Console.WriteLine("2nd method: "+str); } Delegates.cs

Interfaces C# supports interfaces  Your types can implement interfaces Must implement all methods in the interface  You can define custom interfaces Interfaces can contain methods but no fields  Properties and events included  Constructors are not supported in interfaces Use the interface keyword interface Name{ // Members } Sortable.cs

Operator Overloading and Type Conversion C# allows you to write operator overload methods Called when a custom type is used in an expression with operators  Can overload: +, -, *, |, etc. Can create custom cast methods  Implicitly or explicitly convert your type to another type Overloading.csTypeConverters.cs

C# and OOP C# and the.NET Framework promote component development  Can use binary or pre-compiled objects  More applications will use more components  Creates a market for third-party component venders  Strong security story allows for internet deployment of objects C# has a great set of tools for the object oriented programmer

Writing Object Oriented Software with C#

Hidden Slides are Originals for Art in Tutorial.DOC file

Bus Car TypesInstances (objects) Honda School Grey- Hound Chevy Ford Types include methods and description of fields for objects. Stored in Exe. Instances are in-memory data. Includes data for fields and refers to type information.

Automobile Machine Fields and Methods String Description; FuelType Fuel; Double EffeciencyQuotient Fields and Methods String Make; String Model; A type defines a number of fields and methods. A derived type inherits the base type’s fields and methods, and adds a few of its own, to become a new type- extension of an existing type.