简体   繁体   English

访问列表C#中的对象

[英]access objects in list c#

The code is: 代码是:

struct m1
{
    public int a;
    public int b;
}

int main()
{
    List <m1> mList;
    m1.initialize;
    //new. and add some items to it.

now I want to access the objects in mList at first I tried: 现在,我首先尝试在mList中访问对象:

    for each(m1 it in mList)
    {
        m1.a = 5;
    }

but it failed. 但是失败了。 becuase after the for each I wrote m1.first().a on console. 因为每次我在控制台上写了m1.first()。a之后, it was it's initialized value not 5. 那是它的初始化值而不是5。

then I tried 然后我尝试

    for (int counter = 0; counter < mList.size(); counter++)
    {
        m1 it = mList[counter];
        it.a = 5;
    }

again the same problem. 同样的问题。

then I tried 然后我尝试

    for (int counter = 0; counter < mList.size(); counter++)
    {
        mList[counter].a = 5;
    }

it even didn't compiled. 它甚至没有编译。 it gives me an error. 这给了我一个错误。 it says something about not being modifiable return value of list.this[int]. 它说了一些关于list.this [int]不可修改的返回值。

then I tried 然后我尝试

    for (int counter = 0; counter < mList.size(); counter++)
    {
        var m1 it = mList[counter];
        it.a = 5;
    }

it didn't work too. 它也不起作用。 I tried all I could and everything that I found in internet and this site. 我尽了我所能,并尝试了在互联网和此站点中找到的所有内容。 Could you please help you to find a way to access parameters of objects(of type struct) in list? 您能否帮助您找到一种访问列表中对象(结构类型)参数的方法? Obviously it is easy when list is made from objects(from classes). 显然,从对象(从类)创建列表很容易。 it comes to complication if I want to made a list from objects of structs. 如果我想从结构对象中列出一个清单,就会很复杂。 Any help would highly welcomed. 任何帮助都将受到高度欢迎。

The problem is that you've put a struct in a list. 问题在于您已将结构放入列表中。 When you fetch it from the list, that will create a copy - so modifying it won't do any good. 当您从列表中获取它时,将创建一个副本-因此对其进行修改将无济于事。 Given your comments, it's possible that you're confused about how value types and reference types work in C#, in which case I suggest you read my article on the topic (or books, or MSDN etc). 根据您的评论,您可能对值类型和引用类型在C#中的工作方式感到困惑,在这种情况下,我建议您阅读有关该主题的文章 (或书籍,或MSDN等)。

You'd need to fetch it, modify the copy, then replace the value in the list: 您需要获取它,修改副本,然后替换列表中的值:

var copy = list[index];
copy.a = 5;
list[index] = copy;

However, I would strongly advise against creating mutable value types in the first place, partly because they cause exactly this sort of problem. 但是,我强烈建议不要一开始就创建可变的值类型,部分原因是它们确实引起了这种问题。 Value types should usually be used for more "fundamental" values which can't be changed in place. 值类型通常应用于无法就地更改的更多“基本”值。 It doesn't make sense to change what "the number 5" means, for example, or "January 1st at 10am". 例如,更改“数字5”的含义或“ 1月1日上午10点”的含义是没有意义的。 If you change the meaning, you've got a new value - so force that to genuinely be a new value. 如果您更改了含义,那么您将获得新的价值-因此请强制将其真正变为新的价值。

It's unclear (due to lack of context) whether you should be creating an immutable value type and replacing it in the list, or creating a mutable reference type (a class) instead. 目前尚不清楚(由于缺乏上下文)是应该创建一个不可变的值类型并在列表中替换它,还是创建一个可变的引用类型(一个类)。 Or possibly even an immutable reference type, as that can often aid readability. 甚至可能是不可变的引用类型,因为这通常可以提高可读性。

I'd also advise against using public fields - use properties instead , to separate implementation details from the type's API. 我还建议不要使用公共字段,而应使用属性来将实现细节与类型的API分开。

Try this: 尝试这个:

m1 temp ;
for (int counter = 0; counter < mList.size(); counter++)
    {
        temp = mList[counter];
        temp.a = 5;
        mList[counter] = temp
    }

if you are interesting to work with struct without using a temporary variable: use a struct collection: 如果您有兴趣在不使用临时变量的情况下使用struct,请使用struct集合:

           m1[] mList = new m1[100];
            //initialize 100 items

            for (int i = 0; i < 100; i++)
            {
                mList[i].a = 5;
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM