Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override .ToString method c#

Tags:

c#

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();     } } } 
like image 867
Zoro Avatar asked Aug 13 '13 03:08

Zoro


People also ask

What happen if we override toString () method?

Override the toString() method in a Java Class A string representation of an object can be obtained using the toString() method in Java. This method is overridden so that the object values can be returned.

What is the method signature declaration to override the toString () method?

toString() method must return a String . That's the only way to override Object 's toString() . If you wish to use the same name but not override Object 's toString , you can overload the name toString by adding arguments, thus changing the signature of your method.

What is toString method in C#?

ToString is the major formatting method in the . NET Framework. It converts an object to its string representation so that it is suitable for display.


Video Answer


1 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.

like image 75
Scott Chamberlain Avatar answered Oct 03 '22 11:10

Scott Chamberlain