Skip to content

Instantly share code, notes, and snippets.

@Retrockit
Created May 4, 2024 12:03
Show Gist options
  • Save Retrockit/1757719a31fd6433e52ad6a98dc57c3d to your computer and use it in GitHub Desktop.
Save Retrockit/1757719a31fd6433e52ad6a98dc57c3d to your computer and use it in GitHub Desktop.
Car Class Example
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