Download - C# Starter L02-Classes and Objects
![Page 1: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/1.jpg)
Mohammad Shakermohammadshaker.com
C# Programming Course@ZGTRShaker
2011, 2012, 2013, 2014
C# StarterL02 – Classes, Polymorphism, Versioning, Interfaces, Reference and Value Types
![Page 2: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/2.jpg)
Today’s Agenda
• Classes
• Inheritance
• Polymorphism
• Versioning
• Interfaces
• Reference types VS Value types
![Page 3: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/3.jpg)
Object oriented programming (OOP) is a way of programming where you create chunks of code that match up with real world objects.
![Page 4: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/4.jpg)
Classes
![Page 5: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/5.jpg)
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;
}}
![Page 6: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/6.jpg)
Classes and ObjectsWho is Who?
![Page 7: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/7.jpg)
Objects appear only at Runtime
![Page 8: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/8.jpg)
this Keyword
![Page 9: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/9.jpg)
this KeywordWhat is it?
![Page 10: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/10.jpg)
this KeywordA pointer pointing on the object itself at runtime
![Page 11: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/11.jpg)
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;
}
![Page 12: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/12.jpg)
Static KeywordWhat is it?
![Page 13: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/13.jpg)
Destructors
![Page 14: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/14.jpg)
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
}}
![Page 15: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/15.jpg)
Inheritance
![Page 16: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/16.jpg)
Constructors and Inheritance
![Page 17: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/17.jpg)
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
![Page 18: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/18.jpg)
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
![Page 19: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/19.jpg)
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
![Page 20: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/20.jpg)
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
![Page 21: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/21.jpg)
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
![Page 22: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/22.jpg)
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
![Page 23: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/23.jpg)
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 DerivedChild Constructor.I'm a Parent Class.I'm a Child Class.I'm a Parent Class.
Inheritance
![Page 24: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/24.jpg)
Polymorphism
![Page 25: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/25.jpg)
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
![Page 26: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/26.jpg)
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
![Page 27: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/27.jpg)
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
![Page 28: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/28.jpg)
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
![Page 29: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/29.jpg)
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
![Page 30: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/30.jpg)
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
![Page 31: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/31.jpg)
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
![Page 32: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/32.jpg)
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
![Page 33: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/33.jpg)
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
![Page 34: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/34.jpg)
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
![Page 35: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/35.jpg)
Polymorphism
![Page 36: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/36.jpg)
Polymorphism
![Page 37: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/37.jpg)
Polymorphism
![Page 38: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/38.jpg)
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
![Page 39: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/39.jpg)
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
![Page 40: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/40.jpg)
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, DrawingObjectConsoleApplicationCourseTest.LinePress any key to continue...
Polymorphism
![Page 41: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/41.jpg)
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, DrawingObjectConsoleApplicationCourseTest.LinePress any key to continue...
Polymorphism
![Page 42: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/42.jpg)
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, DrawingObjectConsoleApplicationCourseTest.LinePress any key to continue...
Polymorphism
![Page 43: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/43.jpg)
Polymorphism
![Page 44: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/44.jpg)
Polymorphism
![Page 45: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/45.jpg)
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
![Page 46: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/46.jpg)
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, DrawingObjectjust another Line object on runtime!Press any key to continue...
Polymorphism
![Page 47: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/47.jpg)
Abstract Classes
![Page 48: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/48.jpg)
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...}}
![Page 49: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/49.jpg)
sealed and internal classessealed: can’t be extended (Java’s final)
internal: can’t be used in other namespaces
![Page 50: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/50.jpg)
Versioning
![Page 51: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/51.jpg)
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";}
}
![Page 52: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/52.jpg)
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";}
}
![Page 53: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/53.jpg)
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";}
}
![Page 54: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/54.jpg)
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";}
}
![Page 55: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/55.jpg)
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-Meth1BaseClass-Meth2BaseClass-Meth3
![Page 56: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/56.jpg)
Multiple Inheritance?C#.NET doesn't allow it, Why?
![Page 57: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/57.jpg)
Multiple Inheritance?C#.NET doesn't allow it
C++.NET doesn’t allow itJava doesn’t allow it
C++, as you know, allows it
![Page 58: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/58.jpg)
However, C# allow multiple interfaces
![Page 59: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/59.jpg)
However, C# allow multiple interfacesBut what are they?
![Page 60: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/60.jpg)
Interfaces
![Page 61: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/61.jpg)
Interfaces :D
![Page 62: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/62.jpg)
Interfaces – The concept
![Page 63: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/63.jpg)
Interfaces VS Abstract Classes
![Page 64: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/64.jpg)
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!
![Page 65: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/65.jpg)
When to use?(An Example)
![Page 66: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/66.jpg)
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();
![Page 67: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/67.jpg)
SomeObject
Human Animal Car
And they they can’t be subclassed from one particular abstract/base class like this:
![Page 68: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/68.jpg)
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!
![Page 69: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/69.jpg)
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!
![Page 70: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/70.jpg)
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!
![Page 71: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/71.jpg)
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!
![Page 72: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/72.jpg)
Interfaces – the Code
![Page 73: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/73.jpg)
Interfaces – the CodePublic interface IWaterable{
public void ConsumeWater();}
![Page 74: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/74.jpg)
Interfaces – the CodePublic 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(){
} }
![Page 75: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/75.jpg)
Interfaces – the CodePublic 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(){
} }
![Page 76: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/76.jpg)
Interfaces – the CodePublic 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}
}
![Page 77: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/77.jpg)
Interfaces – the CodePublic 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();}
![Page 78: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/78.jpg)
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!
![Page 79: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/79.jpg)
Reference VS Value Types
![Page 80: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/80.jpg)
using System;
class Program{
static void Main(){
float lengthFloat = 7.35f;
// lose precision - explicit conversionint lengthInt = (int)lengthFloat;
// no problem - implicit conversiondouble lengthDouble = lengthInt;
Console.WriteLine("lengthInt = " + lengthInt);Console.WriteLine("lengthDouble = " + lengthDouble);Console.ReadKey();
}}
Reference VS Value Types
![Page 81: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/81.jpg)
using System;
class Program{
static void Main(){
float lengthFloat = 7.35f;
// lose precision - explicit conversionint lengthInt = (int)lengthFloat;
// no problem - implicit conversiondouble lengthDouble = lengthInt;
Console.WriteLine("lengthInt = " + lengthInt);Console.WriteLine("lengthDouble = " + lengthDouble);Console.ReadKey();
}}
lengthInt = 7lengthDouble = 7
Reference VS Value Types
![Page 82: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/82.jpg)
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.
![Page 83: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/83.jpg)
using System;
class Employee{
private string _name;
public string Name{
get { return _name; }set { _name = value; }
}}
Reference VS Value Types
![Page 84: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/84.jpg)
Reference Typesclass 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 variablebob = 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();}
}
![Page 85: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/85.jpg)
Reference Typesclass 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 variablebob = 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 = Joebob = BobValues After Reference Assignment:joe = Joebob = JoeValues After Changing One Instance:joe = Bobbi Jobob = Bobbi Jo
![Page 86: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/86.jpg)
Reference Typesclass 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 variablebob = 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 = Joebob = BobValues After Reference Assignment:joe = Joebob = JoeValues After Changing One Instance:joe = Bobbi Jobob = Bobbi Jo
How is that?!
![Page 87: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/87.jpg)
Reference Typesclass 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 variablebob = 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
![Page 88: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/88.jpg)
Reference Typesclass 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 variablebob = 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
![Page 89: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/89.jpg)
Reference Typesclass 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 variablebob = 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
![Page 90: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/90.jpg)
Reference Typesclass 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 variablebob = 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
![Page 91: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/91.jpg)
Reference Typesclass 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 variablebob = 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
![Page 92: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/92.jpg)
Reference Typesclass 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 variablebob = 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
![Page 93: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/93.jpg)
Reference Typesclass 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 variablebob = 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
![Page 94: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/94.jpg)
Reference Typesclass 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 variablebob = 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
![Page 95: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/95.jpg)
Reference Typesclass 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 variablebob = 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
![Page 96: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/96.jpg)
Reference Typesclass 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 variablebob = 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
EmpName = “Joe”
![Page 97: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/97.jpg)
Reference Typesclass 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 variablebob = 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
EmpName =
“Bobbi Jo”
![Page 98: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/98.jpg)
Reference Typesclass 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 variablebob = 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
EmpName =
“Bobbi Jo”
![Page 99: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/99.jpg)
Reference Typesclass 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 variablebob = 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
EmpName =
“Bobbi Jo”
Original Employee Values:joe = Joebob = BobValues After Reference Assignment:joe = Joebob = JoeValues After Changing One Instance:joe = Bobbi Jobob = Bobbi Jo
![Page 100: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/100.jpg)
Reference Types
• The following types are reference types:
• arrays
• class
• delegates
• interfaces
![Page 101: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/101.jpg)
Value Types
![Page 102: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/102.jpg)
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.
![Page 103: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/103.jpg)
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.
![Page 104: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/104.jpg)
struct Height{
private int m_inches;
public int Inches{
get { return m_inches; }set { m_inches = value; }
}}
Value Types
![Page 105: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/105.jpg)
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
![Page 106: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/106.jpg)
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 TypesHeight Height
joe bob
![Page 107: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/107.jpg)
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 TypesHeight Height
joe bob
![Page 108: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/108.jpg)
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 TypesHeight Height
joe bob
Height
![Page 109: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/109.jpg)
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 TypesHeight Height
joe bob
Exactly the
same
![Page 110: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/110.jpg)
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 Types71 71
joe bob
Exactly the
same
![Page 111: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/111.jpg)
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 Types71 71
joe bob
![Page 112: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/112.jpg)
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 Types65 71
joe bob
![Page 113: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/113.jpg)
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 Types65 71
joe bob
Original Height Values:joe = 71bob = 59Values After Value Assignment:joe = 71bob = 71Values After Changing One Instance:joe = 65bob = 71
![Page 114: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/114.jpg)
Value Types
• The following types are value types:
– enum
– struct
![Page 115: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/115.jpg)
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
![Page 116: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/116.jpg)
Creating a Class Library Project for Your Project’s Logic
![Page 117: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/117.jpg)
Now write all your code in the Class Library project and reference it in your presentation layer project
![Page 118: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/118.jpg)
Adding References to Other Projects to Your Project
![Page 119: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/119.jpg)
Adding References to Your Project
![Page 120: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/120.jpg)
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.
![Page 121: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/121.jpg)
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.
![Page 122: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/122.jpg)
![Page 123: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/123.jpg)
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());
}}
![Page 124: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/124.jpg)
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-Meth1BaseClass-Meth2BaseClass-Meth3Press any key to continue...
![Page 125: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/125.jpg)
Question #2class 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!");}
}}
![Page 126: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/126.jpg)
Question #2class 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 Class1obj is Class1obj is Class3Sth wrong happened!Press any key to continue...
![Page 127: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/127.jpg)
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
![Page 128: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/128.jpg)
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 StringThat's the pointIn Point3Press any key to continue...
![Page 129: C# Starter L02-Classes and Objects](https://reader034.vdocuments.mx/reader034/viewer/2022052618/554ebbcab4c905de468b4782/html5/thumbnails/129.jpg)
That’s it for today!Hope you enjoy it!