仍然无法理解.ToString()

Zor*_*oro 0 c#

问题是"覆盖ToString()方法以返回所有数据成员.

当我返回_name时,会给出我在当前上下文中不存在的错误.我不明白为了返回所有数据成员应该如何看待

sing 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 HOW DO I RETURN THIS???
        }
    }
Run Code Online (Sandbox Code Playgroud)

Jar*_*Par 13

覆盖ToString需要在Employee类型内部,而不是在它之外.

public class Employee {
  ...
  public override string ToString() { 
    return String.Format("{0} {1} {2} {3}", _name, _number, _date, _salary);
  }
}
Run Code Online (Sandbox Code Playgroud)

完成此操作后,任何调用都Employee::ToString将返回格式化的字符串而不是默认字符串