名单C#访问对象 [英] access objects in list c#

查看:108
本文介绍了名单C#访问对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

中的代码是:

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

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

现在我想一开始我试图访问mList
的对象:

now I want to access the objects in mList at first I tried:

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



但失败了。监守后,每个我写m1.first()。一个控制台上。这是它的初始化不是值5。

but it failed. becuase after the for each I wrote m1.first().a on console. it was it's initialized value not 5.

然后我试图

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



再次同样的问题。

again the same problem.

然后我试图

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



它甚至没有编译。它给了我一个错误。它说,一些关于不被list.this [INT]中修改的返回值。

it even didn't compiled. it gives me an error. it says something about not being modifiable return value of list.this[int].

然后我试图

    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.

推荐答案

的问题是,你已经把一个的结构在列表中。当您从列表中,这将创建一个副本获取它 - 这样修改它不会做任何好处。鉴于您的意见,这是可能的,你是混淆了值类型和引用类型是如何在C#中的工作,在这种情况下,我建议你阅读我的文章题目(或书籍,或MSDN等)。

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).

您就需要取水的时候,修改副本,然后更换在列表中的值:

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;



不过,我会强烈建议不要在第一时间创建可变的值类型,一方面是因为它们会导致完全相同这类问题。值类型通常应被用于不能到位而改变更多的根本的值。它没有意义的改变是什么5号的意思,例如,或上午10点1月1日。如果你改变的意思,你已经有了一个新的价值 - 所以强制,要真正的的新值

目前还不清楚(因缺乏上下文),你是否应该创建一个不可变的值类型,在列表中替换它,或创建一个可变引用类型(类)来代替。 ,或者甚至可能是一个不可变的引用类型,因为这往往能提高可读性

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.

我也建议不要使用公共领域 - 的使用属性,而不是,以实现细节从类型的API中分离出来。

I'd also advise against using public fields - use properties instead, to separate implementation details from the type's API.

这篇关于名单C#访问对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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