如何替换同一位置的集合列表中的值? [英] How do I replace a value in a collection list at the same location?

查看:62
本文介绍了如何替换同一位置的集合列表中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

List<Animals> alumnos = new List<Animals>();







 class Animals{ 
int Idanimals  { get; set; }
int Type { get; set; }
}





0 =猫

1 =狗

2 =小鸟





0 = cat
1 = dog
2 = bird

animals[2] = "Dog";





然后将它们上传到DVgrid

帮帮我!!!!



我有什么试过:





Then upload them to a DVgrid
Help me!!!!

What I have tried:

animals[2] = "Dog";





但我收到错误

CS0029 C#无法将类型'string'隐式转换为'Project.Animals'



But I get error
CS0029 C # Can not implicitly convert type 'string' to 'Project.Animals'

推荐答案

animals[2] = "Dog";



但是(根据编译器)你定义了 animals 作为 Animals 的集合,而不是字符串的集合。所以你需要创建一个新的Animal并将其分配给你的列表,例如:


But (according to the compiler) you defined animals as a collection of Animals, not a collection of strings. So you need to create a new Animal and assign that to your list, like:

Animals newDog = new Animals(1, "Dog");
animals[2] = newDog;


这篇关于如何替换同一位置的集合列表中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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