如何获得清单的价值 [英] How get value of List

查看:65
本文介绍了如何获得清单的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

List<object> items= new List<object>();
List.Add(...);


..........


..........

var s = items[0]; // not working



来自OP:
无法将源类型对象"转换为目标类型对象[]



From OP:
cannot convert source type ''object'' to target type object[]

推荐答案

List<object> items= new List<object>();
items.Add(1);
items.Add(2);
items.Add(3);

var s = items[0];



以上对我有用.检查您的语法.



The above works for me. Check your syntax.


在这个词的所有意义上,它当然是有效的.在发布代码时,只需尝试自己的代码即可,别无其他.但是,为什么要对通用集合使用类型object?使用一些更具体的类型很有意义.

—SA
Of course it''s working, in all senses of this word. Just try your own code, as you post it and nothing else. But why using the type object with generic collection? It makes sense to use some more concrete type.

—SA


认为您的错误是将List与item混淆.您将新变量添加到列表中,而不是item中.所以是的是正确的,因为您的语法是错误的...

您应该注意一下,您创建的新列表被命名为"item"而不是"List"
Think your mistake is confusing List with item.... You adds the new variables to the list and not the item... SO yes they are right in that your syntax is wrong...

You should just take a note that the new list you created is named "item" an not "List"


这篇关于如何获得清单的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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