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

範例

下列範例會判斷指定的 ConstraintConstraintCollection 是否存在於刪除之前。

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 移除 。

適用於

另請參閱