有没有办法在C#数组中更改元组值? [英] Is there a way to change tuple values inside of a C# array?

查看:494
本文介绍了有没有办法在C#数组中更改元组值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用的数组是 int [,,] ,我想用一种方法将每个(我认为是)元组的第一个值设为那么我想多次修改后两个值。 (用另一种方法)

The array I'm using is int[,,] and I want to make the first value of each (what I assume to be) tuple in one method and then I want to modify the second two values several times. (in another method)

例如:

int[,,] MyArrayGet()
{    
    int [,,] myArray;
    int [,,] myArray = new int[9,9,9];
    for (int i = 0; i < 10; i++)
    {
        myArray[i] = [SomeInt,0,0];
    }
    return myArray;
}
int[,,] MyArrayModify(int[,,] myArray)
{   
    for (int i = 0; i < 10; i++)
    {
        if (somthing is true) 
        {
            myArray[i] = [dont change this value,n+1,dont change this value]
        }
        if (somthingelse is true)
        {
            my  Array[i] = [dont change this value,dont change this value,n+1]
    }
}

是否有快速简便的方法?

Is there a quick and easy way to do this?

我已经检查过这个问题如何从中获取字符串数组List< Tuple< int,int,string>>>> ;? ,但是我都不认为它可以回答我的问题。

I have checked this question How to get array of string from List<Tuple<int, int, string>>? however either I do not feel it answers my question.

推荐答案

元组在设计上是不可变的。

Tuple by design is immutable. Just create new one when modifying.

IEnumerable<Tuple<int, int, int>> Get()
{
    for (int i = 0; i < 10; i++)
    {
        yield return Tuple.Create(i, 0, 0);
    }
}

IEnumerable<Tuple<int, int, int>> Modify(IEnumerable<Tuple<int, int, int>> tuples)
{
    foreach (var tuple in tuples)
    {
        if (tuple.Item1 < 5)
        {
            yield return Tuple.Create(tuple.Item1, tuple.Item2 + 1, tuple.Item3);
        }
        else
        {
            yield return Tuple.Create(tuple.Item1, tuple.Item2, tuple.Item3 + 1);
        }
    }
}






您的代码比C#更像javascript。 int [,,] 是3D数组,而不是3个项目的数组。


Your code looks like javascript more than C#. int[,,] is 3D array not array of 3 items.

Tuple< int,int,int> [] 是3个项目的 Tuple 的数组。如果您不是初学者并且使用LINQ,则 IEnumerable< Tuple< int,int,int>> 是更好的版本。

Tuple<int, int, int>[] is an array of Tuple of 3 items. If you are not a beginner and use LINQ, IEnumerable<Tuple<int, int, int>> is a better version.

这篇关于有没有办法在C#数组中更改元组值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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