Constructors
Initialize stateof an object.
Method with no return value (not even
void).
Name of constructor is similar with class
name.
class Machine {
//no constructor means default constructor
}
class Gear {
//custom default constructor
public Gear() {}
}
7.
Trivia #1
class A{
public A() { Console.WriteLine("A"); } }
class B : A {
public B() { Console.WriteLine("B"); } }
class C : B {
public C() { Console.WriteLine("C"); } }
class Program {
static void Main() {
C c = new C();
}
}
8.
Static Keyword
Classlevel member.
Can be applied to:
Class (static class)
Method (static method)
Field (static field)
Constructor (static constructor)
9.
Trivia #2
class D{
public static int I;
}
class Program {
static void Main() {
D d = new D();
d.I = 9;
}
}
10.
Encapsulation
On
Off
class Machine {
privateGear[] gears;
private int temperature;
public int Temperature {
get { return temperature;}
set { temperature = value;}
}
public void TurnOn() {}
public void TurnOff() {}
}
11.
Encapsulation
Can bedone by making fields private.
Use setter and getter methods.
Use property for better flexibility.
Expose as few as public class members.
12.
Access Modifier
Access ModifierApplied to Description
public Types or type
members
No access restriction. Can be accessed from
any objects, any derived classes and external
assemblies
private Type
members or
nested types
Can only be accessed by the class (or structure)
the defines the item.
protected Type
members or
nested types
Not accessible from other objects, but can be
accessed from derived classes.
internal Type or type
members
Can only be accessed within the current
assembly.
protected
internal
Type
members or
nested types
Can be accessed within the current assembly,
the defining class and derived classes.
13.
Trivia #3
class D{
int I;
}
class Program {
static void Main() {
D d = new D();
d.I = 10;
}
}
Overloading & Overriding
Overload: multiple methods with same
name but different parameter list.
Override: multipe methods in the different
hierarchy with same name and same
parameter list.
20.
Polymorphism
Polymorphism isthe ability of object type
acts as their base class type.
Polymorphism means “many forms”.
Real object type is determined at runtime.
Abstract Class
Classthat can not be instantiated.
Abstract class contains at least one
abstract function.
abstract class Shape {
public abstract void Draw();
}
Interface
a classthat specifies a set of functions that
are to be implemented by other classes to
provide a standardized way of providing
some specific functionality.
25.
Interface Example
public interfaceICanvasDrawable {
void DrawOnCanvas(Canvas c);
}
public class Circle : Shape, ICanvasDrawable {
public void Draw() { Console.WriteLine(“draw circle”);}
public void DrawOnCanvas(Canvas c) {
c.DrawShape(this);
}
}
26.
Type Casting
//Employee is-aPerson
Person p1 = new Employee();
Employee e1 = (Employee) p1;
//as keyword
Employee e2 = p1 as Employee;
//is keyword
if (p1 is Employee) {}
27.
System.Object
Instance Method
of ObjectClass
Meaning
Equals() By default, this method returns true only if the items being
compared refer to the exact same item in memory.
GetHashCode() This method returns an int that identifies a specific object
instance.
GetType() This method returns a Type object that fully describes the
object you are currently referencing.
ToString() This method returns a string representation of this object,
using
the <namespace>.<type name> format (termed the fully
qualified name).
Finalize() This method is called to free any allocated resources before
the
object is destroyed.
28.
References
Troelsen, Andrew.2007. Pro C# 2008 and
the .NET 3.5 Platform, 4th edition.
Cwalina, Krzystof; Abrams, Brad. 2009.
Framework Design Guidelines, 2nd edition.