SlideShare ist ein Scribd-Unternehmen logo
1 von 129
Downloaden Sie, um offline zu lesen
Mohammad Shaker
mohammadshaker.com
C# Programming Course
@ZGTRShaker
2011, 2012, 2013, 2014
C# Starter
L02 – Classes, Polymorphism, Versioning,
Interfaces, Reference and Value Types
Today’s Agenda
• Classes
• Inheritance
• Polymorphism
• Versioning
• Interfaces
• Reference types VS Value types
Object oriented programming (OOP) is a way
of programming where you create chunks of
code that match up with real world objects.
Classes
A Class
• Let’s create a Player
• And a Constructor
class Player
{
private string name;
private int score;
private int livesLeft;
}
class Player
{
private string name;
private int score;
private int livesLeft;
public Player(string name)
{
this.name = name;
}
public Player(string name, int startingLives)
{
this.name = name;
livesLeft = startingLives;
}
}
Classes and Objects
Who is Who?
Objects appear only at Runtime
this Keyword
this Keyword
What is it?
this Keyword
A pointer pointing on the object itself at runtime
this Keyword Usage
• Name hiding and clarity
• Passing Player instance at runtime to other object.
• Cloning the Player object (instance at runtime) into another Player object (in
constructor.)
public Player(string name)
{
this.name = name;
}
public Player(string name, int startingLives)
{
this.name = name;
livesLeft = startingLives;
}
Static Keyword
What is it?
Destructors
Destructor
• Correspond to finalizers in Java.
• Called for an object before it is removed by the garbage collector.
• No public or private.
• Is dangerous (object resurrection) and should be avoided
class Test
{
~Test()
{
... finalization work ...
// automatically calls the destructor of the base class
}
}
Inheritance
Constructors and Inheritance
using System;
public class ParentClass
{
public ParentClass()
{
Console.WriteLine("Parent Constructor.");
}
public void print()
{
Console.WriteLine("I'm a Parent Class.");
}
}
public class ChildClass : ParentClass
{
public ChildClass()
{
Console.WriteLine("Child Constructor.");
}
public static void Main()
{
ChildClass child = new ChildClass();
child.print();
}
}
Inheritance
using System;
public class ParentClass
{
public ParentClass()
{
Console.WriteLine("Parent Constructor.");
}
public void print()
{
Console.WriteLine("I'm a Parent Class.");
}
}
public class ChildClass : ParentClass
{
public ChildClass()
{
Console.WriteLine("Child Constructor.");
}
public static void Main()
{
ChildClass child = new ChildClass();
child.print();
}
}
Inheritance
using System;
public class ParentClass
{
public ParentClass()
{
Console.WriteLine("Parent Constructor.");
}
public void print()
{
Console.WriteLine("I'm a Parent Class.");
}
}
public class ChildClass : ParentClass
{
public ChildClass()
{
Console.WriteLine("Child Constructor.");
}
public static void Main()
{
ChildClass child = new ChildClass();
child.print();
}
}
Inheritance
using System;
public class ParentClass
{
public ParentClass()
{
Console.WriteLine("Parent Constructor.");
}
public void print()
{
Console.WriteLine("I'm a Parent Class.");
}
}
public class ChildClass : ParentClass
{
public ChildClass()
{
Console.WriteLine("Child Constructor.");
}
public static void Main()
{
ChildClass child = new ChildClass();
child.print();
}
}
Parent Constructor.
Child Constructor.
I'm a Parent Class.
Inheritance
public class Parent
{
string parentString;
public Parent()
{
Console.WriteLine("Parent Constructor.");
}
public Parent(string myString)
{
parentString = myString;
Console.WriteLine(parentString);
}
public void print()
{
Console.WriteLine("I'm a Parent Class.");
}
}
public class Child : Parent
{
public Child() : base("From Derived")
{
Console.WriteLine("Child Constructor.");
}
public new void print()
{
base.print();
Console.WriteLine("I'm a Child Class.");
}
public static void Main()
{
Child child = new Child();
child.print();
((Parent)child).print();
}
}
Inheritance
public class Parent
{
string parentString;
public Parent()
{
Console.WriteLine("Parent Constructor.");
}
public Parent(string myString)
{
parentString = myString;
Console.WriteLine(parentString);
}
public void print()
{
Console.WriteLine("I'm a Parent Class.");
}
}
public class Child : Parent
{
public Child() : base("From Derived")
{
Console.WriteLine("Child Constructor.");
}
public new void print()
{
base.print();
Console.WriteLine("I'm a Child Class.");
}
public static void Main()
{
Child child = new Child();
child.print();
((Parent)child).print();
}
}
Inheritance
public class Parent
{
string parentString;
public Parent()
{
Console.WriteLine("Parent Constructor.");
}
public Parent(string myString)
{
parentString = myString;
Console.WriteLine(parentString);
}
public void print()
{
Console.WriteLine("I'm a Parent Class.");
}
}
public class Child : Parent
{
public Child() : base("From Derived")
{
Console.WriteLine("Child Constructor.");
}
public new void print()
{
base.print();
Console.WriteLine("I'm a Child Class.");
}
public static void Main()
{
Child child = new Child();
child.print();
((Parent)child).print();
}
}
From Derived
Child Constructor.
I'm a Parent Class.
I'm a Child Class.
I'm a Parent Class.
Inheritance
Polymorphism
public class DrawingObject
{
public virtual void Draw()
{
Console.WriteLine(
"I'm just a generic drawing object.");
}
}
public class Line : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I'm a Line.");
}
}
public class Circle : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I'm a Circle.");
}
}
public class Square : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I'm a Square.");
}
}
Polymorphism
public class DrawingObject
{
public virtual void Draw()
{
Console.WriteLine(
"I'm just a generic drawing object.");
}
}
public class Line : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I'm a Line.");
}
}
public class Circle : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I'm a Circle.");
}
}
public class Square : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I'm a Square.");
}
}
Polymorphism
public class DrawingObject
{
public virtual void Draw()
{
Console.WriteLine(
"I'm just a generic drawing object.");
}
}
public class Line : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I'm a Line.");
}
}
public class Circle : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I'm a Circle.");
}
}
public class Square : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I'm a Square.");
}
}
Polymorphism
class Program
{
static void Main(string[] args)
{
DrawingObject[] dObj = new DrawingObject[4];
dObj[0] = new Line();
dObj[1] = new Circle();
dObj[2] = new Square();
dObj[3] = new DrawingObject();
foreach (DrawingObject drawObj in dObj)
{
drawObj.Draw();
}
}
}
Polymorphism
class Program
{
static void Main(string[] args)
{
DrawingObject[] dObj = new DrawingObject[4];
dObj[0] = new Line();
dObj[1] = new Circle();
dObj[2] = new Square();
dObj[3] = new DrawingObject();
foreach (DrawingObject drawObj in dObj)
{
drawObj.Draw();
}
}
}
I'm a Line.
I'm a Circle.
I'm a Square.
I'm just a generic drawing object.
Press any key to continue...
Polymorphism
public class DrawingObject
{
public DrawingObject(string objectName)
{
}
public virtual void Draw()
{
Console.WriteLine("I'm just a generic drawing object.");
}
}
public class Line : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I'm a Line.");
}
}
Polymorphism
public class DrawingObject
{
public DrawingObject(string objectName)
{
}
public virtual void Draw()
{
Console.WriteLine("I'm just a generic drawing object.");
}
}
public class Line : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I'm a Line.");
}
}
Polymorphism
public class DrawingObject
{
public DrawingObject(string objectName)
{
}
public virtual void Draw()
{
Console.WriteLine("I'm just a generic drawing object.");
}
}
public class Line : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I'm a Line.");
}
}
Polymorphism
public class DrawingObject
{
public DrawingObject(string objectName)
{
}
public virtual void Draw()
{
Console.WriteLine("I'm just a generic drawing object.");
}
}
public class Line : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I'm a Line.");
}
}
Polymorphism
public class DrawingObject
{
public DrawingObject(string objectName)
{
}
public virtual void Draw()
{
Console.WriteLine("I'm just a generic drawing object.");
}
}
public class Line : DrawingObject
{
public override void Draw()
{
Console.WriteLine("I'm a Line.");
}
}
Polymorphism
Polymorphism
Polymorphism
Polymorphism
public class DrawingObject
{
public DrawingObject(string objectName)
{
Console.WriteLine(objectName);
}
public virtual void Draw()
{
Console.WriteLine("I'm just a generic drawing
object.");
}
}
public class Line : DrawingObject
{
public Line():base("ForBaseClass,
DrawingObject")
{
Console.WriteLine(this.ToString());
}
public override void Draw()
{
Console.WriteLine("I'm a Line.");
}
}
Polymorphism
public class DrawingObject
{
public DrawingObject(string objectName)
{
Console.WriteLine(objectName);
}
public virtual void Draw()
{
Console.WriteLine("I'm just a generic drawing
object.");
}
}
public class Line : DrawingObject
{
public Line():base("ForBaseClass,
DrawingObject")
{
Console.WriteLine(this.ToString());
}
public override void Draw()
{
Console.WriteLine("I'm a Line.");
}
}
Polymorphism
public class DrawingObject
{
public DrawingObject(string objectName)
{
Console.WriteLine(objectName);
}
public virtual void Draw()
{
Console.WriteLine("I'm just a generic drawing
object.");
}
}
public class Line : DrawingObject
{
public Line():base("ForBaseClass,
DrawingObject")
{
Console.WriteLine(this.ToString());
}
public override void Draw()
{
Console.WriteLine("I'm a Line.");
}
}
ForBaseClass, DrawingObject
ConsoleApplicationCourseTest.Line
Press any key to continue...
Polymorphism
public class DrawingObject
{
public DrawingObject(string objectName)
{
Console.WriteLine(objectName);
}
public virtual void Draw()
{
Console.WriteLine("I'm just a generic drawing
object.");
}
}
public class Line : DrawingObject
{
public Line():base("ForBaseClass,
DrawingObject")
{
Console.WriteLine(this.ToString());
}
public override void Draw()
{
Console.WriteLine("I'm a Line.");
}
}
ForBaseClass, DrawingObject
ConsoleApplicationCourseTest.Line
Press any key to continue...
Polymorphism
public class DrawingObject
{
public DrawingObject(string objectName)
{
Console.WriteLine(objectName);
}
public virtual void Draw()
{
Console.WriteLine("I'm just a generic drawing
object.");
}
}
public class Line : DrawingObject
{
public Line():base("ForBaseClass,
DrawingObject")
{
Console.WriteLine(this.ToString());
}
public override void Draw()
{
Console.WriteLine("I'm a Line.");
}
}
ForBaseClass, DrawingObject
ConsoleApplicationCourseTest.Line
Press any key to continue...
Polymorphism
Polymorphism
Polymorphism
public class DrawingObject
{
public DrawingObject(string objectName)
{
Console.WriteLine(objectName);
}
public virtual void Draw()
{
Console.WriteLine("I'm just a generic drawing
object.");
}
}
public class Line : DrawingObject
{
public Line():base("ForBaseClass,
DrawingObject")
{
Console.WriteLine(this.ToString());
}
public override string ToString()
{
return "just another Line object on runtime!";
}
public override void Draw()
{
Console.WriteLine("I'm a Line.");
}
}
Polymorphism
public class DrawingObject
{
public DrawingObject(string objectName)
{
Console.WriteLine(objectName);
}
public virtual void Draw()
{
Console.WriteLine("I'm just a generic drawing
object.");
}
}
public class Line : DrawingObject
{
public Line():base("ForBaseClass,
DrawingObject")
{
Console.WriteLine(this.ToString());
}
public override string ToString()
{
return "just another Line object on runtime!";
}
public override void Draw()
{
Console.WriteLine("I'm a Line.");
}
}
ForBaseClass, DrawingObject
just another Line object on runtime!
Press any key to continue...
Polymorphism
Abstract Classes
Abstract Classes
• Abstract methods do not have an implementation.
• Abstract methods are implicitly virtual.
• If a class has abstract methods it must be declared abstract itself.
• One cannot create objects of an abstract class.
abstract class Stream
{
public abstract void Write(char ch);
public void WriteString(string s) { foreach (char ch in s) Write(s); }
}
class File: Stream
{
public override void Write(char ch) {... write ch to disk...}
}
sealed and internal classes
sealed: can’t be extended (Java’s final)
internal: can’t be used in other namespaces
Versioning
class DerivedClass: BaseClass
{
public override string Meth1()
{
return "MyDerived-Meth1";
}
public new string Meth2()
{
return "MyDerived-Meth2";
}
public string Meth3()
{
return "MyDerived-Meth3";
}
public static void Main()
{
DerivedClassmD = new MyDerived();
BaseClass mB = (BaseClass)mD;
System.Console.WriteLine(mB.Meth1());
System.Console.WriteLine(mB.Meth2());
System.Console.WriteLine(mB.Meth3());
}
}
Versioning
public class BaseClass
{
public virtual string Meth1()
{
return "BaseClass-Meth1";
}
public virtual string Meth2()
{
return "BaseClass-Meth2";
}
public virtual string Meth3()
{
return "BaseClass-Meth3";
}
}
class DerivedClass: BaseClass
{
public override string Meth1()
{
return "MyDerived-Meth1";
}
public new string Meth2()
{
return "MyDerived-Meth2";
}
public string Meth3()
{
return "MyDerived-Meth3";
}
public static void Main()
{
DerivedClassmD = new MyDerived();
BaseClass mB = (BaseClass)mD;
System.Console.WriteLine(mB.Meth1());
System.Console.WriteLine(mB.Meth2());
System.Console.WriteLine(mB.Meth3());
}
}
Versioning
Overrides the virtual method
Meth1 using the override
keyword
public class BaseClass
{
public virtual string Meth1()
{
return "BaseClass-Meth1";
}
public virtual string Meth2()
{
return "BaseClass-Meth2";
}
public virtual string Meth3()
{
return "BaseClass-Meth3";
}
}
class DerivedClass: BaseClass
{
public override string Meth1()
{
return "MyDerived-Meth1";
}
public new string Meth2()
{
return "MyDerived-Meth2";
}
public string Meth3()
{
return "MyDerived-Meth3";
}
public static void Main()
{
DerivedClassmD = new MyDerived();
BaseClass mB = (BaseClass)mD;
System.Console.WriteLine(mB.Meth1());
System.Console.WriteLine(mB.Meth2());
System.Console.WriteLine(mB.Meth3());
}
}
Versioning
Explicitly hide the virtual
method Meth2 using the new
keyword
public class BaseClass
{
public virtual string Meth1()
{
return "BaseClass-Meth1";
}
public virtual string Meth2()
{
return "BaseClass-Meth2";
}
public virtual string Meth3()
{
return "BaseClass-Meth3";
}
}
class DerivedClass: BaseClass
{
public override string Meth1()
{
return "MyDerived-Meth1";
}
public new string Meth2()
{
return "MyDerived-Meth2";
}
public string Meth3()
{
return "MyDerived-Meth3";
}
public static void Main()
{
DerivedClassmD = new MyDerived();
BaseClass mB = (BaseClass)mD;
System.Console.WriteLine(mB.Meth1());
System.Console.WriteLine(mB.Meth2());
System.Console.WriteLine(mB.Meth3());
}
}
Versioning
Because no keyword is specified
in the following declaration a
warning will be issued to alert
the programmer that the method
hides the inherited member
BaseClass.Meth3()
public class BaseClass
{
public virtual string Meth1()
{
return "BaseClass-Meth1";
}
public virtual string Meth2()
{
return "BaseClass-Meth2";
}
public virtual string Meth3()
{
return "BaseClass-Meth3";
}
}
class DerivedClass: BaseClass
{
public override string Meth1()
{
return "MyDerived-Meth1";
}
public new string Meth2()
{
return "MyDerived-Meth2";
}
public string Meth3()
{
return "MyDerived-Meth3";
}
public static void Main()
{
DerivedClassmD = new MyDerived();
BaseClass mB = (BaseClass)mD;
System.Console.WriteLine(mB.Meth1());
System.Console.WriteLine(mB.Meth2());
System.Console.WriteLine(mB.Meth3());
}
}
Versioning
public class BaseClass
{
public virtual string Meth1()
{
return "BaseClass-Meth1";
}
public virtual string Meth2()
{
return "BaseClass-Meth2";
}
public virtual string Meth3()
{
return "BaseClass-Meth3";
}
}
MyDerived-Meth1
BaseClass-Meth2
BaseClass-Meth3
Multiple Inheritance?
C#.NET doesn't allow it, Why?
Multiple Inheritance?
C#.NET doesn't allow it
C++.NET doesn’t allow it
Java doesn’t allow it
C++, as you know, allows it
However, C# allow
multiple interfaces
However, C# allow
multiple interfaces
But what are they?
Interfaces
Interfaces :D
Interfaces – The concept
Interfaces VS Abstract Classes
Interfaces
• An interface contains only the signatures of methods, delegates or events.
• The implementation of the methods is done in the class that implements the
interface.
• Can’t contain Fields!
When to use?
(An Example)
Consider a Human, an Animal and a Car Class, where they all implement a crazy method called
ConsumeWater().
If we have many objects of each type of Human, Animal and Car and we want to call
ConsumeWater() for all objects of Human, Animal and Car; we have to call it like this:
human1.ConsumeWater();
human2.ConsumeWater();
human3.ConsumeWater();
animal1.ConsumeWater();
animal2.ConsumeWater();
car1.ConsumeWater();
car2.ConsumeWater();
SomeObject
Human Animal Car
And they they can’t be subclassed from one particular abstract/base class like this:
SomeObject
Human Animal Car
And they they can’t be subclassed from one particular abstract/base class like this:
Because they are not the same and they share some common properties!
If we have many objects of each type of Human, Animal and Car and we want to call
ConsumeWater() for all objects of Human, Animal and Car; we have to call it like this:
human1.ConsumeWater();
human2.ConsumeWater();
human3.ConsumeWater();
animal1.ConsumeWater();
animal2.ConsumeWater();
car1.ConsumeWater();
car2.ConsumeWater();
But if we can implement a common functionalities from a common place, that would be nice!
interface
Human Animal Car
Implementation
and not
inheritance!
If we have many objects of each type of Human, Animal and Car and we want to call
ConsumeWater() for all objects of Human, Animal and Car; we have to call it like this:
human1.ConsumeWater();
human2.ConsumeWater();
human3.ConsumeWater();
animal1.ConsumeWater();
animal2.ConsumeWater();
car1.ConsumeWater();
car2.ConsumeWater();
But if we can implement a common functionalities from a common place, that would be nice!
IWaterable
Human Animal Car
Implementation
and not
inheritance!
Now we can add all objects to a common list of IWaterable and just call ConsumeWater()
for each IWaterable object (they are all Waterable now!)
List<IWaterable> waterables = new List<IWaterable>() {“human1”, “human2”, “human3”,
“animal1”, “animal2”, “car1”, “car2”};
foreach(IWaterable waterable in waterables)
waterable.ConsumeWater();
Look how nice the code is and how clear the relation is. When we implement an interface we are
just saying that this interface provides a certain functionality for us (and others may freely have this
functionality as well.)
IWaterable
Human Animal Car
Implementation
and not
inheritance!
Interfaces – the Code
Interfaces – the Code
Public interface IWaterable
{
public void ConsumeWater();
}
Interfaces – the Code
Public interface IWaterable
{
public void ConsumeWater();
}
Public class Human: IWaterable
{
public void ConsumeWater()
{
}
}
Public class Animal: IWaterable
{
public void ConsumeWater()
{
}
}
Public class Car: IWaterable
{
public void ConsumeWater()
{
}
}
Interfaces – the Code
Public interface IWaterable
{
public void ConsumeWater();
}
Public class Human: IWaterable
{
public void ConsumeWater()
{
}
}
Public class Animal: IWaterable
{
public void ConsumeWater()
{
}
}
Public class Car: IWaterable
{
public void ConsumeWater()
{
}
}
Interfaces – the Code
Public interface IWaterable
{
public void ConsumeWater();
}
Public class Human: IWaterable
{
public void ConsumeWater()
{
//Drinking Water
}
}
Public class Animal: IWaterable
{
public void ConsumeWater()
{
//Drinking Water
}
}
Public class Car: IWaterable
{
public void ConsumeWater()
{
//Cooling the engine
}
}
Interfaces – the Code
Public interface IWaterable
{
public void ConsumeWater();
}
Public class Human: IWaterable
{
public void ConsumeWater()
{
//Drinking Water
}
}
Public class Animal:IWaterable,
INosiable
{
public void ConsumeWater()
{
//Drinking Water
}
public void MakeNoise()
{
//Mew, Roar or Moo!
}
}
Public class Car: IWaterable,
INosiable
{
public void ConsumeWater()
{
//Cooling the engine
}
public void MakeNoise()
{
//Rev the engine!
}
}
Public interface INoisable
{
public void MakeNoise();
}
Interfaces
• An interface can be a member of a namespace or a class and can contain
signatures of the following members:
– Methods
– Properties
– Indexers
– Events
• “No” Fields!
Reference VS Value Types
using System;
class Program
{
static void Main()
{
float lengthFloat = 7.35f;
// lose precision - explicit conversion
int lengthInt = (int)lengthFloat;
// no problem - implicit conversion
double lengthDouble = lengthInt;
Console.WriteLine("lengthInt = " + lengthInt);
Console.WriteLine("lengthDouble = " + lengthDouble);
Console.ReadKey();
}
}
Reference VS Value Types
using System;
class Program
{
static void Main()
{
float lengthFloat = 7.35f;
// lose precision - explicit conversion
int lengthInt = (int)lengthFloat;
// no problem - implicit conversion
double lengthDouble = lengthInt;
Console.WriteLine("lengthInt = " + lengthInt);
Console.WriteLine("lengthDouble = " + lengthDouble);
Console.ReadKey();
}
}
lengthInt = 7
lengthDouble = 7
Reference VS Value Types
Reference VS Value Types
• Reference type
• variables are named appropriately (reference) because the variable holds a reference to an
object.
• In C and C++, we have something similar that which is “a pointer”, which points to an object.
While you can modify a pointer, you can't modify the value of a reference - it simply points at
the object in memory.
using System;
class Employee
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
}
Reference VS Value Types
Reference Types
class Program
{ static void Main()
{
Employee joe = new Employee();
joe.Name = "Joe";
Employee bob = new Employee();
bob.Name = "Bob";
Console.WriteLine("Original Employee Values:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
// assign joe reference to bob variable
bob = joe;
Console.WriteLine("Values After Reference Assignment:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
joe.Name = "Bobbi Jo";
Console.WriteLine("Values After Changing One Instance:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
Console.ReadKey();
}
}
Reference Types
class Program
{ static void Main()
{
Employee joe = new Employee();
joe.Name = "Joe";
Employee bob = new Employee();
bob.Name = "Bob";
Console.WriteLine("Original Employee Values:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
// assign joe reference to bob variable
bob = joe;
Console.WriteLine("Values After Reference Assignment:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
joe.Name = "Bobbi Jo";
Console.WriteLine("Values After Changing One Instance:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
Console.ReadKey();
}
}
Original Employee Values:
joe = Joe
bob = Bob
Values After Reference Assignment:
joe = Joe
bob = Joe
Values After Changing One Instance:
joe = Bobbi Jo
bob = Bobbi Jo
Reference Types
class Program
{ static void Main()
{
Employee joe = new Employee();
joe.Name = "Joe";
Employee bob = new Employee();
bob.Name = "Bob";
Console.WriteLine("Original Employee Values:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
// assign joe reference to bob variable
bob = joe;
Console.WriteLine("Values After Reference Assignment:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
joe.Name = "Bobbi Jo";
Console.WriteLine("Values After Changing One Instance:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
Console.ReadKey();
}
}
Original Employee Values:
joe = Joe
bob = Bob
Values After Reference Assignment:
joe = Joe
bob = Joe
Values After Changing One Instance:
joe = Bobbi Jo
bob = Bobbi Jo
How is that?!
Reference Types
class Program
{ static void Main()
{
Employee joe = new Employee();
joe.Name = "Joe";
Employee bob = new Employee();
bob.Name = "Bob";
Console.WriteLine("Original Employee Values:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
// assign joe reference to bob variable
bob = joe;
Console.WriteLine("Values After Reference Assignment:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
joe.Name = "Bobbi Jo";
Console.WriteLine("Values After Changing One Instance:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
Console.ReadKey();
}
}
Emp Emp
joe bob
Reference Types
class Program
{ static void Main()
{
Employee joe = new Employee();
joe.Name = "Joe";
Employee bob = new Employee();
bob.Name = "Bob";
Console.WriteLine("Original Employee Values:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
// assign joe reference to bob variable
bob = joe;
Console.WriteLine("Values After Reference Assignment:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
joe.Name = "Bobbi Jo";
Console.WriteLine("Values After Changing One Instance:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
Console.ReadKey();
}
}
Emp Emp
joe bob
Reference Types
class Program
{ static void Main()
{
Employee joe = new Employee();
joe.Name = "Joe";
Employee bob = new Employee();
bob.Name = "Bob";
Console.WriteLine("Original Employee Values:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
// assign joe reference to bob variable
bob = joe;
Console.WriteLine("Values After Reference Assignment:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
joe.Name = "Bobbi Jo";
Console.WriteLine("Values After Changing One Instance:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
Console.ReadKey();
}
}
Emp Emp
joe bob
Reference Types
class Program
{ static void Main()
{
Employee joe = new Employee();
joe.Name = "Joe";
Employee bob = new Employee();
bob.Name = "Bob";
Console.WriteLine("Original Employee Values:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
// assign joe reference to bob variable
bob = joe;
Console.WriteLine("Values After Reference Assignment:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
joe.Name = "Bobbi Jo";
Console.WriteLine("Values After Changing One Instance:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
Console.ReadKey();
}
}
Emp Emp
joe bob
Reference Types
class Program
{ static void Main()
{
Employee joe = new Employee();
joe.Name = "Joe";
Employee bob = new Employee();
bob.Name = "Bob";
Console.WriteLine("Original Employee Values:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
// assign joe reference to bob variable
bob = joe;
Console.WriteLine("Values After Reference Assignment:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
joe.Name = "Bobbi Jo";
Console.WriteLine("Values After Changing One Instance:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
Console.ReadKey();
}
}
Emp Emp
joe bob
Reference Types
class Program
{ static void Main()
{
Employee joe = new Employee();
joe.Name = "Joe";
Employee bob = new Employee();
bob.Name = "Bob";
Console.WriteLine("Original Employee Values:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
// assign joe reference to bob variable
bob = joe;
Console.WriteLine("Values After Reference Assignment:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
joe.Name = "Bobbi Jo";
Console.WriteLine("Values After Changing One Instance:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
Console.ReadKey();
}
}
Emp
joe
Emp
bob
Reference Types
class Program
{ static void Main()
{
Employee joe = new Employee();
joe.Name = "Joe";
Employee bob = new Employee();
bob.Name = "Bob";
Console.WriteLine("Original Employee Values:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
// assign joe reference to bob variable
bob = joe;
Console.WriteLine("Values After Reference Assignment:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
joe.Name = "Bobbi Jo";
Console.WriteLine("Values After Changing One Instance:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
Console.ReadKey();
}
}
Emp
joe
Emp
bob
Reference Types
class Program
{ static void Main()
{
Employee joe = new Employee();
joe.Name = "Joe";
Employee bob = new Employee();
bob.Name = "Bob";
Console.WriteLine("Original Employee Values:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
// assign joe reference to bob variable
bob = joe;
Console.WriteLine("Values After Reference Assignment:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
joe.Name = "Bobbi Jo";
Console.WriteLine("Values After Changing One Instance:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
Console.ReadKey();
}
}
Emp
joe
Emp
bob
Reference Types
class Program
{ static void Main()
{
Employee joe = new Employee();
joe.Name = "Joe";
Employee bob = new Employee();
bob.Name = "Bob";
Console.WriteLine("Original Employee Values:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
// assign joe reference to bob variable
bob = joe;
Console.WriteLine("Values After Reference Assignment:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
joe.Name = "Bobbi Jo";
Console.WriteLine("Values After Changing One Instance:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
Console.ReadKey();
}
}
Emp
joe bob
Reference Types
class Program
{ static void Main()
{
Employee joe = new Employee();
joe.Name = "Joe";
Employee bob = new Employee();
bob.Name = "Bob";
Console.WriteLine("Original Employee Values:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
// assign joe reference to bob variable
bob = joe;
Console.WriteLine("Values After Reference Assignment:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
joe.Name = "Bobbi Jo";
Console.WriteLine("Values After Changing One Instance:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
Console.ReadKey();
}
}
joe bob
Emp
Name =
“Joe”
Reference Types
class Program
{ static void Main()
{
Employee joe = new Employee();
joe.Name = "Joe";
Employee bob = new Employee();
bob.Name = "Bob";
Console.WriteLine("Original Employee Values:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
// assign joe reference to bob variable
bob = joe;
Console.WriteLine("Values After Reference Assignment:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
joe.Name = "Bobbi Jo";
Console.WriteLine("Values After Changing One Instance:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
Console.ReadKey();
}
}
joe bob
Emp
Name =
“Bobbi Jo”
Reference Types
class Program
{ static void Main()
{
Employee joe = new Employee();
joe.Name = "Joe";
Employee bob = new Employee();
bob.Name = "Bob";
Console.WriteLine("Original Employee Values:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
// assign joe reference to bob variable
bob = joe;
Console.WriteLine("Values After Reference Assignment:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
joe.Name = "Bobbi Jo";
Console.WriteLine("Values After Changing One Instance:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
Console.ReadKey();
}
}
joe bob
Emp
Name =
“Bobbi Jo”
Reference Types
class Program
{ static void Main()
{
Employee joe = new Employee();
joe.Name = "Joe";
Employee bob = new Employee();
bob.Name = "Bob";
Console.WriteLine("Original Employee Values:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
// assign joe reference to bob variable
bob = joe;
Console.WriteLine("Values After Reference Assignment:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
joe.Name = "Bobbi Jo";
Console.WriteLine("Values After Changing One Instance:");
Console.WriteLine("joe = " + joe.Name);
Console.WriteLine("bob = " + bob.Name);
Console.ReadKey();
}
}
joe bob
Emp
Name =
“Bobbi Jo”
Original Employee Values:
joe = Joe
bob = Bob
Values After Reference Assignment:
joe = Joe
bob = Joe
Values After Changing One Instance:
joe = Bobbi Jo
bob = Bobbi Jo
Reference Types
• The following types are reference types:
• arrays
• class
• delegates
• interfaces
Value Types
Value Types
• A value type
– variable holds its own copy of an object and when you perform assignment from one value
type variable to another, both the left-hand-side and right-hand-side of the assignment hold
two separate copies of that value.
Value Types
• A value type
– variable holds its own copy of an object and when you perform assignment from one value
type variable to another, both the left-hand-side and right-hand-side of the assignment hold
two separate copies of that value.
• An important fact you need to understand is that when you are assigning one
reference type variable to another, only the reference is copied, not the
object. The variable holds the reference and that is what is being copied.
struct Height
{
private int m_inches;
public int Inches
{
get { return m_inches; }
set { m_inches = value; }
}
}
Value Types
class Program
{
static void Main()
{
Height joe = new Height();
joe.Inches = 71;
Height bob = new Height();
bob.Inches = 59;
Console.WriteLine("Original Height Values:");
Console.WriteLine("joe = " + joe.Inches);
Console.WriteLine("bob = " + bob.Inches);
bob = joe;
Console.WriteLine("Values After Value Assignment:");
Console.WriteLine("joe = " + joe.Inches);
Console.WriteLine("bob = " + bob.Inches);
joe.Inches = 65;
Console.WriteLine("Values After Changing One Instance:");
Console.WriteLine("joe = " + joe.Inches);
Console.WriteLine("bob = " + bob.Inches);
Console.ReadKey();
}
}
Value Types
class Program
{
static void Main()
{
Height joe = new Height();
joe.Inches = 71;
Height bob = new Height();
bob.Inches = 59;
Console.WriteLine("Original Height Values:");
Console.WriteLine("joe = " + joe.Inches);
Console.WriteLine("bob = " + bob.Inches);
bob = joe;
Console.WriteLine("Values After Value Assignment:");
Console.WriteLine("joe = " + joe.Inches);
Console.WriteLine("bob = " + bob.Inches);
joe.Inches = 65;
Console.WriteLine("Values After Changing One Instance:");
Console.WriteLine("joe = " + joe.Inches);
Console.WriteLine("bob = " + bob.Inches);
Console.ReadKey();
}
}
Value Types
Height Height
joe bob
class Program
{
static void Main()
{
Height joe = new Height();
joe.Inches = 71;
Height bob = new Height();
bob.Inches = 59;
Console.WriteLine("Original Height Values:");
Console.WriteLine("joe = " + joe.Inches);
Console.WriteLine("bob = " + bob.Inches);
bob = joe;
Console.WriteLine("Values After Value Assignment:");
Console.WriteLine("joe = " + joe.Inches);
Console.WriteLine("bob = " + bob.Inches);
joe.Inches = 65;
Console.WriteLine("Values After Changing One Instance:");
Console.WriteLine("joe = " + joe.Inches);
Console.WriteLine("bob = " + bob.Inches);
Console.ReadKey();
}
}
Value Types
Height Height
joe bob
class Program
{
static void Main()
{
Height joe = new Height();
joe.Inches = 71;
Height bob = new Height();
bob.Inches = 59;
Console.WriteLine("Original Height Values:");
Console.WriteLine("joe = " + joe.Inches);
Console.WriteLine("bob = " + bob.Inches);
bob = joe;
Console.WriteLine("Values After Value Assignment:");
Console.WriteLine("joe = " + joe.Inches);
Console.WriteLine("bob = " + bob.Inches);
joe.Inches = 65;
Console.WriteLine("Values After Changing One Instance:");
Console.WriteLine("joe = " + joe.Inches);
Console.WriteLine("bob = " + bob.Inches);
Console.ReadKey();
}
}
Value Types
Height Height
joe bob
Height
class Program
{
static void Main()
{
Height joe = new Height();
joe.Inches = 71;
Height bob = new Height();
bob.Inches = 59;
Console.WriteLine("Original Height Values:");
Console.WriteLine("joe = " + joe.Inches);
Console.WriteLine("bob = " + bob.Inches);
bob = joe;
Console.WriteLine("Values After Value Assignment:");
Console.WriteLine("joe = " + joe.Inches);
Console.WriteLine("bob = " + bob.Inches);
joe.Inches = 65;
Console.WriteLine("Values After Changing One Instance:");
Console.WriteLine("joe = " + joe.Inches);
Console.WriteLine("bob = " + bob.Inches);
Console.ReadKey();
}
}
Value Types
Height Height
joe bob
Exactly
the
same
class Program
{
static void Main()
{
Height joe = new Height();
joe.Inches = 71;
Height bob = new Height();
bob.Inches = 59;
Console.WriteLine("Original Height Values:");
Console.WriteLine("joe = " + joe.Inches);
Console.WriteLine("bob = " + bob.Inches);
bob = joe;
Console.WriteLine("Values After Value Assignment:");
Console.WriteLine("joe = " + joe.Inches);
Console.WriteLine("bob = " + bob.Inches);
joe.Inches = 65;
Console.WriteLine("Values After Changing One Instance:");
Console.WriteLine("joe = " + joe.Inches);
Console.WriteLine("bob = " + bob.Inches);
Console.ReadKey();
}
}
Value Types
71 71
joe bob
Exactly
the
same
class Program
{
static void Main()
{
Height joe = new Height();
joe.Inches = 71;
Height bob = new Height();
bob.Inches = 59;
Console.WriteLine("Original Height Values:");
Console.WriteLine("joe = " + joe.Inches);
Console.WriteLine("bob = " + bob.Inches);
bob = joe;
Console.WriteLine("Values After Value Assignment:");
Console.WriteLine("joe = " + joe.Inches);
Console.WriteLine("bob = " + bob.Inches);
joe.Inches = 65;
Console.WriteLine("Values After Changing One Instance:");
Console.WriteLine("joe = " + joe.Inches);
Console.WriteLine("bob = " + bob.Inches);
Console.ReadKey();
}
}
Value Types
71 71
joe bob
class Program
{
static void Main()
{
Height joe = new Height();
joe.Inches = 71;
Height bob = new Height();
bob.Inches = 59;
Console.WriteLine("Original Height Values:");
Console.WriteLine("joe = " + joe.Inches);
Console.WriteLine("bob = " + bob.Inches);
bob = joe;
Console.WriteLine("Values After Value Assignment:");
Console.WriteLine("joe = " + joe.Inches);
Console.WriteLine("bob = " + bob.Inches);
joe.Inches = 65;
Console.WriteLine("Values After Changing One Instance:");
Console.WriteLine("joe = " + joe.Inches);
Console.WriteLine("bob = " + bob.Inches);
Console.ReadKey();
}
}
Value Types
65 71
joe bob
class Program
{
static void Main()
{
Height joe = new Height();
joe.Inches = 71;
Height bob = new Height();
bob.Inches = 59;
Console.WriteLine("Original Height Values:");
Console.WriteLine("joe = " + joe.Inches);
Console.WriteLine("bob = " + bob.Inches);
bob = joe;
Console.WriteLine("Values After Value Assignment:");
Console.WriteLine("joe = " + joe.Inches);
Console.WriteLine("bob = " + bob.Inches);
joe.Inches = 65;
Console.WriteLine("Values After Changing One Instance:");
Console.WriteLine("joe = " + joe.Inches);
Console.WriteLine("bob = " + bob.Inches);
Console.ReadKey();
}
}
Value Types
65 71
joe bob
Original Height Values:
joe = 71
bob = 59
Values After Value Assignment:
joe = 71
bob = 71
Values After Changing One Instance:
joe = 65
bob = 71
Value Types
• The following types are value types:
– enum
– struct
Classes and Structs
Classes
• Reference Types
• (objects stored on the heap)
• support inheritance
• (all classes are derived from object)
• can implement interfaces
• may have a destructor
Structs
• Value Types
• (objects stored on the stack)
• no inheritance
• (but compatible with object)
• can implement interfaces
• no destructors allowed
Creating a Class Library Project for Your
Project’s Logic
Now write all your code in the Class Library project and reference it in your presentation layer project
Adding References to Other Projects to
Your Project
Adding References to Your Project
The Principles
• Single Responsibility Principle: design your classes so that each has a single purpose
• Open / Closed Principle: Open for extension but closed for modification
• Liskov Substitution Principle (LSP): functions that use pointers or references to base classes must be
able to use objects of derived classes without knowing it
• Interface Segregation Principle (ISP): clients should not be forced to depend upon interfaces that they
do not use.
• Dependency Inversion Principle (DIP): high level modules should not depend upon low level modules.
Both should depend upon abstractions.
abstractions should not depend upon details. Details should depend upon abstractions.
is the process of validating the correctness of a small section of code. The target code may be a method within
a class, a group of members or even entire components that are isolated from all or most of their dependencies.
Question #1
public class BaseClass
{
public virtual string Meth1()
{
return "BaseClass-Meth1";
}
public string Meth2()
{
return "BaseClass-Meth2";
}
public virtual string Meth3()
{
return "BaseClass-Meth3";
}
}
class DerivedClass: BaseClass
{
public override string Meth1()
{
return "MyDerived-Meth1";
}
public new string Meth2()
{
return "MyDerived-Meth2";
}
public string Meth3()
{
return "MyDerived-Meth3";
}
public static void Main()
{
DerivedClassmD = new MyDerived();
BaseClass mB = mD;
System.Console.WriteLine(mB.Meth1());
System.Console.WriteLine(mB.Meth2());
System.Console.WriteLine(mB.Meth3());
}
}
Question #1
public class BaseClass
{
public virtual string Meth1()
{
return "BaseClass-Meth1";
}
public string Meth2()
{
return "BaseClass-Meth2";
}
public virtual string Meth3()
{
return "BaseClass-Meth3";
}
}
class DerivedClass: BaseClass
{
public override string Meth1()
{
return "MyDerived-Meth1";
}
public new string Meth2()
{
return "MyDerived-Meth2";
}
public string Meth3()
{
return "MyDerived-Meth3";
}
public static void Main()
{
DerivedClassmD = new MyDerived();
BaseClass mB = mD;
System.Console.WriteLine(mB.Meth1());
System.Console.WriteLine(mB.Meth2());
System.Console.WriteLine(mB.Meth3());
}
}
MyDerived-Meth1
BaseClass-Meth2
BaseClass-Meth3
Press any key to continue...
Question #2
class Class1 { }
class Class2 : Class1{ }
class Class3 { }
public class TestingClass
{
public static void Test(object o)
{
Class1 a;
Class2 b;
Class3 c;
if (o is Class1)
{
Console.WriteLine("obj is Class1");
a = (Class1)o;
}
else if (o is Class2)
{
Console.WriteLine("obj is Class2");
b = (Class2)o;
}
else if (o is Class3)
{
Console.WriteLine("obj is Class3");
c = (Class3)o;
}
else if((Class3)o!= null)
{}
}
public static void Main()
{
try
{
Class1 c1 = new Class1();
Class2 c2 = new Class2();
Class3 c3 = new Class3();
Test(c1);
Test(c2);
Test(c3);
Test("a string");
}
catch(Exception e)
{
Console.WriteLine("Sth wrong happened!");
}
}
}
Question #2
class Class1 { }
class Class2 : Class1{ }
class Class3 { }
public class TestingClass
{
public static void Test(object o)
{
Class1 a;
Class2 b;
Class3 c;
if (o is Class1)
{
Console.WriteLine("obj is Class1");
a = (Class1)o;
}
else if (o is Class2)
{
Console.WriteLine("obj is Class2");
b = (Class2)o;
}
else if (o is Class3)
{
Console.WriteLine("obj is Class3");
c = (Class3)o;
}
else if((Class3)o!= null)
{}
}
public static void Main()
{
try
{
Class1 c1 = new Class1();
Class2 c2 = new Class2();
Class3 c3 = new Class3();
Test(c1);
Test(c2);
Test(c3);
Test("a string");
}
catch(Exception e)
{
Console.WriteLine("Sth wrong happened!");
}
}
}
obj is Class1
obj is Class1
obj is Class3
Sth wrong happened!
Press any key to continue...
public interface IsBaseTest
{
void Point1(object obj);
}
public class IsTest
{
public static void Point1(object obj)
{
Console.WriteLine(obj.ToString());
Point2("That's the point");
}
public static void Point2(string str)
{
Console.WriteLine(str.ToString());
Point3("That's the point");
}
public static void Point3(object obj)
{
if (obj.ToString() == " Passed String")
{
Console.WriteLine("In Point3");
}
}
public static void Main()
{
Point1("Passed String");
}
}
Question #3
public interface IsBaseTest
{
void Point1(object obj);
}
public class IsTest
{
public static void Point1(object obj)
{
Console.WriteLine(obj.ToString());
Point2("That's the point");
}
public static void Point2(string str)
{
Console.WriteLine(str.ToString());
Point3("That's the point");
}
public static void Point3(object obj)
{
if (obj.ToString() == " Passed String")
{
Console.WriteLine("In Point3");
}
}
public static void Main()
{
Point1("Passed String");
}
}
Question #3
Passed String
That's the point
In Point3
Press any key to continue...
That’s it for today!
Hope you enjoy it!

Weitere ähnliche Inhalte

Was ist angesagt?

AST Transformations at JFokus
AST Transformations at JFokusAST Transformations at JFokus
AST Transformations at JFokus
HamletDRC
 
C# 6.0 - April 2014 preview
C# 6.0 - April 2014 previewC# 6.0 - April 2014 preview
C# 6.0 - April 2014 preview
Paulo Morgado
 
AST Transformations
AST TransformationsAST Transformations
AST Transformations
HamletDRC
 
Club of anonimous developers "Refactoring: Legacy code"
Club of anonimous developers "Refactoring: Legacy code"Club of anonimous developers "Refactoring: Legacy code"
Club of anonimous developers "Refactoring: Legacy code"
Victor_Cr
 

Was ist angesagt? (19)

AST Transformations at JFokus
AST Transformations at JFokusAST Transformations at JFokus
AST Transformations at JFokus
 
ConFess Vienna 2015 - Metaprogramming with Groovy
ConFess Vienna 2015 - Metaprogramming with GroovyConFess Vienna 2015 - Metaprogramming with Groovy
ConFess Vienna 2015 - Metaprogramming with Groovy
 
Groovy 2.0 webinar
Groovy 2.0 webinarGroovy 2.0 webinar
Groovy 2.0 webinar
 
The Ring programming language version 1.5.2 book - Part 6 of 181
The Ring programming language version 1.5.2 book - Part 6 of 181The Ring programming language version 1.5.2 book - Part 6 of 181
The Ring programming language version 1.5.2 book - Part 6 of 181
 
The Ring programming language version 1.3 book - Part 5 of 88
The Ring programming language version 1.3 book - Part 5 of 88The Ring programming language version 1.3 book - Part 5 of 88
The Ring programming language version 1.3 book - Part 5 of 88
 
Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.Final JAVA Practical of BCA SEM-5.
Final JAVA Practical of BCA SEM-5.
 
Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007
 
Groovy 2 and beyond
Groovy 2 and beyondGroovy 2 and beyond
Groovy 2 and beyond
 
Ast transformations
Ast transformationsAst transformations
Ast transformations
 
Kotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsKotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projects
 
C++ Advanced Features
C++ Advanced FeaturesC++ Advanced Features
C++ Advanced Features
 
C# 6.0 - April 2014 preview
C# 6.0 - April 2014 previewC# 6.0 - April 2014 preview
C# 6.0 - April 2014 preview
 
AST Transformations
AST TransformationsAST Transformations
AST Transformations
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
 
Club of anonimous developers "Refactoring: Legacy code"
Club of anonimous developers "Refactoring: Legacy code"Club of anonimous developers "Refactoring: Legacy code"
Club of anonimous developers "Refactoring: Legacy code"
 
From android/java to swift (3)
From android/java to swift (3)From android/java to swift (3)
From android/java to swift (3)
 
Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)
 
Lombokの紹介
Lombokの紹介Lombokの紹介
Lombokの紹介
 
javascript objects
javascript objectsjavascript objects
javascript objects
 

Ähnlich wie C# Starter L02-Classes and Objects

classes object fgfhdfgfdgfgfgfgfdoop.pptx
classes object  fgfhdfgfdgfgfgfgfdoop.pptxclasses object  fgfhdfgfdgfgfgfgfdoop.pptx
classes object fgfhdfgfdgfgfgfgfdoop.pptx
arjun431527
 
Intro to object oriented programming
Intro to object oriented programmingIntro to object oriented programming
Intro to object oriented programming
David Giard
 
Implementation of interface9 cm604.30
Implementation of interface9 cm604.30Implementation of interface9 cm604.30
Implementation of interface9 cm604.30
myrajendra
 
CSharp presentation and software developement
CSharp presentation and software developementCSharp presentation and software developement
CSharp presentation and software developement
frwebhelp
 

Ähnlich wie C# Starter L02-Classes and Objects (20)

Object-oriented Basics
Object-oriented BasicsObject-oriented Basics
Object-oriented Basics
 
C# 7.0 Hacks and Features
C# 7.0 Hacks and FeaturesC# 7.0 Hacks and Features
C# 7.0 Hacks and Features
 
OOP Core Concept
OOP Core ConceptOOP Core Concept
OOP Core Concept
 
JAVA OOP
JAVA OOPJAVA OOP
JAVA OOP
 
c#(loops,arrays)
c#(loops,arrays)c#(loops,arrays)
c#(loops,arrays)
 
Oops concept
Oops conceptOops concept
Oops concept
 
constructors.pptx
constructors.pptxconstructors.pptx
constructors.pptx
 
classes object fgfhdfgfdgfgfgfgfdoop.pptx
classes object  fgfhdfgfdgfgfgfgfdoop.pptxclasses object  fgfhdfgfdgfgfgfgfdoop.pptx
classes object fgfhdfgfdgfgfgfgfdoop.pptx
 
OOP V3.1
OOP V3.1OOP V3.1
OOP V3.1
 
Java OO Revisited
Java OO RevisitedJava OO Revisited
Java OO Revisited
 
Intro to object oriented programming
Intro to object oriented programmingIntro to object oriented programming
Intro to object oriented programming
 
11slide.ppt
11slide.ppt11slide.ppt
11slide.ppt
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 
Clean Code at Silicon Valley Code Camp 2011 (02/17/2012)
Clean Code at Silicon Valley Code Camp 2011 (02/17/2012)Clean Code at Silicon Valley Code Camp 2011 (02/17/2012)
Clean Code at Silicon Valley Code Camp 2011 (02/17/2012)
 
C# - What's Next?
C# - What's Next?C# - What's Next?
C# - What's Next?
 
Clean Code for East Bay .NET User Group
Clean Code for East Bay .NET User GroupClean Code for East Bay .NET User Group
Clean Code for East Bay .NET User Group
 
Implementation of interface9 cm604.30
Implementation of interface9 cm604.30Implementation of interface9 cm604.30
Implementation of interface9 cm604.30
 
Lies Told By The Kotlin Compiler
Lies Told By The Kotlin CompilerLies Told By The Kotlin Compiler
Lies Told By The Kotlin Compiler
 
Oop lecture5
Oop lecture5Oop lecture5
Oop lecture5
 
CSharp presentation and software developement
CSharp presentation and software developementCSharp presentation and software developement
CSharp presentation and software developement
 

Mehr von Mohammad Shaker

Mehr von Mohammad Shaker (20)

12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate
 
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
 
Interaction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with PsychologyInteraction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with Psychology
 
Short, Matters, Love - Passioneers Event 2015
Short, Matters, Love -  Passioneers Event 2015Short, Matters, Love -  Passioneers Event 2015
Short, Matters, Love - Passioneers Event 2015
 
Unity L01 - Game Development
Unity L01 - Game DevelopmentUnity L01 - Game Development
Unity L01 - Game Development
 
Android L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and WearablesAndroid L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and Wearables
 
Interaction Design L03 - Color
Interaction Design L03 - ColorInteraction Design L03 - Color
Interaction Design L03 - Color
 
Interaction Design L05 - Typography
Interaction Design L05 - TypographyInteraction Design L05 - Typography
Interaction Design L05 - Typography
 
Interaction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and CouplingInteraction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and Coupling
 
Android L05 - Storage
Android L05 - StorageAndroid L05 - Storage
Android L05 - Storage
 
Android L04 - Notifications and Threading
Android L04 - Notifications and ThreadingAndroid L04 - Notifications and Threading
Android L04 - Notifications and Threading
 
Android L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOSAndroid L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOS
 
Interaction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsInteraction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile Constraints
 
Interaction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and GridsInteraction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and Grids
 
Android L10 - Stores and Gaming
Android L10 - Stores and GamingAndroid L10 - Stores and Gaming
Android L10 - Stores and Gaming
 
Android L06 - Cloud / Parse
Android L06 - Cloud / ParseAndroid L06 - Cloud / Parse
Android L06 - Cloud / Parse
 
Android L08 - Google Maps and Utilities
Android L08 - Google Maps and UtilitiesAndroid L08 - Google Maps and Utilities
Android L08 - Google Maps and Utilities
 
Android L03 - Styles and Themes
Android L03 - Styles and Themes Android L03 - Styles and Themes
Android L03 - Styles and Themes
 
Android L02 - Activities and Adapters
Android L02 - Activities and AdaptersAndroid L02 - Activities and Adapters
Android L02 - Activities and Adapters
 
Android L01 - Warm Up
Android L01 - Warm UpAndroid L01 - Warm Up
Android L01 - Warm Up
 

Kürzlich hochgeladen

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Kürzlich hochgeladen (20)

Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 

C# Starter L02-Classes and Objects

  • 1. Mohammad Shaker mohammadshaker.com C# Programming Course @ZGTRShaker 2011, 2012, 2013, 2014 C# Starter L02 – Classes, Polymorphism, Versioning, Interfaces, Reference and Value Types
  • 2. Today’s Agenda • Classes • Inheritance • Polymorphism • Versioning • Interfaces • Reference types VS Value types
  • 3. Object oriented programming (OOP) is a way of programming where you create chunks of code that match up with real world objects.
  • 5. A Class • Let’s create a Player • And a Constructor class Player { private string name; private int score; private int livesLeft; } class Player { private string name; private int score; private int livesLeft; public Player(string name) { this.name = name; } public Player(string name, int startingLives) { this.name = name; livesLeft = startingLives; } }
  • 7. Objects appear only at Runtime
  • 10. this Keyword A pointer pointing on the object itself at runtime
  • 11. this Keyword Usage • Name hiding and clarity • Passing Player instance at runtime to other object. • Cloning the Player object (instance at runtime) into another Player object (in constructor.) public Player(string name) { this.name = name; } public Player(string name, int startingLives) { this.name = name; livesLeft = startingLives; }
  • 14. Destructor • Correspond to finalizers in Java. • Called for an object before it is removed by the garbage collector. • No public or private. • Is dangerous (object resurrection) and should be avoided class Test { ~Test() { ... finalization work ... // automatically calls the destructor of the base class } }
  • 17. using System; public class ParentClass { public ParentClass() { Console.WriteLine("Parent Constructor."); } public void print() { Console.WriteLine("I'm a Parent Class."); } } public class ChildClass : ParentClass { public ChildClass() { Console.WriteLine("Child Constructor."); } public static void Main() { ChildClass child = new ChildClass(); child.print(); } } Inheritance
  • 18. using System; public class ParentClass { public ParentClass() { Console.WriteLine("Parent Constructor."); } public void print() { Console.WriteLine("I'm a Parent Class."); } } public class ChildClass : ParentClass { public ChildClass() { Console.WriteLine("Child Constructor."); } public static void Main() { ChildClass child = new ChildClass(); child.print(); } } Inheritance
  • 19. using System; public class ParentClass { public ParentClass() { Console.WriteLine("Parent Constructor."); } public void print() { Console.WriteLine("I'm a Parent Class."); } } public class ChildClass : ParentClass { public ChildClass() { Console.WriteLine("Child Constructor."); } public static void Main() { ChildClass child = new ChildClass(); child.print(); } } Inheritance
  • 20. using System; public class ParentClass { public ParentClass() { Console.WriteLine("Parent Constructor."); } public void print() { Console.WriteLine("I'm a Parent Class."); } } public class ChildClass : ParentClass { public ChildClass() { Console.WriteLine("Child Constructor."); } public static void Main() { ChildClass child = new ChildClass(); child.print(); } } Parent Constructor. Child Constructor. I'm a Parent Class. Inheritance
  • 21. public class Parent { string parentString; public Parent() { Console.WriteLine("Parent Constructor."); } public Parent(string myString) { parentString = myString; Console.WriteLine(parentString); } public void print() { Console.WriteLine("I'm a Parent Class."); } } public class Child : Parent { public Child() : base("From Derived") { Console.WriteLine("Child Constructor."); } public new void print() { base.print(); Console.WriteLine("I'm a Child Class."); } public static void Main() { Child child = new Child(); child.print(); ((Parent)child).print(); } } Inheritance
  • 22. public class Parent { string parentString; public Parent() { Console.WriteLine("Parent Constructor."); } public Parent(string myString) { parentString = myString; Console.WriteLine(parentString); } public void print() { Console.WriteLine("I'm a Parent Class."); } } public class Child : Parent { public Child() : base("From Derived") { Console.WriteLine("Child Constructor."); } public new void print() { base.print(); Console.WriteLine("I'm a Child Class."); } public static void Main() { Child child = new Child(); child.print(); ((Parent)child).print(); } } Inheritance
  • 23. public class Parent { string parentString; public Parent() { Console.WriteLine("Parent Constructor."); } public Parent(string myString) { parentString = myString; Console.WriteLine(parentString); } public void print() { Console.WriteLine("I'm a Parent Class."); } } public class Child : Parent { public Child() : base("From Derived") { Console.WriteLine("Child Constructor."); } public new void print() { base.print(); Console.WriteLine("I'm a Child Class."); } public static void Main() { Child child = new Child(); child.print(); ((Parent)child).print(); } } From Derived Child Constructor. I'm a Parent Class. I'm a Child Class. I'm a Parent Class. Inheritance
  • 25. public class DrawingObject { public virtual void Draw() { Console.WriteLine( "I'm just a generic drawing object."); } } public class Line : DrawingObject { public override void Draw() { Console.WriteLine("I'm a Line."); } } public class Circle : DrawingObject { public override void Draw() { Console.WriteLine("I'm a Circle."); } } public class Square : DrawingObject { public override void Draw() { Console.WriteLine("I'm a Square."); } } Polymorphism
  • 26. public class DrawingObject { public virtual void Draw() { Console.WriteLine( "I'm just a generic drawing object."); } } public class Line : DrawingObject { public override void Draw() { Console.WriteLine("I'm a Line."); } } public class Circle : DrawingObject { public override void Draw() { Console.WriteLine("I'm a Circle."); } } public class Square : DrawingObject { public override void Draw() { Console.WriteLine("I'm a Square."); } } Polymorphism
  • 27. public class DrawingObject { public virtual void Draw() { Console.WriteLine( "I'm just a generic drawing object."); } } public class Line : DrawingObject { public override void Draw() { Console.WriteLine("I'm a Line."); } } public class Circle : DrawingObject { public override void Draw() { Console.WriteLine("I'm a Circle."); } } public class Square : DrawingObject { public override void Draw() { Console.WriteLine("I'm a Square."); } } Polymorphism
  • 28. class Program { static void Main(string[] args) { DrawingObject[] dObj = new DrawingObject[4]; dObj[0] = new Line(); dObj[1] = new Circle(); dObj[2] = new Square(); dObj[3] = new DrawingObject(); foreach (DrawingObject drawObj in dObj) { drawObj.Draw(); } } } Polymorphism
  • 29. class Program { static void Main(string[] args) { DrawingObject[] dObj = new DrawingObject[4]; dObj[0] = new Line(); dObj[1] = new Circle(); dObj[2] = new Square(); dObj[3] = new DrawingObject(); foreach (DrawingObject drawObj in dObj) { drawObj.Draw(); } } } I'm a Line. I'm a Circle. I'm a Square. I'm just a generic drawing object. Press any key to continue... Polymorphism
  • 30. public class DrawingObject { public DrawingObject(string objectName) { } public virtual void Draw() { Console.WriteLine("I'm just a generic drawing object."); } } public class Line : DrawingObject { public override void Draw() { Console.WriteLine("I'm a Line."); } } Polymorphism
  • 31. public class DrawingObject { public DrawingObject(string objectName) { } public virtual void Draw() { Console.WriteLine("I'm just a generic drawing object."); } } public class Line : DrawingObject { public override void Draw() { Console.WriteLine("I'm a Line."); } } Polymorphism
  • 32. public class DrawingObject { public DrawingObject(string objectName) { } public virtual void Draw() { Console.WriteLine("I'm just a generic drawing object."); } } public class Line : DrawingObject { public override void Draw() { Console.WriteLine("I'm a Line."); } } Polymorphism
  • 33. public class DrawingObject { public DrawingObject(string objectName) { } public virtual void Draw() { Console.WriteLine("I'm just a generic drawing object."); } } public class Line : DrawingObject { public override void Draw() { Console.WriteLine("I'm a Line."); } } Polymorphism
  • 34. public class DrawingObject { public DrawingObject(string objectName) { } public virtual void Draw() { Console.WriteLine("I'm just a generic drawing object."); } } public class Line : DrawingObject { public override void Draw() { Console.WriteLine("I'm a Line."); } } Polymorphism
  • 38. public class DrawingObject { public DrawingObject(string objectName) { Console.WriteLine(objectName); } public virtual void Draw() { Console.WriteLine("I'm just a generic drawing object."); } } public class Line : DrawingObject { public Line():base("ForBaseClass, DrawingObject") { Console.WriteLine(this.ToString()); } public override void Draw() { Console.WriteLine("I'm a Line."); } } Polymorphism
  • 39. public class DrawingObject { public DrawingObject(string objectName) { Console.WriteLine(objectName); } public virtual void Draw() { Console.WriteLine("I'm just a generic drawing object."); } } public class Line : DrawingObject { public Line():base("ForBaseClass, DrawingObject") { Console.WriteLine(this.ToString()); } public override void Draw() { Console.WriteLine("I'm a Line."); } } Polymorphism
  • 40. public class DrawingObject { public DrawingObject(string objectName) { Console.WriteLine(objectName); } public virtual void Draw() { Console.WriteLine("I'm just a generic drawing object."); } } public class Line : DrawingObject { public Line():base("ForBaseClass, DrawingObject") { Console.WriteLine(this.ToString()); } public override void Draw() { Console.WriteLine("I'm a Line."); } } ForBaseClass, DrawingObject ConsoleApplicationCourseTest.Line Press any key to continue... Polymorphism
  • 41. public class DrawingObject { public DrawingObject(string objectName) { Console.WriteLine(objectName); } public virtual void Draw() { Console.WriteLine("I'm just a generic drawing object."); } } public class Line : DrawingObject { public Line():base("ForBaseClass, DrawingObject") { Console.WriteLine(this.ToString()); } public override void Draw() { Console.WriteLine("I'm a Line."); } } ForBaseClass, DrawingObject ConsoleApplicationCourseTest.Line Press any key to continue... Polymorphism
  • 42. public class DrawingObject { public DrawingObject(string objectName) { Console.WriteLine(objectName); } public virtual void Draw() { Console.WriteLine("I'm just a generic drawing object."); } } public class Line : DrawingObject { public Line():base("ForBaseClass, DrawingObject") { Console.WriteLine(this.ToString()); } public override void Draw() { Console.WriteLine("I'm a Line."); } } ForBaseClass, DrawingObject ConsoleApplicationCourseTest.Line Press any key to continue... Polymorphism
  • 45. public class DrawingObject { public DrawingObject(string objectName) { Console.WriteLine(objectName); } public virtual void Draw() { Console.WriteLine("I'm just a generic drawing object."); } } public class Line : DrawingObject { public Line():base("ForBaseClass, DrawingObject") { Console.WriteLine(this.ToString()); } public override string ToString() { return "just another Line object on runtime!"; } public override void Draw() { Console.WriteLine("I'm a Line."); } } Polymorphism
  • 46. public class DrawingObject { public DrawingObject(string objectName) { Console.WriteLine(objectName); } public virtual void Draw() { Console.WriteLine("I'm just a generic drawing object."); } } public class Line : DrawingObject { public Line():base("ForBaseClass, DrawingObject") { Console.WriteLine(this.ToString()); } public override string ToString() { return "just another Line object on runtime!"; } public override void Draw() { Console.WriteLine("I'm a Line."); } } ForBaseClass, DrawingObject just another Line object on runtime! Press any key to continue... Polymorphism
  • 48. Abstract Classes • Abstract methods do not have an implementation. • Abstract methods are implicitly virtual. • If a class has abstract methods it must be declared abstract itself. • One cannot create objects of an abstract class. abstract class Stream { public abstract void Write(char ch); public void WriteString(string s) { foreach (char ch in s) Write(s); } } class File: Stream { public override void Write(char ch) {... write ch to disk...} }
  • 49. sealed and internal classes sealed: can’t be extended (Java’s final) internal: can’t be used in other namespaces
  • 51. class DerivedClass: BaseClass { public override string Meth1() { return "MyDerived-Meth1"; } public new string Meth2() { return "MyDerived-Meth2"; } public string Meth3() { return "MyDerived-Meth3"; } public static void Main() { DerivedClassmD = new MyDerived(); BaseClass mB = (BaseClass)mD; System.Console.WriteLine(mB.Meth1()); System.Console.WriteLine(mB.Meth2()); System.Console.WriteLine(mB.Meth3()); } } Versioning public class BaseClass { public virtual string Meth1() { return "BaseClass-Meth1"; } public virtual string Meth2() { return "BaseClass-Meth2"; } public virtual string Meth3() { return "BaseClass-Meth3"; } }
  • 52. class DerivedClass: BaseClass { public override string Meth1() { return "MyDerived-Meth1"; } public new string Meth2() { return "MyDerived-Meth2"; } public string Meth3() { return "MyDerived-Meth3"; } public static void Main() { DerivedClassmD = new MyDerived(); BaseClass mB = (BaseClass)mD; System.Console.WriteLine(mB.Meth1()); System.Console.WriteLine(mB.Meth2()); System.Console.WriteLine(mB.Meth3()); } } Versioning Overrides the virtual method Meth1 using the override keyword public class BaseClass { public virtual string Meth1() { return "BaseClass-Meth1"; } public virtual string Meth2() { return "BaseClass-Meth2"; } public virtual string Meth3() { return "BaseClass-Meth3"; } }
  • 53. class DerivedClass: BaseClass { public override string Meth1() { return "MyDerived-Meth1"; } public new string Meth2() { return "MyDerived-Meth2"; } public string Meth3() { return "MyDerived-Meth3"; } public static void Main() { DerivedClassmD = new MyDerived(); BaseClass mB = (BaseClass)mD; System.Console.WriteLine(mB.Meth1()); System.Console.WriteLine(mB.Meth2()); System.Console.WriteLine(mB.Meth3()); } } Versioning Explicitly hide the virtual method Meth2 using the new keyword public class BaseClass { public virtual string Meth1() { return "BaseClass-Meth1"; } public virtual string Meth2() { return "BaseClass-Meth2"; } public virtual string Meth3() { return "BaseClass-Meth3"; } }
  • 54. class DerivedClass: BaseClass { public override string Meth1() { return "MyDerived-Meth1"; } public new string Meth2() { return "MyDerived-Meth2"; } public string Meth3() { return "MyDerived-Meth3"; } public static void Main() { DerivedClassmD = new MyDerived(); BaseClass mB = (BaseClass)mD; System.Console.WriteLine(mB.Meth1()); System.Console.WriteLine(mB.Meth2()); System.Console.WriteLine(mB.Meth3()); } } Versioning Because no keyword is specified in the following declaration a warning will be issued to alert the programmer that the method hides the inherited member BaseClass.Meth3() public class BaseClass { public virtual string Meth1() { return "BaseClass-Meth1"; } public virtual string Meth2() { return "BaseClass-Meth2"; } public virtual string Meth3() { return "BaseClass-Meth3"; } }
  • 55. class DerivedClass: BaseClass { public override string Meth1() { return "MyDerived-Meth1"; } public new string Meth2() { return "MyDerived-Meth2"; } public string Meth3() { return "MyDerived-Meth3"; } public static void Main() { DerivedClassmD = new MyDerived(); BaseClass mB = (BaseClass)mD; System.Console.WriteLine(mB.Meth1()); System.Console.WriteLine(mB.Meth2()); System.Console.WriteLine(mB.Meth3()); } } Versioning public class BaseClass { public virtual string Meth1() { return "BaseClass-Meth1"; } public virtual string Meth2() { return "BaseClass-Meth2"; } public virtual string Meth3() { return "BaseClass-Meth3"; } } MyDerived-Meth1 BaseClass-Meth2 BaseClass-Meth3
  • 57. Multiple Inheritance? C#.NET doesn't allow it C++.NET doesn’t allow it Java doesn’t allow it C++, as you know, allows it
  • 59. However, C# allow multiple interfaces But what are they?
  • 64. Interfaces • An interface contains only the signatures of methods, delegates or events. • The implementation of the methods is done in the class that implements the interface. • Can’t contain Fields!
  • 65. When to use? (An Example)
  • 66. Consider a Human, an Animal and a Car Class, where they all implement a crazy method called ConsumeWater(). If we have many objects of each type of Human, Animal and Car and we want to call ConsumeWater() for all objects of Human, Animal and Car; we have to call it like this: human1.ConsumeWater(); human2.ConsumeWater(); human3.ConsumeWater(); animal1.ConsumeWater(); animal2.ConsumeWater(); car1.ConsumeWater(); car2.ConsumeWater();
  • 67. SomeObject Human Animal Car And they they can’t be subclassed from one particular abstract/base class like this:
  • 68. SomeObject Human Animal Car And they they can’t be subclassed from one particular abstract/base class like this: Because they are not the same and they share some common properties!
  • 69. If we have many objects of each type of Human, Animal and Car and we want to call ConsumeWater() for all objects of Human, Animal and Car; we have to call it like this: human1.ConsumeWater(); human2.ConsumeWater(); human3.ConsumeWater(); animal1.ConsumeWater(); animal2.ConsumeWater(); car1.ConsumeWater(); car2.ConsumeWater(); But if we can implement a common functionalities from a common place, that would be nice! interface Human Animal Car Implementation and not inheritance!
  • 70. If we have many objects of each type of Human, Animal and Car and we want to call ConsumeWater() for all objects of Human, Animal and Car; we have to call it like this: human1.ConsumeWater(); human2.ConsumeWater(); human3.ConsumeWater(); animal1.ConsumeWater(); animal2.ConsumeWater(); car1.ConsumeWater(); car2.ConsumeWater(); But if we can implement a common functionalities from a common place, that would be nice! IWaterable Human Animal Car Implementation and not inheritance!
  • 71. Now we can add all objects to a common list of IWaterable and just call ConsumeWater() for each IWaterable object (they are all Waterable now!) List<IWaterable> waterables = new List<IWaterable>() {“human1”, “human2”, “human3”, “animal1”, “animal2”, “car1”, “car2”}; foreach(IWaterable waterable in waterables) waterable.ConsumeWater(); Look how nice the code is and how clear the relation is. When we implement an interface we are just saying that this interface provides a certain functionality for us (and others may freely have this functionality as well.) IWaterable Human Animal Car Implementation and not inheritance!
  • 73. Interfaces – the Code Public interface IWaterable { public void ConsumeWater(); }
  • 74. Interfaces – the Code Public interface IWaterable { public void ConsumeWater(); } Public class Human: IWaterable { public void ConsumeWater() { } } Public class Animal: IWaterable { public void ConsumeWater() { } } Public class Car: IWaterable { public void ConsumeWater() { } }
  • 75. Interfaces – the Code Public interface IWaterable { public void ConsumeWater(); } Public class Human: IWaterable { public void ConsumeWater() { } } Public class Animal: IWaterable { public void ConsumeWater() { } } Public class Car: IWaterable { public void ConsumeWater() { } }
  • 76. Interfaces – the Code Public interface IWaterable { public void ConsumeWater(); } Public class Human: IWaterable { public void ConsumeWater() { //Drinking Water } } Public class Animal: IWaterable { public void ConsumeWater() { //Drinking Water } } Public class Car: IWaterable { public void ConsumeWater() { //Cooling the engine } }
  • 77. Interfaces – the Code Public interface IWaterable { public void ConsumeWater(); } Public class Human: IWaterable { public void ConsumeWater() { //Drinking Water } } Public class Animal:IWaterable, INosiable { public void ConsumeWater() { //Drinking Water } public void MakeNoise() { //Mew, Roar or Moo! } } Public class Car: IWaterable, INosiable { public void ConsumeWater() { //Cooling the engine } public void MakeNoise() { //Rev the engine! } } Public interface INoisable { public void MakeNoise(); }
  • 78. Interfaces • An interface can be a member of a namespace or a class and can contain signatures of the following members: – Methods – Properties – Indexers – Events • “No” Fields!
  • 80. using System; class Program { static void Main() { float lengthFloat = 7.35f; // lose precision - explicit conversion int lengthInt = (int)lengthFloat; // no problem - implicit conversion double lengthDouble = lengthInt; Console.WriteLine("lengthInt = " + lengthInt); Console.WriteLine("lengthDouble = " + lengthDouble); Console.ReadKey(); } } Reference VS Value Types
  • 81. using System; class Program { static void Main() { float lengthFloat = 7.35f; // lose precision - explicit conversion int lengthInt = (int)lengthFloat; // no problem - implicit conversion double lengthDouble = lengthInt; Console.WriteLine("lengthInt = " + lengthInt); Console.WriteLine("lengthDouble = " + lengthDouble); Console.ReadKey(); } } lengthInt = 7 lengthDouble = 7 Reference VS Value Types
  • 82. Reference VS Value Types • Reference type • variables are named appropriately (reference) because the variable holds a reference to an object. • In C and C++, we have something similar that which is “a pointer”, which points to an object. While you can modify a pointer, you can't modify the value of a reference - it simply points at the object in memory.
  • 83. using System; class Employee { private string _name; public string Name { get { return _name; } set { _name = value; } } } Reference VS Value Types
  • 84. Reference Types class Program { static void Main() { Employee joe = new Employee(); joe.Name = "Joe"; Employee bob = new Employee(); bob.Name = "Bob"; Console.WriteLine("Original Employee Values:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); // assign joe reference to bob variable bob = joe; Console.WriteLine("Values After Reference Assignment:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); joe.Name = "Bobbi Jo"; Console.WriteLine("Values After Changing One Instance:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); Console.ReadKey(); } }
  • 85. Reference Types class Program { static void Main() { Employee joe = new Employee(); joe.Name = "Joe"; Employee bob = new Employee(); bob.Name = "Bob"; Console.WriteLine("Original Employee Values:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); // assign joe reference to bob variable bob = joe; Console.WriteLine("Values After Reference Assignment:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); joe.Name = "Bobbi Jo"; Console.WriteLine("Values After Changing One Instance:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); Console.ReadKey(); } } Original Employee Values: joe = Joe bob = Bob Values After Reference Assignment: joe = Joe bob = Joe Values After Changing One Instance: joe = Bobbi Jo bob = Bobbi Jo
  • 86. Reference Types class Program { static void Main() { Employee joe = new Employee(); joe.Name = "Joe"; Employee bob = new Employee(); bob.Name = "Bob"; Console.WriteLine("Original Employee Values:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); // assign joe reference to bob variable bob = joe; Console.WriteLine("Values After Reference Assignment:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); joe.Name = "Bobbi Jo"; Console.WriteLine("Values After Changing One Instance:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); Console.ReadKey(); } } Original Employee Values: joe = Joe bob = Bob Values After Reference Assignment: joe = Joe bob = Joe Values After Changing One Instance: joe = Bobbi Jo bob = Bobbi Jo How is that?!
  • 87. Reference Types class Program { static void Main() { Employee joe = new Employee(); joe.Name = "Joe"; Employee bob = new Employee(); bob.Name = "Bob"; Console.WriteLine("Original Employee Values:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); // assign joe reference to bob variable bob = joe; Console.WriteLine("Values After Reference Assignment:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); joe.Name = "Bobbi Jo"; Console.WriteLine("Values After Changing One Instance:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); Console.ReadKey(); } } Emp Emp joe bob
  • 88. Reference Types class Program { static void Main() { Employee joe = new Employee(); joe.Name = "Joe"; Employee bob = new Employee(); bob.Name = "Bob"; Console.WriteLine("Original Employee Values:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); // assign joe reference to bob variable bob = joe; Console.WriteLine("Values After Reference Assignment:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); joe.Name = "Bobbi Jo"; Console.WriteLine("Values After Changing One Instance:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); Console.ReadKey(); } } Emp Emp joe bob
  • 89. Reference Types class Program { static void Main() { Employee joe = new Employee(); joe.Name = "Joe"; Employee bob = new Employee(); bob.Name = "Bob"; Console.WriteLine("Original Employee Values:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); // assign joe reference to bob variable bob = joe; Console.WriteLine("Values After Reference Assignment:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); joe.Name = "Bobbi Jo"; Console.WriteLine("Values After Changing One Instance:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); Console.ReadKey(); } } Emp Emp joe bob
  • 90. Reference Types class Program { static void Main() { Employee joe = new Employee(); joe.Name = "Joe"; Employee bob = new Employee(); bob.Name = "Bob"; Console.WriteLine("Original Employee Values:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); // assign joe reference to bob variable bob = joe; Console.WriteLine("Values After Reference Assignment:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); joe.Name = "Bobbi Jo"; Console.WriteLine("Values After Changing One Instance:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); Console.ReadKey(); } } Emp Emp joe bob
  • 91. Reference Types class Program { static void Main() { Employee joe = new Employee(); joe.Name = "Joe"; Employee bob = new Employee(); bob.Name = "Bob"; Console.WriteLine("Original Employee Values:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); // assign joe reference to bob variable bob = joe; Console.WriteLine("Values After Reference Assignment:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); joe.Name = "Bobbi Jo"; Console.WriteLine("Values After Changing One Instance:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); Console.ReadKey(); } } Emp Emp joe bob
  • 92. Reference Types class Program { static void Main() { Employee joe = new Employee(); joe.Name = "Joe"; Employee bob = new Employee(); bob.Name = "Bob"; Console.WriteLine("Original Employee Values:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); // assign joe reference to bob variable bob = joe; Console.WriteLine("Values After Reference Assignment:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); joe.Name = "Bobbi Jo"; Console.WriteLine("Values After Changing One Instance:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); Console.ReadKey(); } } Emp joe Emp bob
  • 93. Reference Types class Program { static void Main() { Employee joe = new Employee(); joe.Name = "Joe"; Employee bob = new Employee(); bob.Name = "Bob"; Console.WriteLine("Original Employee Values:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); // assign joe reference to bob variable bob = joe; Console.WriteLine("Values After Reference Assignment:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); joe.Name = "Bobbi Jo"; Console.WriteLine("Values After Changing One Instance:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); Console.ReadKey(); } } Emp joe Emp bob
  • 94. Reference Types class Program { static void Main() { Employee joe = new Employee(); joe.Name = "Joe"; Employee bob = new Employee(); bob.Name = "Bob"; Console.WriteLine("Original Employee Values:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); // assign joe reference to bob variable bob = joe; Console.WriteLine("Values After Reference Assignment:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); joe.Name = "Bobbi Jo"; Console.WriteLine("Values After Changing One Instance:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); Console.ReadKey(); } } Emp joe Emp bob
  • 95. Reference Types class Program { static void Main() { Employee joe = new Employee(); joe.Name = "Joe"; Employee bob = new Employee(); bob.Name = "Bob"; Console.WriteLine("Original Employee Values:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); // assign joe reference to bob variable bob = joe; Console.WriteLine("Values After Reference Assignment:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); joe.Name = "Bobbi Jo"; Console.WriteLine("Values After Changing One Instance:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); Console.ReadKey(); } } Emp joe bob
  • 96. Reference Types class Program { static void Main() { Employee joe = new Employee(); joe.Name = "Joe"; Employee bob = new Employee(); bob.Name = "Bob"; Console.WriteLine("Original Employee Values:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); // assign joe reference to bob variable bob = joe; Console.WriteLine("Values After Reference Assignment:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); joe.Name = "Bobbi Jo"; Console.WriteLine("Values After Changing One Instance:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); Console.ReadKey(); } } joe bob Emp Name = “Joe”
  • 97. Reference Types class Program { static void Main() { Employee joe = new Employee(); joe.Name = "Joe"; Employee bob = new Employee(); bob.Name = "Bob"; Console.WriteLine("Original Employee Values:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); // assign joe reference to bob variable bob = joe; Console.WriteLine("Values After Reference Assignment:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); joe.Name = "Bobbi Jo"; Console.WriteLine("Values After Changing One Instance:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); Console.ReadKey(); } } joe bob Emp Name = “Bobbi Jo”
  • 98. Reference Types class Program { static void Main() { Employee joe = new Employee(); joe.Name = "Joe"; Employee bob = new Employee(); bob.Name = "Bob"; Console.WriteLine("Original Employee Values:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); // assign joe reference to bob variable bob = joe; Console.WriteLine("Values After Reference Assignment:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); joe.Name = "Bobbi Jo"; Console.WriteLine("Values After Changing One Instance:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); Console.ReadKey(); } } joe bob Emp Name = “Bobbi Jo”
  • 99. Reference Types class Program { static void Main() { Employee joe = new Employee(); joe.Name = "Joe"; Employee bob = new Employee(); bob.Name = "Bob"; Console.WriteLine("Original Employee Values:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); // assign joe reference to bob variable bob = joe; Console.WriteLine("Values After Reference Assignment:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); joe.Name = "Bobbi Jo"; Console.WriteLine("Values After Changing One Instance:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); Console.ReadKey(); } } joe bob Emp Name = “Bobbi Jo” Original Employee Values: joe = Joe bob = Bob Values After Reference Assignment: joe = Joe bob = Joe Values After Changing One Instance: joe = Bobbi Jo bob = Bobbi Jo
  • 100. Reference Types • The following types are reference types: • arrays • class • delegates • interfaces
  • 102. Value Types • A value type – variable holds its own copy of an object and when you perform assignment from one value type variable to another, both the left-hand-side and right-hand-side of the assignment hold two separate copies of that value.
  • 103. Value Types • A value type – variable holds its own copy of an object and when you perform assignment from one value type variable to another, both the left-hand-side and right-hand-side of the assignment hold two separate copies of that value. • An important fact you need to understand is that when you are assigning one reference type variable to another, only the reference is copied, not the object. The variable holds the reference and that is what is being copied.
  • 104. struct Height { private int m_inches; public int Inches { get { return m_inches; } set { m_inches = value; } } } Value Types
  • 105. class Program { static void Main() { Height joe = new Height(); joe.Inches = 71; Height bob = new Height(); bob.Inches = 59; Console.WriteLine("Original Height Values:"); Console.WriteLine("joe = " + joe.Inches); Console.WriteLine("bob = " + bob.Inches); bob = joe; Console.WriteLine("Values After Value Assignment:"); Console.WriteLine("joe = " + joe.Inches); Console.WriteLine("bob = " + bob.Inches); joe.Inches = 65; Console.WriteLine("Values After Changing One Instance:"); Console.WriteLine("joe = " + joe.Inches); Console.WriteLine("bob = " + bob.Inches); Console.ReadKey(); } } Value Types
  • 106. class Program { static void Main() { Height joe = new Height(); joe.Inches = 71; Height bob = new Height(); bob.Inches = 59; Console.WriteLine("Original Height Values:"); Console.WriteLine("joe = " + joe.Inches); Console.WriteLine("bob = " + bob.Inches); bob = joe; Console.WriteLine("Values After Value Assignment:"); Console.WriteLine("joe = " + joe.Inches); Console.WriteLine("bob = " + bob.Inches); joe.Inches = 65; Console.WriteLine("Values After Changing One Instance:"); Console.WriteLine("joe = " + joe.Inches); Console.WriteLine("bob = " + bob.Inches); Console.ReadKey(); } } Value Types Height Height joe bob
  • 107. class Program { static void Main() { Height joe = new Height(); joe.Inches = 71; Height bob = new Height(); bob.Inches = 59; Console.WriteLine("Original Height Values:"); Console.WriteLine("joe = " + joe.Inches); Console.WriteLine("bob = " + bob.Inches); bob = joe; Console.WriteLine("Values After Value Assignment:"); Console.WriteLine("joe = " + joe.Inches); Console.WriteLine("bob = " + bob.Inches); joe.Inches = 65; Console.WriteLine("Values After Changing One Instance:"); Console.WriteLine("joe = " + joe.Inches); Console.WriteLine("bob = " + bob.Inches); Console.ReadKey(); } } Value Types Height Height joe bob
  • 108. class Program { static void Main() { Height joe = new Height(); joe.Inches = 71; Height bob = new Height(); bob.Inches = 59; Console.WriteLine("Original Height Values:"); Console.WriteLine("joe = " + joe.Inches); Console.WriteLine("bob = " + bob.Inches); bob = joe; Console.WriteLine("Values After Value Assignment:"); Console.WriteLine("joe = " + joe.Inches); Console.WriteLine("bob = " + bob.Inches); joe.Inches = 65; Console.WriteLine("Values After Changing One Instance:"); Console.WriteLine("joe = " + joe.Inches); Console.WriteLine("bob = " + bob.Inches); Console.ReadKey(); } } Value Types Height Height joe bob Height
  • 109. class Program { static void Main() { Height joe = new Height(); joe.Inches = 71; Height bob = new Height(); bob.Inches = 59; Console.WriteLine("Original Height Values:"); Console.WriteLine("joe = " + joe.Inches); Console.WriteLine("bob = " + bob.Inches); bob = joe; Console.WriteLine("Values After Value Assignment:"); Console.WriteLine("joe = " + joe.Inches); Console.WriteLine("bob = " + bob.Inches); joe.Inches = 65; Console.WriteLine("Values After Changing One Instance:"); Console.WriteLine("joe = " + joe.Inches); Console.WriteLine("bob = " + bob.Inches); Console.ReadKey(); } } Value Types Height Height joe bob Exactly the same
  • 110. class Program { static void Main() { Height joe = new Height(); joe.Inches = 71; Height bob = new Height(); bob.Inches = 59; Console.WriteLine("Original Height Values:"); Console.WriteLine("joe = " + joe.Inches); Console.WriteLine("bob = " + bob.Inches); bob = joe; Console.WriteLine("Values After Value Assignment:"); Console.WriteLine("joe = " + joe.Inches); Console.WriteLine("bob = " + bob.Inches); joe.Inches = 65; Console.WriteLine("Values After Changing One Instance:"); Console.WriteLine("joe = " + joe.Inches); Console.WriteLine("bob = " + bob.Inches); Console.ReadKey(); } } Value Types 71 71 joe bob Exactly the same
  • 111. class Program { static void Main() { Height joe = new Height(); joe.Inches = 71; Height bob = new Height(); bob.Inches = 59; Console.WriteLine("Original Height Values:"); Console.WriteLine("joe = " + joe.Inches); Console.WriteLine("bob = " + bob.Inches); bob = joe; Console.WriteLine("Values After Value Assignment:"); Console.WriteLine("joe = " + joe.Inches); Console.WriteLine("bob = " + bob.Inches); joe.Inches = 65; Console.WriteLine("Values After Changing One Instance:"); Console.WriteLine("joe = " + joe.Inches); Console.WriteLine("bob = " + bob.Inches); Console.ReadKey(); } } Value Types 71 71 joe bob
  • 112. class Program { static void Main() { Height joe = new Height(); joe.Inches = 71; Height bob = new Height(); bob.Inches = 59; Console.WriteLine("Original Height Values:"); Console.WriteLine("joe = " + joe.Inches); Console.WriteLine("bob = " + bob.Inches); bob = joe; Console.WriteLine("Values After Value Assignment:"); Console.WriteLine("joe = " + joe.Inches); Console.WriteLine("bob = " + bob.Inches); joe.Inches = 65; Console.WriteLine("Values After Changing One Instance:"); Console.WriteLine("joe = " + joe.Inches); Console.WriteLine("bob = " + bob.Inches); Console.ReadKey(); } } Value Types 65 71 joe bob
  • 113. class Program { static void Main() { Height joe = new Height(); joe.Inches = 71; Height bob = new Height(); bob.Inches = 59; Console.WriteLine("Original Height Values:"); Console.WriteLine("joe = " + joe.Inches); Console.WriteLine("bob = " + bob.Inches); bob = joe; Console.WriteLine("Values After Value Assignment:"); Console.WriteLine("joe = " + joe.Inches); Console.WriteLine("bob = " + bob.Inches); joe.Inches = 65; Console.WriteLine("Values After Changing One Instance:"); Console.WriteLine("joe = " + joe.Inches); Console.WriteLine("bob = " + bob.Inches); Console.ReadKey(); } } Value Types 65 71 joe bob Original Height Values: joe = 71 bob = 59 Values After Value Assignment: joe = 71 bob = 71 Values After Changing One Instance: joe = 65 bob = 71
  • 114. Value Types • The following types are value types: – enum – struct
  • 115. Classes and Structs Classes • Reference Types • (objects stored on the heap) • support inheritance • (all classes are derived from object) • can implement interfaces • may have a destructor Structs • Value Types • (objects stored on the stack) • no inheritance • (but compatible with object) • can implement interfaces • no destructors allowed
  • 116. Creating a Class Library Project for Your Project’s Logic
  • 117. Now write all your code in the Class Library project and reference it in your presentation layer project
  • 118. Adding References to Other Projects to Your Project
  • 119. Adding References to Your Project
  • 120. The Principles • Single Responsibility Principle: design your classes so that each has a single purpose • Open / Closed Principle: Open for extension but closed for modification • Liskov Substitution Principle (LSP): functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it • Interface Segregation Principle (ISP): clients should not be forced to depend upon interfaces that they do not use. • Dependency Inversion Principle (DIP): high level modules should not depend upon low level modules. Both should depend upon abstractions. abstractions should not depend upon details. Details should depend upon abstractions.
  • 121. is the process of validating the correctness of a small section of code. The target code may be a method within a class, a group of members or even entire components that are isolated from all or most of their dependencies.
  • 122.
  • 123. Question #1 public class BaseClass { public virtual string Meth1() { return "BaseClass-Meth1"; } public string Meth2() { return "BaseClass-Meth2"; } public virtual string Meth3() { return "BaseClass-Meth3"; } } class DerivedClass: BaseClass { public override string Meth1() { return "MyDerived-Meth1"; } public new string Meth2() { return "MyDerived-Meth2"; } public string Meth3() { return "MyDerived-Meth3"; } public static void Main() { DerivedClassmD = new MyDerived(); BaseClass mB = mD; System.Console.WriteLine(mB.Meth1()); System.Console.WriteLine(mB.Meth2()); System.Console.WriteLine(mB.Meth3()); } }
  • 124. Question #1 public class BaseClass { public virtual string Meth1() { return "BaseClass-Meth1"; } public string Meth2() { return "BaseClass-Meth2"; } public virtual string Meth3() { return "BaseClass-Meth3"; } } class DerivedClass: BaseClass { public override string Meth1() { return "MyDerived-Meth1"; } public new string Meth2() { return "MyDerived-Meth2"; } public string Meth3() { return "MyDerived-Meth3"; } public static void Main() { DerivedClassmD = new MyDerived(); BaseClass mB = mD; System.Console.WriteLine(mB.Meth1()); System.Console.WriteLine(mB.Meth2()); System.Console.WriteLine(mB.Meth3()); } } MyDerived-Meth1 BaseClass-Meth2 BaseClass-Meth3 Press any key to continue...
  • 125. Question #2 class Class1 { } class Class2 : Class1{ } class Class3 { } public class TestingClass { public static void Test(object o) { Class1 a; Class2 b; Class3 c; if (o is Class1) { Console.WriteLine("obj is Class1"); a = (Class1)o; } else if (o is Class2) { Console.WriteLine("obj is Class2"); b = (Class2)o; } else if (o is Class3) { Console.WriteLine("obj is Class3"); c = (Class3)o; } else if((Class3)o!= null) {} } public static void Main() { try { Class1 c1 = new Class1(); Class2 c2 = new Class2(); Class3 c3 = new Class3(); Test(c1); Test(c2); Test(c3); Test("a string"); } catch(Exception e) { Console.WriteLine("Sth wrong happened!"); } } }
  • 126. Question #2 class Class1 { } class Class2 : Class1{ } class Class3 { } public class TestingClass { public static void Test(object o) { Class1 a; Class2 b; Class3 c; if (o is Class1) { Console.WriteLine("obj is Class1"); a = (Class1)o; } else if (o is Class2) { Console.WriteLine("obj is Class2"); b = (Class2)o; } else if (o is Class3) { Console.WriteLine("obj is Class3"); c = (Class3)o; } else if((Class3)o!= null) {} } public static void Main() { try { Class1 c1 = new Class1(); Class2 c2 = new Class2(); Class3 c3 = new Class3(); Test(c1); Test(c2); Test(c3); Test("a string"); } catch(Exception e) { Console.WriteLine("Sth wrong happened!"); } } } obj is Class1 obj is Class1 obj is Class3 Sth wrong happened! Press any key to continue...
  • 127. public interface IsBaseTest { void Point1(object obj); } public class IsTest { public static void Point1(object obj) { Console.WriteLine(obj.ToString()); Point2("That's the point"); } public static void Point2(string str) { Console.WriteLine(str.ToString()); Point3("That's the point"); } public static void Point3(object obj) { if (obj.ToString() == " Passed String") { Console.WriteLine("In Point3"); } } public static void Main() { Point1("Passed String"); } } Question #3
  • 128. public interface IsBaseTest { void Point1(object obj); } public class IsTest { public static void Point1(object obj) { Console.WriteLine(obj.ToString()); Point2("That's the point"); } public static void Point2(string str) { Console.WriteLine(str.ToString()); Point3("That's the point"); } public static void Point3(object obj) { if (obj.ToString() == " Passed String") { Console.WriteLine("In Point3"); } } public static void Main() { Point1("Passed String"); } } Question #3 Passed String That's the point In Point3 Press any key to continue...
  • 129. That’s it for today! Hope you enjoy it!