ValueTuple用作参数传递

元组作为轻量语法定义的类型,用ValueTuple作为参数传递,可以用更简单的写法实现。

命名元组的声明:

 (string cellValue, int rowMergeNum, int colMergeNum) Title;

相比于未命名元组的Item,命名元组变量声明更为清晰。

例:

        public void ListToExcelSpecial(List<(string cellValue, int rowMergeNum, int colMergeNum)[]> Titles)
        {
            foreach (var tuples in Titles)
            {
                IRow row = sheet.CreateRow(count);
                int colInt = 0;
                foreach (var tuple in tuples)
                {
                    var columns = tuple.colMergeNum;
                    var cell = row.CreateCell(colInt);
                    cell.SetCellValue(tuple.cellValue);
                    cell.CellStyle = cellstyle;
                    sheet.AddMergedRegion(new CellRangeAddress(count, count + tuple.rowMergeNum - 1, colInt, colInt + tuple.colMergeNum - 1));
                    colInt += tuple.colMergeNum;
                }
                count++;
            }
        }
static void Main(string[] args)
        {
            List<(string, int, int)[]> lists = new List<(string, int, int)[]>();
            lists.Add(new(string, int, int)[] { ("XX年XX月", 1, 11) });
            lists.Add(new(string, int, int)[]
            {
                ("", 2, 1),
                ("", 1, 2),
                ("", 1, 3),
                ("", 1, 5)
            });
            new Program().ListToExcelSpecial(lists);
     }

写法简单明了,也不用再定义一个变量去传递参数。

你可能感兴趣的:(ValueTuple用作参数传递)