This lesson digs into more details about classes—what exactly happens when you create a new instance of a class? What is a reference to an instance of a class? How does passing the reference to a method affect a class? We also review overloaded methods, static versus instance methods, and constructors.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ObjectLifetime
{
class Program
{
static void Main(string[] args)
{
Car myCar = new Car();
// set properties
Car myOtherCar = myCar;
Car myThirdCar = new Car("Ford", "Escape", 2005, "White");
myOtherCar = null;
myCar = null;
}
}
class Car
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public string Color { get; set; }
public double OriginalPrice { get; set; }
public Car()
{
// You could this from a configuration file, a database, etc.
// I'll just hardcode in this instance.
this.Make = "Nissan";
}
public Car(string make, string model, int year, string color)
{
Make = make;
Model = model;
Year = year;
Color = color;
}
/*
public Car(string someOtherInputParameter, string model, int year, string color)
{
Make = someOtherInputParameter;
Model = model;
Year = year;
Color = color;
}
* */
public static void MyMethod()
{
}
}
}
Source : MS Virtual Academy
No comments:
Post a Comment