Mega Code Archive

 
Categories / C# / Class Interface
 

A Simple Class and Objects

public class Product {     public string make;     public string model;     public string color;     public int yearBuilt;     public void Start() {         System.Console.WriteLine(model + " started");     }     public void Stop() {         System.Console.WriteLine(model + " stopped");     } } class MainClass{     public static void Main() {         Product myProduct;         myProduct = new Product();         myProduct.make = "Toyota";         myProduct.model = "MR2";         myProduct.color = "black";         myProduct.yearBuilt = 1995;         System.Console.WriteLine("myProduct details:");         System.Console.WriteLine("myProduct.make = " + myProduct.make);         System.Console.WriteLine("myProduct.model = " + myProduct.model);         System.Console.WriteLine("myProduct.color = " + myProduct.color);         System.Console.WriteLine("myProduct.yearBuilt = " + myProduct.yearBuilt);         myProduct.Start();         myProduct.Stop();         Product redPorsche = new Product();         redPorsche.make = "Porsche";         redPorsche.model = "Boxster";         redPorsche.color = "red";         redPorsche.yearBuilt = 2000;         System.Console.WriteLine(          "redPorsche is a " + redPorsche.model);         System.Console.WriteLine("Assigning redPorsche to myProduct");         myProduct = redPorsche;         System.Console.WriteLine("myProduct details:");         System.Console.WriteLine("myProduct.make = " + myProduct.make);         System.Console.WriteLine("myProduct.model = " + myProduct.model);         System.Console.WriteLine("myProduct.color = " + myProduct.color);         System.Console.WriteLine("myProduct.yearBuilt = " + myProduct.yearBuilt);     } }