【C#】函数返回多个值/Tuple元组/ValueTuple值元组

    //可返回多个值,需要早函数外声明字段,通过函数修改字段值
    private void TestFunc1(ref string key, ref int value)
    {
        key = "key";
        value = 1;
    }
    private void TestFunc2(out string key, out int value)
    {
        key = "key";
        value = 1;
    }

    //只能返回两个值
    private KeyValuePair<string, int> TestFunc3()
    {
        return new KeyValuePair<string, int>("key", 1);
    }

    //可返回多个值,不过需要类型转换,不推荐
    private List<object> TestFunc4()
    {
        return new List<object>() { "key", 1 };
    }

    //返回自定义类
    public class MoreValue
    {
        public string key;
        public int value;
    }
    private MoreValue TestFunc5()
    {
        return new MoreValue() { key = "key", value = 1 };
    }

    //--------------------

    //Tuple元组: C# 4.0的新特性
    //是一种数据结构,引用类型,具有特定数量和元素序列
    //最多有八个元素,要想更多只能通过最后一个元素进行嵌套扩展,(使用值元组的嵌套和Rest属性实现)
    //访问元素通过ItemX去访问,使用前需要明确元素顺序,属性名字没有实际意义
    private System.Tuple<string, int> TestFunc6()
    {
        System.Tuple<string, int> tuple = new System.Tuple<string, int>("key", 1);
        Debug.Log(tuple.Item1 + tuple.Item2);
        return tuple;
    }

    //--------------------

    //ValueTuple值元组: C# 7.0的新特性
    //也是一种数据结构,是值类型,用于表示特定数量和元素序列
    //值元组元素是可变的,不是只读的,值元组的数据成员是字段不是属性
    private System.ValueTuple<string, int> TestFunc7()
    {
        System.ValueTuple<string, int> valueTuple = new System.ValueTuple<string, int>("key", 1);
        Debug.Log(valueTuple.Item1 + valueTuple.Item2);
        return valueTuple;
    }
    //ValueTuple值元组,返回值可以不明显指定ValueTuple,如(string, int)
    private (string, int) TestFunc8()
    {
        return ("key", 1);
    }
    //ValueTuple值元组,返回值可以指定元素名字,方便赋值和访问
    private (string key, int value) TestFunc9()
    {
        return (key: "key", value: 1);
    }

好的,以下是使用C#编写的二维装箱问题程序,注释已经加上: ```csharp using System; using System.Collections.Generic; using System.Linq; namespace BinPacking { class Program { static void Main(string[] args) { // 测试数据 int binWidth = 6; int binHeight = 4; List<Box> boxes = new List<Box>() { new Box(3, 2), new Box(1, 1), new Box(2, 4), new Box(1, 2), new Box(3, 1) }; // 调用装箱函数 var packedBoxes = PackBoxes(binWidth, binHeight, boxes); // 输出结果 Console.WriteLine($"Bin size: {binWidth} x {binHeight}"); Console.WriteLine($"Total boxes: {boxes.Count}"); Console.WriteLine($"Packed boxes: {packedBoxes.Count}"); Console.WriteLine("Box positions:"); foreach (var box in packedBoxes) { Console.WriteLine($"Box ({box.Width} x {box.Height}): ({box.X}, {box.Y})"); } } // 装箱函数 static List<Box> PackBoxes(int binWidth, int binHeight, List<Box> boxes) { // 对所有箱子按面积从大到小排序 boxes = boxes.OrderByDescending(b => b.Area).ToList(); // 初始化一个空白的矩形,代表装箱空间 Rectangle bin = new Rectangle(0, 0, binWidth, binHeight); // 初始化已放置箱子的列表 List<Box> packedBoxes = new List<Box>(); // 遍历每个箱子 foreach (var box in boxes) { // 尝试将箱子放置在空白矩形中 var packedBox = PackBoxInRectangle(box, bin); // 如果箱子放置成功,则将其加入已放置箱子列表中 if (packedBox != null) { packedBoxes.Add(packedBox); } } return packedBoxes; } // 在给定矩形中放置箱子 static Box PackBoxInRectangle(Box box, Rectangle rect) { // 如果箱子无法放入矩形,则返回空 if (box.Width > rect.Width || box.Height > rect.Height) { return null; } // 将箱子放置在矩形的左上角 Box packedBox = new Box(box.Width, box.Height); packedBox.X = rect.X; packedBox.Y = rect.Y; // 更新矩形,将已放置的箱子所占用的位置从矩形中删除 int newX = rect.X + box.Width; int newY = rect.Y + box.Height; int newWidth = rect.Width - box.Width; int newHeight = rect.Height - box.Height; // 如果剩余空间在水平方向上大于垂直方向,则将剩余空间分成两个矩形,一个在箱子的右侧,一个在箱子的下方 if (newWidth > newHeight) { Rectangle rightRect = new Rectangle(newX, rect.Y, newWidth, box.Height); Rectangle bottomRect = new Rectangle(rect.X, newY, rect.Width, newHeight); // 尝试先将箱子放在右侧矩形中,如果无法放置,则放在下方矩形中 Box rightPackedBox = PackBoxInRectangle(box, rightRect); if (rightPackedBox != null) { return packedBox; } else { Box bottomPackedBox = PackBoxInRectangle(box, bottomRect); if (bottomPackedBox != null) { return packedBox; } } } // 如果剩余空间在垂直方向上大于水平方向,则将剩余空间分成两个矩形,一个在箱子的下方,一个在箱子的右侧 else { Rectangle bottomRect = new Rectangle(rect.X, newY, rect.Width, newHeight); Rectangle rightRect = new Rectangle(newX, rect.Y, newWidth, rect.Height); // 尝试先将箱子放在下方矩形中,如果无法放置,则放在右侧矩形中 Box bottomPackedBox = PackBoxInRectangle(box, bottomRect); if (bottomPackedBox != null) { return packedBox; } else { Box rightPackedBox = PackBoxInRectangle(box, rightRect); if (rightPackedBox != null) { return packedBox; } } } // 如果无法放置箱子,则返回空 return null; } } // 表示一个矩形 class Rectangle { public int X { get; set; } public int Y { get; set; } public int Width { get; set; } public int Height { get; set; } public Rectangle(int x, int y, int width, int height) { X = x; Y = y; Width = width; Height = height; } } // 表示一个箱子 class Box { public int Width { get; set; } public int Height { get; set; } public int X { get; set; } public int Y { get; set; } public int Area { get { return Width * Height; } } public Box(int width, int height) { Width = width; Height = height; } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

萧然CS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值