C# ToString method 💭

preview_player
Показать описание
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)
{
}
public override string ToString()
{
return "This is a " + make + " " + model;
}
}
}
Рекомендации по теме
Комментарии
Автор

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

BroCodez
Автор

bro your videos are an amazing help, i would and think other would also appreciate data structure and algorithms in c#

lukhanyokalashe
Автор

ToString() method is defined in base class which is System.Object, System.Object is the ultimate base class for all C# classes.

neelkamal
Автор

I never really understood the ToString method until now!

cabbiechuck
Автор

super simple and understandable! thx a ton :)

Cookiebro
Автор

So what exactly is the point of using the ToString() method? like when would I ever use it?

the_dude_josh
Автор

So there is NO auto generated toStrig() method (with ALL fields), like in JAVA?

andrasfejes
Автор

but we didnt create virtual tostring wtf

xoomzera