【问题标题】:how to search a string in structure variable ( C# )如何在结构变量中搜索字符串(C#)
【发布时间】:2010-05-04 08:14:21
【问题描述】:
 public struct Items
{
    public string Id;
    public string Name;

}

   public Items[] _items = null;

if (_items.Contains("Table"))
                {
                    // i need to check the structure and need to return correponding id
                }

并且我的结构变量中有一个变量列表...
我需要在结构中搜索一个名称(_items),我想返回对应的 ID。

如何做到这一点,

【问题讨论】:

    标签: struct


    【解决方案1】:
    foreach (Items item in _items)
    {
        if (item.Name.Contains("search string"))
        {
            return item.Id;
        }
    }
    

    【讨论】:

    • 在这一行 if(item.Name.Contains("search string")) ,它不会进入循环,它会抛出错误,同时检查条件
    • @deep: 粘贴你的源代码,我可能认为你的收藏没有声明或者你做错了。
    • 感谢 Sivss,通过以下代码获得答案。由于集合为空而导致的错误。解决了,谢谢 Ans: for (int i = 0; i
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-30
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    • 2014-03-09
    • 2017-12-16
    相关资源
    最近更新 更多