简体   繁体   English

如何检查列表中是否已存在某项?

[英]How do i check if an item is already exist in a List?

if (this.Handle != hwnd && (Constants.GetWindowLongA(hwnd, Constants.GWL_STYLE) & Constants.TARGETWINDOW) == Constants.TARGETWINDOW)
{
    StringBuilder sb = new StringBuilder(100);
    Constants.GetWindowText(hwnd, sb, sb.Capacity);

    Window t = new Window();
    t.Handle = hwnd;
    t.Title = sb.ToString();
    windows.Add(t);
}

I want to check that if the item variable (t) is already in the List windows don't add it again. 我要检查列表窗口中是否已存在项目变量(t),请不要再添加它。 Just after the line t.Title = sb.ToString(); t.Title = sb.ToString(); to check if it's not exist then add it. 检查它是否不存在,然后添加它。

if(!windows.Any(window=>window.Handle == t.Handle)){
    windows.Add(t);
}

Or, better yet, check at the beginning of the method (although I admit I don't know what it's doing.) 或者,更好的是,检查方法的开头(尽管我承认我不知道它在做什么)。

if(windows.Any(window=>window.Handle == hwnd)) return;

Did you try the Contains method? 您是否尝试了Contains方法?

if (!windows.Contains(t)) { windows.Add(t) }

That might work. 那可能行得通。

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

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