Best way to remove items from a collection

前端 未结 15 2023
天命终不由人
天命终不由人 2020-12-08 05:47

What is the best way to approach removing items from a collection in C#, once the item is known, but not it\'s index. This is one way to do it, but it seems inelegant at be

15条回答
  •  [愿得一人]
    2020-12-08 06:42

    If you want to access members of the collection by one of their properties, you might consider using a Dictionary or KeyedCollection instead. This way you don't have to search for the item you're looking for.

    Otherwise, you could at least do this:

    foreach (SPRoleAssignment spAssignment in workspace.RoleAssignments)
    {
        if (spAssignment.Member.Name == shortName)
        {
            workspace.RoleAssignments.Remove(spAssignment);
            break;
        }
    }
    

提交回复
热议问题