Loading…
Transcript

Not always a solution, especially when

there are variable number of arguments

Normal Arrays

Parameter arrays

-params cannot be used with multidimensional arrays

cant overload a method solely based on param

ref or out modifier cannot be specified

params array must be last parameter

non-params method always takes priority over params method

params[] object

to hold variable number of parameters of any type

Console.WriteLine(string format, params object[ ] arg)

Combining parameter arrays and optional parameters

-parameter list fixed or variable

-default values

Inheritance in programming is all about classification- relationship between classes

Specific:

Horse: hooves

Whale: flippers and fluke

class DerivedClass:BaseClass

{

.....

}

structures cannot be inherited

System.object is the root of all classes

class Base

{

public Base(string s) { }

}

class Derived:Base

{

public Derived():Base(string s){ }

}

Assigning classes:

-base class variable can refer to derived class object

reverse not true!!

- base class variable can still call only base class objects

-as keyword for safe cast between base and derived class

suppress warnings using new

keywords:

virtual and override

rules :

- virtual and override methods cannot be private

- signatures of virtual and override methods must be identical

- one can only override a virtual method

- override keyword is compulsory otherwise its hiding

- override method is implicitly virtual

Dynamic method dispatch

Protected

with inheritance - protected behaves as public

without inheritance - protected behaves as private

Interfaces

order the objects in the collection not collection itself

give a method prototype int compareTo(object o)

Interfaces

- seperate what from how

Defining interfaces

- use keyword interface

-just method declarations no implementation

- no access specifiers (implictly public)

- no data fields

- convention use I for start of an interface

Implementing interfaces:

- notation similar to inheritance

- class/struct can implement inheritance

-method name and return types should match

- ref and out parameters if used must match

- implemented methods must be public accessible

- class can inherit and implement at the same time

Interface Extension

Referencing a class through its interface

- interface refering object

- designing general methods

- using is to check valid references

Multiple interfacing

-not alllowed to define any fields

- not allowed to define any constructors

- not allowed to define any destructor

- not specify any access specifers for any method

- cannot nest any types

- not allowed to inherit from any class/struct

Abstract Classes

-no objects can be created

- can contain abstract methods

-abstract methods can enforce overriding

Sealed

sealed classes - no inheritance further

sealed methods - no overriding further

interface - introduces method name

virtual method - first implementation

override - another implementation

sealed - last implementation

Parameter Arrays, Inheritance, Interfaces and Garbage Collection

Parameter Arrays

Inheritance

Common:

- breathe air

- warm blooded

- suckle young (nourish)