Skip to content

Instantly share code, notes, and snippets.

@woodss
Created June 22, 2015 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woodss/579b3a6d8fa01974a136 to your computer and use it in GitHub Desktop.
Save woodss/579b3a6d8fa01974a136 to your computer and use it in GitHub Desktop.
How to use Constructors and Deconstructors
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();
}
}
}
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