C# 如何把数组中的数据转换为一个整体数值?

比如arr[0]=1,arr[1]=2,arr[2]=3,我想转换成数字123,怎么写?
2025-04-08 01:27:36
推荐回答(2个)
回答1:

public sealed class ArrayDigits {
        public Int32[] Value { get; private set; }
        public ArrayDigits(Int32[] value){
            this.Value = value;
        }
        public static implicit operator Int32(ArrayDigits ad) {
            String strValue = String.Empty;
            Int32 result=0;
            foreach (var item in ad.Value)
            {
                strValue += item;
            }

            checked
            {
                Int32.TryParse(strValue, out result);
            }

            return result;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            ArrayDigits ad = new ArrayDigits(new Int32[] { 1, 2, 3});
            int result = ad;

            Console.WriteLine("result="+result);

            int sum = result + 1;
            Console.WriteLine("sum="+sum);

            Console.ReadKey(true);
        }
    }

回答2:

int.Parse(string.Join("", arr));