Writing Object Oriented Software With C#
Writing Object Oriented Software With C#
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
class MyType{
public Int32 SomeMethod(){
return x;
}
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)
class Point{
Int32 x;
Int32 y;
EventInt.cs
Callback Methods (Delegates)
Delegates.cs
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);
}
}
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
Propertiesand events included
Constructors are not supported in interfaces
Overloading.cs TypeConverters.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
Types Instances (objects)
Honda School
Bus Grey-
Hound
Chevy
Ford
Car