C# toString() method tutorial example explained
#C# #ToString #method
using System;
namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
//ToString() = converts an object to its string representation so that it is suitable for display
Car car = new Car("Chevy", "Corvette", 2022, "blue");
Console.WriteLine(car.ToString());
Console.ReadKey();
}
}
class Car
{
String make;
String model;
int year;
String color;
public Car(String make, String model, int year, String color)
{
this.make = make;
this.model = model;
this.year = year;
this.color = color;
}
public override string ToString()
{
return "This is a " + make + " " + model;
}
}
}
Continue this lesson in the app
Install CourseHive on Android or iOS to keep learning while you move.
FAQs
We curate free YouTube courses, organized by category and language โ and make it easy for you to track your learning progress.
Yes. Free courses are embedded YouTube videos.
Currently available in English โ with Portuguese, Spanish, French, German, and Italian coming soon.
Check ratings, reviews, duration, and description.
All courses include a certificate of participation.
Yes, an account lets you track progress, earn certificates, and save favorites.
We auto-save your watch position so you can resume on any device.