如何重载Tostring方法以接受3个参数! [英] how to overload Tostring method to take 3 arguments!

查看:75
本文介绍了如何重载Tostring方法以接受3个参数!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下提到的代码是一维数组代码

below mentioned code is of a single dimensional array code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace array_demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //int[] arr = new int []{ 1, 2, 3, 4 };
            //int[] arr = { 1, 2, 3, 4 };
            int[] arr = new int[3];
            arr[0] = (5);
            arr[1]=arr[0]*2;
            arr[2]=arr[1]*2;

            textBox1.Text = Convert.ToString(arr[0],arr[1],arr[2]);
            
        }
    }
}



现在,如果我只给出一个arr值,即键入

textbox1.text = convert.tostring(arr [0]);

上面的石灰在文本框中显示为5.现在,如果我尝试为ToString再添加2个参数,则显然会引发错误方法"ToString"的无重载需要3个参数.

现在建议重载Tostring以容纳n个参数或任何特定方法(保留lop等).



now here if i am giving only a single arr value i.e. if type

textbox1.text=convert.tostring(arr[0]);

the above lime displays me an out put as 5 in textbox. now if i try to put in 2 more arguments for ToString it obviously throws an error '' No overload for method "ToString" takes 3 arguments.

Now is it advisable to overload Tostring to accomidate n number of arguments or any specific method(leaving for lop etc)

推荐答案

,您可以连接数组值,然后进行转换,否则,tostring仅接受一个参数,并且不能更改..

你可以做这样的事情

textbox1.text = convert.tostring(arr [0] + arr [1] + arr [2]);

希望这会有所帮助..
祝您度过愉快的一天!
you can concatenate the array values and then convert, otherwise tostring accepts only one parameter and that cannot be altered..

you can do something like this

textbox1.text=convert.tostring(arr[0] + arr[1] + arr[2]);

hope this helps..
have a grt day!!


我建​​议您使用一种新方法,如下所示:

I suggest go for a new method something like this:

public string toString(int[] x)
    {
        string r = "";
        foreach (int i in x)
        {
            r = r + "," + i.ToString();
        }
        return r;
    }




修改




modify

r = r + "," + i.ToString();

根据您的输出

我不建议超载,因为这对于您执行小任务来说是很长的路.

希望对您有帮助.

according to ur output

I don''t suggest overload because this is very long way for u to do a small task.

I hope this will help u.


这篇关于如何重载Tostring方法以接受3个参数!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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