A Simple Class and Objects : Class Variables : Class Interface C# Source Code


Custom Search

C# Source Code » Class Interface » Class Variables »

 

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);
    }
}

 
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Class Interface
» Class Variables