Created
May 4, 2024 12:03
-
-
Save Retrockit/1757719a31fd6433e52ad6a98dc57c3d to your computer and use it in GitHub Desktop.
Car Class Example
This file contains hidden or 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
public class Car | |
{ | |
// Properties | |
public string Color { get; set; } | |
public string Model { get; set; } | |
public int Year { get; set; } | |
// Constructor | |
public Car(string color, string model, int year) | |
{ | |
Color = color; | |
Model = model; | |
Year = year; | |
} | |
// Method | |
public void Start() | |
{ | |
Console.WriteLine($"The {Model} is starting."); | |
} | |
public void DisplayInfo() | |
{ | |
Console.WriteLine($"A {Year} {Model} in {Color}."); | |
} | |
} | |
class Program | |
{ | |
static void Main() | |
{ | |
// Creating an object of Car | |
Car myCar = new Car("Red", "Toyota Camry", 2021); | |
// Accessing properties | |
Console.WriteLine(myCar.Model); // Outputs: Toyota Camry | |
// Calling methods | |
myCar.Start(); // Outputs: The Toyota Camry is starting. | |
myCar.DisplayInfo(); // Outputs: A 2021 Toyota Camry in Red. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment