Created
June 22, 2015 15:51
-
-
Save woodss/579b3a6d8fa01974a136 to your computer and use it in GitHub Desktop.
How to use Constructors and Deconstructors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ConDecon | |
{ | |
class Car | |
{ | |
public string Colour { get; set; } | |
public int numWheels { get; set; } | |
public Car() | |
{ | |
Console.WriteLine("We have just created our car"); | |
Colour = "black"; | |
numWheels = 4; | |
Console.WriteLine("The default car colour is " + Colour); | |
} | |
public Car(string col) : this() { | |
Colour = col; | |
} | |
public Car(int wheels) | |
: this() | |
{ | |
numWheels = wheels; | |
} | |
public Car(string col, int wheels) | |
: this() | |
{ | |
Colour = col; | |
numWheels = wheels; | |
} | |
~Car() | |
{ | |
// This will be called when the car object is discard | |
Console.WriteLine("The car has crashed - it had " + numWheels + " wheels"); | |
Console.ReadLine(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ConDecon | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Car mycar = new Car(3); | |
Console.WriteLine("My car is coloured " + mycar.Colour); | |
Console.WriteLine("My car has " + mycar.numWheels + " wheels"); | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment