Sie sind auf Seite 1von 65

Object Oriented Concepts C#

Nauzad Kapadia

Interfaces
An interface defines a contract
An interface is a type Can declare variables of that type
However, you cannot directly instantiate an interface

Includes methods, properties, indexers, events Any class or struct implementing an interface must support all parts of the contract

Interfaces provide no implementation


When a class or struct implements an interface it must provide the implementations for all members of the interface

Interfaces provide polymorphism


Many classes and structs may implement a particular interface

Interfaces
Example
public interface IDisposable { void Dispose(); } public class TextBox : IDisposable { public void Dispose() { ... } } public class Car : IDisposable { public void Dispose() { ... } } void PolyDispose(IDisposable id) { id.Dispose(); } TextBox tb = new TextBox(); PolyDispose(tb);
Car c = new Car(); PolyDispose(c);

Interfaces
Casting
A class that implements an interface can be cast (converted) to any of those interfaces

public interface IDisposable { } public interface IEnumerable { } public class ListBox : IDisposable, IEnumerable { } ListBox lb = new ListBox(); IDisposable id = (IDisposable)lb; IEnumerable ie = (IEnumerable)lb;

Interfaces

Multiple Inheritance
Interfaces can inherit from multiple interfaces Classes and structs can inherit from multiple interfaces

interface ITextBox { void SetText(string s); } interface IListBox { void SetItems(string[] items); } interface IComboBox: ITextBox, IListBox { }

Classes and Structs


Similarities
Both are user-defined types Both can contain
Data
Fields, constants, events, arrays

Functions
Methods, properties, indexers, operators Constructors, finalizers

Type definitions
Classes, structs, enums, interfaces, delegates

Both can implement multiple interfaces Both are instantiated with new operator

Classes and Structs


Differences

Class
Reference type Can inherit from any non-sealed reference class Can have a finalizer Can have user-defined parameterless constructor Value type

Struct

No inheritance (inherits only from System.ValueType) No finalizer

No user-defined parameterless constructor

Classes and Structs


C# Structs vs. C++ Structs
Very different from C++ struct

C++ Struct
Same as C++ class, but all members are public Can be allocated on the heap, on the stack or as a member (can be used as value or reference), just like C++ class Members are always public

C# Struct
User-defined value type

Always allocated on the stack

Members can be public, internal or private

Classes and Structs


Class
public class Car : Vehicle { public enum Make { GM, Honda, BMW } Make make; string vid; Point location; Car(Make m, string vid; Point loc) { this.make = m; this.vid = vid; this.location = loc; } Car c = new Car(Car.Make.BMW, JF3559QT98, new Point(3,7)); c.Drive();

public void Drive() { Console.WriteLine(vroom); } }

Classes and Structs


Struct

public struct Point { int x, y; public Point(int x, int y) { this.x = x; this.y = y; } public int X { get { return x; } set { x = value; } } public int Y { get { return y; } set { y = value; } } } Point p = new Point(2,5); p.X += 100; int px = p.X; // px = 102

Classes and Structs

Static vs. Instance Members


By default, members are per instance
Each instance gets its own fields Methods apply to a specific instance

Static members are per type


Static methods cant access instance data No this variable in static methods

Dont abuse static members


They are essentially object-oriented global data and global functions

Classes and Structs


Access Modifiers
Access modifiers specify who can use a type or a member Access modifiers control encapsulation Top-level types (those directly in a namespace) can be public or internal Class members can be public, private, protected, internal, or protected internal Struct members can be public, private or internal

Classes and Structs


Access Modifiers

If the access modifier is


public private

Then a member defined in type T and assembly A is accessible


to everyone within T only (the default)

protected
internal protected internal

to T or types derived from T


to types within A to T or types derived from T or to types within A

Classes and Structs


Abstract Classes
An abstract class is one that cannot be instantiated Intended to be used as a base class May contain abstract and non-abstract function members Similar to an interface Cannot be sealed

Classes and Structs


Sealed Classes
A sealed class is one that cannot be used as a base class Sealed classes cant be abstract All structs are implicitly sealed System.String is sealed Why seal a class?
To prevent unintended derivation Code optimization
Virtual function calls can be resolved at compile-time

Classes and Structs


this
The this keyword is a predefined variable available in non-static function members
Used to access data and function members unambiguously

class Person { string name; public Person(string name) { this.name = name; } public void Introduce(Person p) { if (p != this) Console.WriteLine(Hi, Im + name); } }

Classes and Structs


base
The base keyword is used to access class members that are hidden by similarly named members of the current class

class Shape { int x, y; public override string ToString() { return "x=" + x + ",y=" + y; } } class Circle : Shape { int r; public override string ToString() { return base.ToString() + ",r=" + r; } }

Classes and Structs


Fields
A field is a member variable Holds data for a class or struct Can hold:
a reference type (e.g. class instance), a value type (e.g. a struct instance), or an array of class or struct instances (an array is actually a reference)

Classes and Structs


Constants
A constant is a data member that is evaluated at compile-time and is implicitly static (per type)
e.g. Math.PI

public class MyClass { public const string version = 1.0.0; public const string s1 = abc + def; public const int i3 = 1 + 2; public const double PI_I3 = i3 * Math.PI; public const double s = Math.Sin(Math.PI); ... }

//ERROR

Classes and Structs


Readonly Fields
Similar to a const, but is initialized at run-time in its declaration or in a constructor
Once initialized, it cannot be modified

Differs from a constant


Initialized at run-time (vs. compile-time)
Dont have to re-compile clients

Can be static or per-instance


public class MyClass { public static readonly double d1 = Math.Sin(Math.PI); public readonly string s1; public MyClass(string s) { s1 = s; } }

Classes and Structs


Properties
A property is a virtual field Looks like a field, but is implemented with code

public class Button: Control { private string _caption; public string caption { get { return _caption; } set { _caption = value; Repaint(); } } } Button b = new Button(); b.caption = "OK"; String s = b.caption;

Properties
Access to private fields normally requires accessors and mutators Properties allow access to private fields as if they were public

public class Person { private int age; public int Age { Property get { return age; } set { age = value; Console.WriteLine(age); } } }

Person p = new Person(); p.Age = 27; p.Age++;

C#

instead of:

Other

Person p = new Person(); p.setAge(27); p.setAge(p.getAge() + 1);

Classes and Structs


Properties
Properties can be:
Read-only (implement only get)
Not the same as a readonly field

Write-only (implement only set) Read/write (implement both get and set)

Classes and Structs


Indexers
An indexer lets an instance behave as a virtual array Can be overloaded (e.g. index by int and by string) Can be public class ListBox: Control { read-only, private string[] _items; public string this[int index] { write-only, get { return _items[index]; } or read/write set { _items[index] = value; Repaint(); } } } ListBox listBox = new ListBox(); listBox[0] = "hello"; Console.WriteLine(listBox[0]);

Indexers
Index an object as if it were an array

C#
public class numbers { private int[3] nums; public int this[int index] { get { return nums[index]; } set { nums[index] = value; } } } Numbers Num = new Numbers(); Num[0] = 5; int Val = Num[0];

instead of:
Numbers Num = new Numbers(); Num.Set_Nums(0,5); int Val = Num.Get_Nums(0);

Indexer

Other

Classes and Structs


Methods
All code executes in a method
Constructors, finalizers and operators are special types of methods Properties and indexers are implemented with get/set methods

Methods have argument lists Methods contain statements Methods can return a value
Only if return type is not void

Classes and Structs


Method Argument Passing
By default, data is passed by value A copy of the data is created and passed to the method For value types that are passed by value, variables cannot be modified by a method call For reference types that are passed by value, the instance can be modified by a method call, but the variable itself cannot be modified by a method call

Classes and Structs


Method Argument Passing
The ref modifier causes arguments to be passed by reference Allows a method call to modify a variable Applies to both value and reference types Have to use ref modifier in method definition and in the code that calls it Variable has to have a value before call void RefFunction(ref int p) { p++; int x = 10; } RefFunction(ref x); // x is now 11

Classes and Structs


Method Argument Passing
The out modifier causes arguments to be passed out by reference Allows a method call to initialize a variable Have to use out modifier in method definition and the code that calls it Argument has to have a value before returning

void OutFunction(out int p) { p = 22; int x; } OutFunction(out x); // x is now 22

Classes and Structs


Overloaded Methods
A type may overload methods, i.e. provide multiple methods with the same name Each must have a unique signature Signature is based upon arguments only, the return value is ignored
void Print(int i); void Print(string s); void Print(char c); void Print(float f); int Print(float f); // Error: duplicate signature

Classes and Structs


Parameter Arrays
Methods can have a variable number of arguments, called a parameter array params keyword declares parameter array Must be the last argument

int Sum(params int[] intArr) { int sum = 0; foreach (int i in intArr) sum += i; return sum; int sum = Sum(13,87,34); }

Classes and Structs


Abstract Methods
An abstract method is virtual and has no implementation Must belong to an abstract class Intended to be implemented in a derived class

Classes and Structs


Abstract Methods

abstract class Shape { public abstract void Draw(); } class Box : Shape { public override void Draw() { ... } } class Sphere : Shape { public override void Draw() { ... } } void HandleShape(Shape s) { s.Draw(); ... } HandleShape(new Box()); HandleShape(new Sphere()); HandleShape(new Shape()); // Error!

Classes and Structs

Overloaded vs. Overridden Methods


Unlike method overriding, method overloading depends upon the type of the variable, NOT on the type of the object
Method overloading is resolved at compile-time

However, at run-time, if the overloaded method is virtual, then the method that is called is determined by the type of the object
Method overriding is resolved at run-time

Classes and Structs


Method Versioning
Must explicitly use override or new keywords to specify versioning intent Avoids accidental overriding Methods are non-virtual by default C++ and Java produce fragile base classes cannot specify versioning intent

Method Versioning in C#
class Base // // v1.0 v2.0 { } public virtual void int Foo() Foo() { Database.Log("Base.Foo"); return 0; } }

v1.0 class Derived : Base // v2.0 { new virtual void void Foo() Foo() public virtual override void Foo() { Console.WriteLine("Derived.Foo"); super.Foo(); } Console.WriteLine("Derived.Foo"); } } }

Classes and Structs


Constructors
Instance constructors are special methods that are called when a class or struct is instantiated Performs custom initialization Can be overloaded If a class doesnt define any constructors, an implicit parameterless constructor is created Cannot create a parameterless constructor for a struct
All fields initialized to zero/null

Classes and Structs


Constructors
Order of construction:
1. 2. 3.

Instance variables are initialized Base constructor is called Body of constructor is called

All during construction the object is considered to be the constructed type


Avoid calling virtual methods in constructors
The object may not be property initialized

This is like Java, but unlike C++

Classes and Structs


Constructor Initializers
One constructor can call another with a constructor initializer Can call this(...) or base(...) Default constructor initializer is base()

class B { private int h; public B() { } public B(int h) { this.h = h; } } class D : B { private int i; public D() : this(24) { } public D(int i) { this.i = i; } public D(int h, int i) : base(h) { this.i = i; } }

Classes and Structs


Static Constructors
A static constructor lets you create initialization code that is called once for the class Guaranteed to be executed before the first instance of a class or struct is created and before any static member of the class or struct is accessed No other guarantees on execution order Only one static constructor per type Must be parameterless

Classes and Structs


Finalizers
A finalizer is a method that is called before an instance is garbage collected
Different from a C++ destructor

Used to clean up any resources held by the instance, do bookkeeping, etc. Only classes, not structs, can have finalizers
class Foo { ~Foo() { Console.WriteLine(Destroyed {0}, this); } }

Classes and Structs


Finalizers
Unlike C++, C# finalizers are non-deterministic They are not guaranteed to be called at a specific time (i.e. they arent called when an instance goes out of scope) They are guaranteed to be called before shutdown (...kind of) Use the using statement and the IDisposable interface to achieve deterministic finalization

Classes and Structs


Operator Overloading
User-defined operators Must be a static method

class Car { string vid; public static bool operator ==(Car x, Car y) { return x.vid == y.vid; } }

Classes and Structs


Operator Overloading
Overloadable unary operators

+ true

false

! ++

~ --

Overloadable binary operators


+ % << & >> * | < / ^ > ! == <= ~ != >=

Classes and Structs


Operator Overloading
No overloading for member access, method invocation, assignment operators, nor these operators: sizeof, new, is, as, typeof, checked, unchecked, &&, ||, and ?: The && and || operators are automatically evaluated from & and | Overloading a binary operator (e.g. *) implicitly overloads the corresponding assignment operator (e.g. *=)

Classes and Structs


Operator Overloading
Example

struct Vector { int x, y; public Vector(x, y) { this.x = x; this.y = y; } public static Vector operator +(Vector a, Vector b) { return Vector(a.x + b.x, a.y + b.y); } ... }

Classes and Structs


Conversion Operators
User-defined explicit and implicit conversions

class Note { int value; // Convert to public static return ...; } // Convert to public static return ...; } }

hertz no loss of precision implicit operator double(Note x) {

nearest note explicit operator Note(double x) { Note n = (Note)442.578; double d = n;

Classes and Structs


is Operator
The is operator is used to dynamically test if the runtime type of an object is compatible with a given type

static void DoSomething(object o) { if (o is Car) ((Car)o).Drive(); }

Dont abuse the is operator: it is preferable to design an appropriate type hierarchy with polymorphic methods

Classes and Structs


as Operator
The as operator tries to convert a variable to a specified type; if no such conversion is possible the result is null
static void DoSomething(object o) { Car c = o as Car; if (c != null) c.Drive(); }

More efficient than using is operator: test and convert in one operation Same design warning as with the is operator

Classes and Structs


typeof Operator
The typeof operator returns the System.Type object for a specified type Can then use reflection to dynamically obtain information about the type
Console.WriteLine(typeof(int).FullName); Console.WriteLine(typeof(System.Int32).Name); Console.WriteLine(typeof(float).Module); Console.WriteLine(typeof(double).IsPublic); Console.WriteLine(typeof(Car).MemberType);

Use GetType() to get the type of an instance

Agenda
Review Object-Oriented Concepts Interfaces Classes and Structs Delegates Events

Delegates
Object oriented function pointers
A reference type encapsulating a method signature and return value

Can point to multiple functions


Thread-safe + and - operations

Foundation for events

Delegate
delegate double Func(double x);

Multicast Delegate
delegate void Greeting();

Func func = new Func(Math.Sin); double x = func(1.0);


public class math { double Sin(double x) { //return (sin x) } }

Greeting greeting; greeting += new Greeting(A.SayHello); greeting += new Greeting(A.Bow);


public class a { void SayHello() { //Say Hello } void Bow() { // bow } }

Agenda
Review Object-Oriented Concepts Interfaces Classes and Structs Delegates Events

Events
Specialized multicast delegate Represent a signal that something has occurred in a program Two main issues
Generating events Consuming events

Events
Overview
C# has native support for events Based upon delegates An event is essentially a field holding a delegate However, public users of the class can only register delegates
They can only call += and -= They cannot invoke the events delegate
Only the object containing the event can invoke the delegate

Multicast delegates allow multiple objects to register with the same event

Events
Create a Type defining the information to be passed to receivers of the event.
public class MailManager { public class MailMsgEventArgs : EventArgs { public readonly string from, to, body; public MailMsgEventArgs(string From, string To, string Body) { this.from = From; this.to = To; this.body = Body;

}
} }

Events
Sourcing
Define the event signature
public delegate void MailMsgEventHandler(object sender, MailMsgEventArgs e);

Define the event and firing logic


public class MailManager { public event MailMsgEventHandler MessageReceived;

protected void OnMessageReceived(MailMsgEventArgs e) { if (MessageReceived != null) MessageReceived(this, e); }


}

Events
Create a Method that is responsible for raising the event.
public void SimulateArrivingMsg (string from, string to, string body) { MailMsgEventArgs e = new MailMsgEventArgs(from, to, body);

OnMailMessageReceived(e);
}

Events
Handling
Define and register event handler

public class MyForm: Form { MailManager FaxApplication; public MyForm() { FaxApplication = new MailManager(); FaxApplication.MessageReceived += new MailMsgEventHandler(FaxMsg); } void FaxMsg(object sender, MailMsgEventArgs e) { MessageBox.Show("You recd a msg from + e.from); } }

Attributes
How do you associate information with types and members?
Documentation URL for a class Transaction context for a method XML persistence mapping

Traditional solutions
Add keywords or pragmas to language Use external files, e.g., IDL, DEF

C# solution: Attributes

Attributes
Appear in square brackets Attached to code elements [HelpUrl(http://SomeUrl/Docs/SomeClass)] class SomeClass { [WebMethod] void GetCustomers() { } string Test([SomeAttr] string param1) {} }

Attribute Fundamentals
Can be Intrinsic or Custom Attributes are generic classes!
class HelpUrl : System.Attribute { public HelpUrl(string url) { } }

Easy to attach to types and members


[HelpUrl(http://SomeUrl/APIDocs/SomeClass)] class SomeClass { }

Attributes can be queried at runtime


Type type = Type.GetType(SomeClass); Attribute[] attributes = type.GetCustomAttributes();

Creating Attributes
Attributes are simply classes
Derived from System.Attribute Class functionality = attribute functionality

public class HelpURLAttribute: System.Attribute { public HelpURLAttribute(string url) { } public string URL { get { } } public string Tag { get { } set { } } }

Using Attributes
Just attach it to a class
[HelpURL(http://someurl/)] Class MyClass { }

Use named parameters


[HelpURL(http://someurl/, Tag=ctor)] Class MyClass { }

Use multiple attributes


[HelpURL(http://someurl/), HelpURL(http://someurl/, Tag=ctor)] Class MyClass { }

Querying Attributes
Use reflection to query attributes
Type type = typeof(MyClass); foreach(object attr in type.GetCustomAttributes()) { if(attr is HelpURLAttribute) { HelpURLAttribute ha = (HelpURLAttribute) attr; myBrowser.Navigate(ha.URL); } }

Das könnte Ihnen auch gefallen