Okay, so I wrote this program out of the exercise of a C# programming book (I'm trying to learn here) and it asks for "Override the ToString() method to return all data members".
Have I done this correctly? Or have I just successfully wrote code that compiles but does nothing. What is the purpose of ToString?
I have spent about 30 minutes looking at other posts on this and havn't figured it out, So I decided to make this.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication297 { class Program { static void Main(string[] args) { String name = "Stormtrooper"; Employee s = new Employee(name); Console.WriteLine("The type of hire is a {0}", s.Name); Console.WriteLine("The identification number is {0}", s.Number); Console.WriteLine("The date of hire is {0} ABY", s.Date); Console.WriteLine("The standard galactic salary is...{0:C}", s.Salary); } class Employee { private string _name; private string _number; private int _date; private int _salary; public string Name { get { return _name; } } public string Number { get { return _number; } } public int Date { get { return _date; } } public int Salary { get { return _salary; } } public Employee(string n) { _name = n; _number = "AA23TK421"; _date = 4; _salary = 800; } } public override string ToString() { return "_name + _number + _date + _salary".ToString(); } } } 47 Answers
You are returning a string that just says the phrase _name + _number + _date + _salary.
What you likely wanted to do is build a string using those fields. If you wanted them all mushed together Concat would work, but it would be highly un-readable
public override string ToString() { return String.Concat(_name, _number, _date, _salary); } However what would be better is to use Format and include labels with the values
public override string ToString() { return String.Format("Name:{0}, Number:{1}, Date:{2}, Salary:{3}",_name, _number, _date, _salary); } If you are using C# 6 or newer you can use the following cleaner format
public override string ToString() { return $"Name:{_name}, Number:{_number}, Date:{_date}, Salary:{_salary}"; } Which is the exact same logic as the previous String.Format version.
The reason people override the ToString() method is to have a default string representation of your object, usually for display to the user or in a log or console, like this:
Console.WriteLine(yourClassObject); If you do not override the ToString(), then its default implementation is to return the fully qualified name of your object, like this:
YourNamespace.YourClassName By changing the inherited implementation (from System.Object), then you can make a nicer (read: prettier) representation, like this:
public override string ToString() { return String.Format("This instance of my object has the following: Name = {0}, Number = {1}, Date = {2}, Salary = ${3}", _name, _number, _date, _salary); } Rather try something like
public override string ToString() { return String.Format("Name : {0}, number {1}, date {2}, salary {3}",_name,_number,_date,_salary); } But it neads to be part of the class
so
class Employee { private string _name; private string _number; private int _date; private int _salary; ..... public override string ToString() { return String.Format("Name : {0}, number {1}, date {2}, salary {3}",_name,_number,_date,_salary); } } Have a look at String.Format Method
2Replaces each format item in a specified string with the text equivalent of a corresponding object's value.
If you are using C# 6 (or later) use the nameof() method for the property names in the string in case the property names change. You can also use the $"" notation instead of using string.Format().
For example:
public override string ToString() { return $"{nameof(Name)}: {_name}"; } You could try to format the output in a nice format. (not tested, though)
public override string ToString() { return string.Format("Name: {0} Number: {1:n0} Date: {2:yyyy-MM-dd} Salary: {3:n2}", _name, _number, _date, _salary); } there are a lot of purposes overwriting .ToString(), depending on the context. for example,
- some developers like to have nicely formatted object description when doing debug, overwriting .ToString() would allow them to have meaningful description with some identifier (for example, the Id of a object);
- Some developers like to put some serialization code into the ToString() method;
- Some developers even put some debug code into the .ToString() method, though it might not be a good practice.
it really depending on the context of your needs. you may find some good practices to follow online - believe there are plenty of resources online.
Without overiding ToString, if you tried to "get" the string value of an Employee, e.g.
var employee1= new Employee(); Console.WriteLine(employee1);
What you'd get is:
ConsoleApplication1.Program+Employee
Which provides no information at all to help you (or a UI) display relevant information.
I use return _name + _number + _date + _salary; Which defaults to string,
or a more verbose
return "Name:" + _name + " Number:" + _number + " etc...";
class Program { static void Main( ) { int Number = 10; Console.WriteLine(Number.ToString()); Customer cc = new Customer(); cc.FirstName = "Rakibuz"; cc.LastName = "Sultan"; Console.WriteLine(Convert.ToString(cc)); } } public class Customer { public string FirstName; public string LastName; public override string ToString() { return FirstName + " " + LastName; } }