Dieser Artikel wurde maschinell übersetzt. Wenn Sie die englische Version des Artikels anzeigen möchten, aktivieren Sie das Kontrollkästchen Englisch. Sie können den englischen Text auch in einem Popupfenster anzeigen, indem Sie den Mauszeiger über den Text bewegen.
Übersetzung
Englisch

Array.Exists<T>-Methode: (T[], Predicate<T>)

 

Veröffentlicht: Oktober 2016

Bestimmt, ob das angegebene Array Elemente enthält, die mit den vom angegebenen Prädikat definierten Bedingungen übereinstimmen.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public static bool Exists<T>(
	T[] array,
	Predicate<T> match
)

Parameter

array
Type: T[]

Das zu durchsuchende eindimensionale und nullbasierte Array.

match
Type: System.Predicate<T>

Das Predicate<T>, das die Bedingungen für die Elemente definiert, nach denen gesucht werden soll.

Rückgabewert

Type: System.Boolean

true, wenn array mindestens ein Element enthält, das die durch das angegebene Prädikat definierten Bedingungen erfüllt, andernfalls false.

Typparameter

T

Der Typ der Elemente des Arrays.

Exception Condition
ArgumentNullException

array ist null.

- oder -

match ist null.

The T:System.Predicate`1 is a delegate to a method that returns true if the object passed to it matches the conditions defined in the delegate. The elements of array are individually passed to the T:System.Predicate`1, and processing is stopped when a match is found.

System_CAPS_noteHinweis

In C# and Visual Basic, it is not necessary to create the T:System.Predicate`1 delegate explicitly. These languages infer the correct delegate from context and create it automatically.

This method is an O(n) operation, where n is the P:System.Array.Length of array.

The following example specifies the match conditions for the M:System.Array.Exists``1(``0[],System.Predicate{``0})method usinglambda expressions to check whether a planet starts with a given letter or whether the planet is found on the given array.

using System;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] planets = { "Mercury", "Venus",
                "Earth", "Mars", "Jupiter",
                "Saturn", "Uranus", "Neptune" };

            Console.WriteLine("One or more planets begin with 'M': {0}",
                Array.Exists(planets, element => element.StartsWith("M")));

            Console.WriteLine("One or more planets begin with 'T': {0}",
                Array.Exists(planets, element => element.StartsWith("T")));

            Console.WriteLine("Is Pluto one of the planets? {0}",
                Array.Exists(planets, element => element == "Pluto"));
        }
    }
}
// The example displays the following output:
//       One or more planets begin with 'M': True
//       One or more planets begin with 'T': False
//       Is Pluto one of the planets? False

The following example uses the M:System.Array.Exists``1(``0[],System.Predicate{``0}) method to indicate whether any names in a string array begin with a specified character. The example instantiates a StringSearcher object by passing the string to search for to its class constructor. The StringSearcher.StartsWith method has same signature as the T:System.Predicate`1 delegate. When theM:System.Array.Exists``1(``0[],System.Predicate{``0}) method is called, each member of the array is passed to the delegate until it returns true or iterates all the elements in the array.

using System;

public class Example
{
   public static void Main()
   {
      String[] names = { "Adam", "Adel", "Bridgette", "Carla",
                         "Charles", "Daniel", "Elaine", "Frances",
                         "George", "Gillian", "Henry", "Irving",
                         "James", "Janae", "Lawrence", "Miguel",
                         "Nicole", "Oliver", "Paula", "Robert",
                         "Stephen", "Thomas", "Vanessa",
                         "Veronica", "Wilberforce" };
      Char[] charsToFind = { 'A', 'K', 'W', 'Z' };

      foreach (var charToFind in charsToFind)
         Console.WriteLine("One or more names begin with '{0}': {1}",
                           charToFind,
                           Array.Exists(names, (new StringSearcher(charToFind)).StartsWith));
   }
}

public class StringSearcher
{
   Char firstChar;

   public StringSearcher(Char firstChar)
   {
      this.firstChar = Char.ToUpper(firstChar);
   }

   public bool StartsWith(String s)
   {
      if (String.IsNullOrEmpty(s)) return false;

      if(s.Substring(0, 1).ToUpper() == firstChar.ToString())
         return true;
      else
         return false;
   }
}
// The example displays the following output:
//       One or more names begin with 'A': True
//       One or more names begin with 'K': False
//       One or more names begin with 'W': True
//       One or more names begin with 'Z': False

You can also use a lambda expression rather than explicitly define a method whose signature corresponds to that of the delegate. The following example replaces the StringSearcherclass and its StartsWith method with a lambda expression.

using System;

public class Example
{
   public static void Main()
   {
      String[] names = { "Adam", "Adel", "Bridgette", "Carla",
                         "Charles", "Daniel", "Elaine", "Frances",
                         "George", "Gillian", "Henry", "Irving",
                         "James", "Janae", "Lawrence", "Miguel",
                         "Nicole", "Oliver", "Paula", "Robert",
                         "Stephen", "Thomas", "Vanessa",
                         "Veronica", "Wilberforce" };
      Char[] charsToFind = { 'A', 'K', 'W', 'Z' };

      foreach (var charToFind in charsToFind)
         Console.WriteLine("One or more names begin with '{0}': {1}",
                           charToFind,
                           Array.Exists(names,
                                        s => { if (String.IsNullOrEmpty(s))
                                                  return false;

                                               if (s.Substring(0, 1).ToUpper() == charToFind.ToString())
                                                  return true;
                                               else
                                                  return false;
                                             } ));
   }
}
// The example displays the following output:
//       One or more names begin with 'A': True
//       One or more names begin with 'K': False
//       One or more names begin with 'W': True
//       One or more names begin with 'Z': False

Universelle Windows-Plattform
Verfügbar seit 8
.NET Framework
Verfügbar seit 2.0
Portierbare Klassenbibliothek
Unterstützt in: portierbare .NET-Plattformen
Windows Phone Silverlight
Verfügbar seit 8.0
Windows Phone
Verfügbar seit 8.1
Zurück zum Anfang
Anzeigen: