ConstraintCollection.Contains(String) 方法

定义

指示集合中是否存在按名称指定的 Constraint 对象。

public:
 bool Contains(System::String ^ name);
public bool Contains (string? name);
public bool Contains (string name);
member this.Contains : string -> bool
Public Function Contains (name As String) As Boolean

参数

name
String

约束的 ConstraintName

返回

如果集合包含指定的约束,则为 true;否则为 false

示例

以下示例确定在删除之前,ConstraintCollection指定的 Constraint 是否存在于 中。

public static void RemoveConstraint(
    ConstraintCollection constraints, Constraint constraint)
{
    try
    {
        if(constraints.Contains(constraint.ConstraintName))
        {
            if(constraints.CanRemove(constraint))
            {
                constraints.Remove(constraint.ConstraintName);
            }
        }
    }
    catch(Exception e)
    {
        // Process exception and return.
        Console.WriteLine("Exception of type {0} occurred.",
            e.GetType());
    }
}
Public Shared Sub RemoveConstraint( _
    constraints As ConstraintCollection, constraint As Constraint)
    Try
        If constraints.Contains(constraint.ConstraintName) Then
            If constraints.CanRemove(constraint)
                constraints.Remove(constraint.ConstraintName)
            End If
        End If

    Catch e As Exception
        ' Process exception and return.
        Console.WriteLine($"Exception of type {e.GetType()} occurred.")
    End Try
 End Sub

注解

Contains在尝试将其从集合中删除之前,使用 方法确定指定的 Constraint 是否存在。 还可以使用 CanRemove 方法确定是否 Constraint 可以删除 。

适用于

另请参阅