合并int数组C#的元素

时间:2014-08-21 03:17:34

标签: c# arrays merge elements

我一直在寻找MSDN和Stack溢出一段时间,但我似乎无法找到任何东西,我想合并一个int数组的元素。我看过string.join但似乎找不到int

的等价物
int[] myArray = new int[] {1,4};
int myArray[0] + myArray[1] would equal 14, Note that i want to merge them together
not mathematically add them.

整个计划:

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

namespace Exercise2
{

/** Create a reference type called Person.  Populate the Person class with the following properties to store the following information:
First name
Last name
Email address
Date of birth **/ 

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

    private void button1_Click(object sender, EventArgs e)
    {

        string DOB = tbDOB.Text;
        DOB.Replace("\\"," ");
        int[] iDOB = DOB.Select(n => Convert.ToInt32(n)).ToArray();
        int day = 

    }


}

}

将它更改为char数组会更好吗?

1 个答案:

答案 0 :(得分:0)

您可以使用string.Concat然后将结果解析为整数:

int result = int.Parse(string.Concat(myArray));

字符串也是不可变的,你需要重新分配DOB,你需要用string.Empty替换非数字字符而不是空格,否则你将获得FormatException

DOB = DOB.Replace("\\", "");