对象泛型列表中包含短返回false [英] generic list of objects contains short returning false

查看:110
本文介绍了对象泛型列表中包含短返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个项目,我要检查型短对象的一个​​泛型列表中的一些值。奇怪的是我注意到,它总是返回false即使是在对象的泛型列表的值。我提供一小块code能够复制这种情况在我的项目。

 名单,其中,对象>链表=新的名单,其中,对象>();
objectList.Add(1);
objectList.Add(2);
objectList.Add(3);
如果(objectList.Contains(1))
{
    总之我= 1;
    如果(objectList.Contains(i))的
    {
    }
    否则,如果(objectList.Contains(i.ToString()))
    {
    }
    其他
    {
        //进入这个东西只是这个循环
    }
}
 

我的假设是因为在这些类型也可能会返回false大小的差异。任何其他的想法。

感谢。

解决方案

  objectList.Add(1);
 

是相同的

  INT I = 1;
objectList.Add(ⅰ);
 

于是

  INT Y = 1;
objectList.Contains(Y); // 真正

短Z = 1;
objectList.Contains(Z); // 假
 

I am working on a project and i have to check for some values of type short in a generic list of object. Strangely I noticed that it is always returning false even if there is that value in the generic list of objects. I am providing a small piece of code which replicates that scenario in my project.

List<object> objectList = new List<object>();
objectList.Add(1);
objectList.Add(2);
objectList.Add(3);
if (objectList.Contains(1))
{
    short i = 1;
    if (objectList.Contains(i))
    {
    }
    else if (objectList.Contains(i.ToString()))
    {
    }
    else
    {
        //Entering this else this loop only
    }
}

My assumption is because of the difference in the size for those types it may be returning false. Any other thoughts.

Thanks.

解决方案

objectList.Add(1);

is the same as

int i = 1;
objectList.Add(i);

So

int y = 1;
objectList.Contains(y); // true

short z = 1;
objectList.Contains(z); // false

这篇关于对象泛型列表中包含短返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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