重写的ToString方法C# [英] Override .ToString method c#

查看:164
本文介绍了重写的ToString方法C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我写了这个程序了C#编程的书(我想在这里学习)的行使,并要求覆盖的ToString()方法返回的成员<所有数据/强>



让我正确地做到了这一点?还是我刚刚成功地写道:编译但不执行任何代码。是什么目的的ToString



我已经花了约30分钟,看着就这个问题和没有带其他职位想通了,所以我决定把这个

 使用系统; 
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;

命名空间ConsoleApplication297
{
类节目
{
静态无效的主要(字串[] args)
{
字符串名称=冲锋队;
的Employee =新员工(名);
Console.WriteLine(类型租金是{0},s.Name);
Console.WriteLine(识别号码是{0},s.Number);
Console.WriteLine(租用的日期是{0} ABY,s.Date);
Console.WriteLine(标准工资银河系是... {0:C},s.Salary);

}

类员工
{
私人字符串_name;
私人字符串_number;
私人诠释_date;
私人诠释_salary;
公共字符串名称
{
得到
{
返回_name;
}
}

公共串号
{
得到
{
返回_number;
}
}

公众诠释日期
{
得到
{
返回_date;
}
}
公众诠释工资
{
得到
{
返回_salary;
}
}
公务员(字符串n)
{
_name = N;
_number =AA23TK421;
_date = 4;
_salary = 800;
}
}

公共重写字符串的ToString()

{
返回_name + _number + _date + _salary的ToString( );
}
}
}


解决方案

您正在返回一个字符串,只是说这句话 _name + _number + _date + _salary



你可能想要做的是建立使用这些字段的字符串。如果你想他们都一起捣成泥的毗连会的工作,但它会非常不可读

 公共重写字符串的ToString()
{
返回String.Concat(_name,_number,_date,_salary );
}



不过,这将是更好的是使用的Format ,并包括与值的标签

 公共重写字符串的ToString()
{
返回的String.Format(名称:{0},号码:{1},日期:{2},薪水:{3},_名,_number,_date,_salary);
}


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();
    }
}
}

解决方案

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

这篇关于重写的ToString方法C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆