InvalidOperationException Classe

Definizione

Eccezione generata quando una chiamata a un metodo non è valida per lo stato corrente dell'oggetto.

public ref class InvalidOperationException : Exception
public ref class InvalidOperationException : SystemException
public class InvalidOperationException : Exception
public class InvalidOperationException : SystemException
[System.Serializable]
public class InvalidOperationException : SystemException
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class InvalidOperationException : SystemException
type InvalidOperationException = class
    inherit Exception
type InvalidOperationException = class
    inherit SystemException
[<System.Serializable>]
type InvalidOperationException = class
    inherit SystemException
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type InvalidOperationException = class
    inherit SystemException
Public Class InvalidOperationException
Inherits Exception
Public Class InvalidOperationException
Inherits SystemException
Ereditarietà
InvalidOperationException
Ereditarietà
InvalidOperationException
Derivato
Attributi

Commenti

InvalidOperationException viene usato nei casi in cui l'errore di richiamare un metodo è causato da motivi diversi da argomenti non validi. In genere, viene generata quando lo stato di un oggetto non può supportare la chiamata al metodo. Ad esempio, viene generata un'eccezione InvalidOperationException da metodi come:

  • IEnumerator.MoveNext se gli oggetti di una raccolta vengono modificati dopo la creazione dell'enumeratore. Per altre informazioni, vedere Modifica di una raccolta durante l'iterazione.

  • ResourceSet.GetString se il set di risorse viene chiuso prima che venga eseguita la chiamata al metodo.

  • XContainer.Add, se l'oggetto o gli oggetti da aggiungere comportano un documento XML non strutturato in modo errato.

  • Metodo che tenta di modificare l'interfaccia utente da un thread che non è il thread principale o dell'interfaccia utente.

Importante

Poiché l'eccezione InvalidOperationException può essere generata in un'ampia gamma di circostanze, è importante leggere il messaggio di eccezione restituito dalla Message proprietà.

Contenuto della sezione:

Alcune cause comuni di eccezioni InvalidOperationException
Aggiornamento di un thread dell'interfaccia utente da un thread non dell'interfaccia utente
Modifica di una raccolta durante l'iterazione
Ordinamento di una matrice o di una raccolta i cui oggetti non possono essere confrontati
Cast di un valore T> nullable<per il tipo sottostante
Chiamata di un metodo System.Linq.Enumerable in una raccolta vuota
Chiamata a Enumerable.Single o Enumerable.SingleOrDefault in una sequenza senza un elemento
Accesso di dominio cross-application dinamico
Generazione di un'eccezione InvalidOperationException
Informazioni varie

Alcune cause comuni di eccezioni InvalidOperationException

Nelle sezioni seguenti viene illustrato come vengono generati alcuni casi comuni in cui viene InvalidOperationException generata un'eccezione in un'app. La gestione del problema dipende dalla situazione specifica. In genere, tuttavia, l'eccezione risulta dall'errore dello sviluppatore e l'eccezione InvalidOperationException può essere prevista ed evitata.

Aggiornamento di un thread dell'interfaccia utente da un thread non dell'interfaccia utente

Spesso, i thread di lavoro vengono usati per eseguire alcune operazioni in background che comportano la raccolta di dati da visualizzare nell'interfaccia utente di un'applicazione. Tuttavia, la maggior parte dei framework dell'applicazione GUI (interfaccia utente grafica) per .NET, ad esempio Windows Forms e Windows Presentation Foundation (WPF), consente di accedere solo agli oggetti GUI dal thread che crea e gestisce l'interfaccia utente (thread principale o dell'interfaccia utente). Viene InvalidOperationException generato quando si tenta di accedere a un elemento dell'interfaccia utente da un thread diverso dal thread dell'interfaccia utente. Il testo del messaggio di eccezione viene visualizzato nella tabella seguente.

Tipo di applicazione Messaggio
App WPF Il thread chiamante non può accedere a questo oggetto perché un thread diverso lo possiede.
App UWP L'applicazione ha chiamato un'interfaccia che è stata eseguito il marshalling per un thread diverso.
app Windows Forms Operazione tra thread non valida: il controllo 'TextBox1' a cui è stato eseguito l'accesso da un thread diverso dal thread creato.

I framework dell'interfaccia utente per .NET implementano un modello dispatcher che include un metodo per verificare se una chiamata a un membro di un elemento dell'interfaccia utente viene eseguita nel thread dell'interfaccia utente e altri metodi per pianificare la chiamata al thread dell'interfaccia utente:

  • Nelle app WPF chiamare il metodo per determinare se un metodo è in esecuzione in un thread non dell'interfaccia Dispatcher.CheckAccess utente. Restituisce true se il metodo è in esecuzione nel thread dell'interfaccia utente e false in caso contrario. Chiamare uno degli overload del Dispatcher.Invoke metodo per pianificare la chiamata al thread dell'interfaccia utente.

  • Nelle app UWP controllare la proprietà per determinare se un metodo è in esecuzione in un thread non dell'interfaccia CoreDispatcher.HasThreadAccess utente. Chiamare il metodo per eseguire un delegato che aggiorna il CoreDispatcher.RunAsync thread dell'interfaccia utente.

  • Nelle app Windows Forms usare la proprietà per determinare se un metodo è in esecuzione in un thread non dell'interfaccia Control.InvokeRequired utente. Chiamare uno degli overload del Control.Invoke metodo per eseguire un delegato che aggiorna il thread dell'interfaccia utente.

Gli esempi seguenti illustrano l'eccezione generata quando si tenta di aggiornare un elemento dell'interfaccia InvalidOperationException utente da un thread diverso dal thread che lo ha creato. Ogni esempio richiede la creazione di due controlli:

  • Controllo casella di testo denominato textBox1. In un'app Windows Forms è necessario impostare la relativa Multiline proprietà su true.

  • Controllo pulsante denominato threadExampleBtn. Nell'esempio viene fornito un gestore, ThreadsExampleBtn_Click, per l'evento del Click pulsante.

In ogni caso, il threadExampleBtn_Click gestore eventi chiama il DoSomeWork metodo due volte. La prima chiamata viene eseguita in modo sincrono e ha esito positivo. Ma la seconda chiamata, perché viene eseguita in modo asincrono in un thread del pool di thread, tenta di aggiornare l'interfaccia utente da un thread non dell'interfaccia utente. Ciò comporta un'eccezione InvalidOperationException .

App WPF e UWP

private async void threadExampleBtn_Click(object sender, RoutedEventArgs e)
{
    textBox1.Text = String.Empty;

    textBox1.Text = "Simulating work on UI thread.\n";
    DoSomeWork(20);
    textBox1.Text += "Work completed...\n";

    textBox1.Text += "Simulating work on non-UI thread.\n";
    await Task.Run(() => DoSomeWork(1000));
    textBox1.Text += "Work completed...\n";
}

private async void DoSomeWork(int milliseconds)
{
    // Simulate work.
    await Task.Delay(milliseconds);

    // Report completion.
    var msg = String.Format("Some work completed in {0} ms.\n", milliseconds);
    textBox1.Text += msg;
}
Private Async Sub threadExampleBtn_Click(sender As Object, e As RoutedEventArgs) Handles threadExampleBtn.Click
    textBox1.Text = String.Empty

    textBox1.Text = "Simulating work on UI thread." + vbCrLf
    DoSomeWork(20)
    textBox1.Text += "Work completed..." + vbCrLf

    textBox1.Text += "Simulating work on non-UI thread." + vbCrLf
    Await Task.Factory.StartNew(Sub()
                                    DoSomeWork(1000)
                                End Sub)
    textBox1.Text += "Work completed..." + vbCrLf
End Sub

Private Async Sub DoSomeWork(milliseconds As Integer)
    ' Simulate work.
    Await Task.Delay(milliseconds)

    ' Report completion.
    Dim msg = String.Format("Some work completed in {0} ms.", milliseconds) + vbCrLf
    textBox1.Text += msg
End Sub

La versione seguente del DoSomeWork metodo elimina l'eccezione in un'app WPF.

private async void DoSomeWork(int milliseconds)
{
    // Simulate work.
    await Task.Delay(milliseconds);

    // Report completion.
    bool uiAccess = textBox1.Dispatcher.CheckAccess();
    String msg = String.Format("Some work completed in {0} ms. on {1}UI thread\n",
                               milliseconds, uiAccess ? String.Empty : "non-");
    if (uiAccess)
        textBox1.Text += msg;
    else
        textBox1.Dispatcher.Invoke(() => { textBox1.Text += msg; });
}
Private Async Sub DoSomeWork(milliseconds As Integer)
    ' Simulate work.
    Await Task.Delay(milliseconds)

    ' Report completion.
    Dim uiAccess As Boolean = textBox1.Dispatcher.CheckAccess()
    Dim msg As String = String.Format("Some work completed in {0} ms. on {1}UI thread",
                                      milliseconds, If(uiAccess, String.Empty, "non-")) + 
                                      vbCrLf 
    If uiAccess Then
        textBox1.Text += msg
    Else
        textBox1.Dispatcher.Invoke( Sub() textBox1.Text += msg)
    End If
End Sub

La versione seguente del DoSomeWork metodo elimina l'eccezione in un'app UWP.

private async void DoSomeWork(int milliseconds)
{
    // Simulate work.
    await Task.Delay(milliseconds);

    // Report completion.
    bool uiAccess = textBox1.Dispatcher.HasThreadAccess;
    String msg = String.Format("Some work completed in {0} ms. on {1}UI thread\n",
                               milliseconds, uiAccess ? String.Empty : "non-");
    if (uiAccess)
        textBox1.Text += msg;
    else
        await textBox1.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { textBox1.Text += msg; });
}
Private Async Sub DoSomeWork(milliseconds As Integer)
    ' Simulate work.
    Await Task.Delay(milliseconds)

    ' Report completion.
    Dim uiAccess As Boolean = textBox1.Dispatcher.HasThreadAccess
    Dim msg As String = String.Format("Some work completed in {0} ms. on {1}UI thread" + vbCrLf,
                                      milliseconds, If(uiAccess, String.Empty, "non-"))
    If (uiAccess) Then
        textBox1.Text += msg
    Else
        Await textBox1.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, Sub() textBox1.Text += msg)
    End If
End Sub

App di Windows Form

List<String> lines = new List<String>();

private async void threadExampleBtn_Click(object sender, EventArgs e)
{
    textBox1.Text = String.Empty;
    lines.Clear();

    lines.Add("Simulating work on UI thread.");
    textBox1.Lines = lines.ToArray();
    DoSomeWork(20);

    lines.Add("Simulating work on non-UI thread.");
    textBox1.Lines = lines.ToArray();
    await Task.Run(() => DoSomeWork(1000));

    lines.Add("ThreadsExampleBtn_Click completes. ");
    textBox1.Lines = lines.ToArray();
}

private async void DoSomeWork(int milliseconds)
{
    // simulate work
    await Task.Delay(milliseconds);

    // report completion
    lines.Add(String.Format("Some work completed in {0} ms on UI thread.", milliseconds));
    textBox1.Lines = lines.ToArray();
}
Dim lines As New List(Of String)()
Private Async Sub threadExampleBtn_Click(sender As Object, e As EventArgs) Handles threadExampleBtn.Click
    textBox1.Text = String.Empty
    lines.Clear()

    lines.Add("Simulating work on UI thread.")
    textBox1.Lines = lines.ToArray()
    DoSomeWork(20)

    lines.Add("Simulating work on non-UI thread.")
    textBox1.Lines = lines.ToArray()
    Await Task.Run(Sub() DoSomeWork(1000))

    lines.Add("ThreadsExampleBtn_Click completes. ")
    textBox1.Lines = lines.ToArray()
End Sub

Private Async Sub DoSomeWork(milliseconds As Integer)
    ' Simulate work.
    Await Task.Delay(milliseconds)

    ' Report completion.
    lines.Add(String.Format("Some work completed in {0} ms on UI thread.", milliseconds))
    textBox1.Lines = lines.ToArray()
End Sub

La versione seguente del DoSomeWork metodo elimina l'eccezione in un'app Windows Forms.

private async void DoSomeWork(int milliseconds)
{
    // simulate work
    await Task.Delay(milliseconds);

    // Report completion.
    bool uiMarshal = textBox1.InvokeRequired;
    String msg = String.Format("Some work completed in {0} ms. on {1}UI thread\n",
                               milliseconds, uiMarshal ? String.Empty : "non-");
    lines.Add(msg);

    if (uiMarshal) {
        textBox1.Invoke(new Action(() => { textBox1.Lines = lines.ToArray(); }));
    }
    else {
        textBox1.Lines = lines.ToArray();
    }
}
Private Async Sub DoSomeWork(milliseconds As Integer)
    ' Simulate work.
    Await Task.Delay(milliseconds)

    ' Report completion.
    Dim uiMarshal As Boolean = textBox1.InvokeRequired
    Dim msg As String = String.Format("Some work completed in {0} ms. on {1}UI thread" + vbCrLf,
                                      milliseconds, If(uiMarshal, String.Empty, "non-"))
    lines.Add(msg)

    If uiMarshal Then
        textBox1.Invoke(New Action(Sub() textBox1.Lines = lines.ToArray()))
    Else
        textBox1.Lines = lines.ToArray()
    End If
End Sub

Modifica di una raccolta durante l'iterazione

L'istruzione foreach in C#, for...in in F#o For Each nell'istruzione in Visual Basic viene usata per scorrere i membri di una raccolta e per leggere o modificare i singoli elementi. Tuttavia, non può essere usato per aggiungere o rimuovere elementi dalla raccolta. In questo modo viene generata un'eccezione con un InvalidOperationException messaggio simile a "Raccolta modificata; L'operazione di enumerazione potrebbe non essere eseguita.

Nell'esempio seguente viene eseguito l'iterazione di una raccolta di numeri interi che tenta di aggiungere il quadrato di ogni intero alla raccolta. Nell'esempio viene generato un InvalidOperationException oggetto con la prima chiamata al List<T>.Add metodo .

using System;
using System.Collections.Generic;

public class Example
{
   public static void Main()
   {
      var numbers = new List<int>() { 1, 2, 3, 4, 5 };
      foreach (var number in numbers) {
         int square = (int) Math.Pow(number, 2);
         Console.WriteLine("{0}^{1}", number, square);
         Console.WriteLine("Adding {0} to the collection...\n", square);
         numbers.Add(square);
      }
   }
}
// The example displays the following output:
//    1^1
//    Adding 1 to the collection...
//
//
//    Unhandled Exception: System.InvalidOperationException: Collection was modified;
//       enumeration operation may not execute.
//       at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
//       at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
//       at Example.Main()
open System

let numbers = ResizeArray [| 1; 2; 3; 4; 5 |]
for number in numbers do
    let square = Math.Pow(number, 2) |> int
    printfn $"{number}^{square}"
    printfn $"Adding {square} to the collection...\n"
    numbers.Add square

// The example displays the following output:
//    1^1
//    Adding 1 to the collection...
//
//
//    Unhandled Exception: System.InvalidOperationException: Collection was modified
//       enumeration operation may not execute.
//       at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
//       at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
//       at <StartupCode$fs>.main()
Imports System.Collections.Generic

Module Example
   Public Sub Main()
      Dim numbers As New List(Of Integer)( { 1, 2, 3, 4, 5 } )
      For Each number In numbers
         Dim square As Integer = CInt(Math.Pow(number, 2))
         Console.WriteLine("{0}^{1}", number, square)
         Console.WriteLine("Adding {0} to the collection..." + vbCrLf, 
                           square)
         numbers.Add(square)
      Next
   End Sub
End Module
' The example displays the following output:
'    1^1
'    Adding 1 to the collection...
'    
'    
'    Unhandled Exception: System.InvalidOperationException: Collection was modified; 
'       enumeration operation may not execute.
'       at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
'       at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
'       at Example.Main()

È possibile eliminare l'eccezione in uno dei due modi, a seconda della logica dell'applicazione:

  • Se gli elementi devono essere aggiunti alla raccolta durante l'iterazione, è possibile eseguirne l'iterazione usando l'istruzione for (for..to in F#) anziché foreach, for...ino For Each. Nell'esempio seguente viene utilizzata l'istruzione for per aggiungere il quadrato di numeri nella raccolta all'insieme.

    using System;
    using System.Collections.Generic;
    
    public class Example
    {
       public static void Main()
       {
          var numbers = new List<int>() { 1, 2, 3, 4, 5 };
    
          int upperBound = numbers.Count - 1;
          for (int ctr = 0; ctr <= upperBound; ctr++) {
             int square = (int) Math.Pow(numbers[ctr], 2);
             Console.WriteLine("{0}^{1}", numbers[ctr], square);
             Console.WriteLine("Adding {0} to the collection...\n", square);
             numbers.Add(square);
          }
    
          Console.WriteLine("Elements now in the collection: ");
          foreach (var number in numbers)
             Console.Write("{0}    ", number);
       }
    }
    // The example displays the following output:
    //    1^1
    //    Adding 1 to the collection...
    //
    //    2^4
    //    Adding 4 to the collection...
    //
    //    3^9
    //    Adding 9 to the collection...
    //
    //    4^16
    //    Adding 16 to the collection...
    //
    //    5^25
    //    Adding 25 to the collection...
    //
    //    Elements now in the collection:
    //    1    2    3    4    5    1    4    9    16    25
    
    open System
    open System.Collections.Generic
    
    let numbers = ResizeArray [| 1; 2; 3; 4; 5 |]
    
    let upperBound = numbers.Count - 1
    for i = 0 to upperBound do
        let square = Math.Pow(numbers[i], 2) |> int
        printfn $"{numbers[i]}^{square}"
        printfn $"Adding {square} to the collection...\n"
        numbers.Add square
    
    printfn "Elements now in the collection: "
    for number in numbers do
        printf $"{number}    "
    // The example displays the following output:
    //    1^1
    //    Adding 1 to the collection...
    //
    //    2^4
    //    Adding 4 to the collection...
    //
    //    3^9
    //    Adding 9 to the collection...
    //
    //    4^16
    //    Adding 16 to the collection...
    //
    //    5^25
    //    Adding 25 to the collection...
    //
    //    Elements now in the collection:
    //    1    2    3    4    5    1    4    9    16    25
    
    Imports System.Collections.Generic
    
    Module Example
       Public Sub Main()
          Dim numbers As New List(Of Integer)( { 1, 2, 3, 4, 5 } )
          Dim upperBound = numbers.Count - 1
    
          For ctr As Integer = 0 To upperBound
             Dim square As Integer = CInt(Math.Pow(numbers(ctr), 2))
             Console.WriteLine("{0}^{1}", numbers(ctr), square)
             Console.WriteLine("Adding {0} to the collection..." + vbCrLf, 
                               square)
             numbers.Add(square)
          Next
       
          Console.WriteLine("Elements now in the collection: ")
          For Each number In numbers
             Console.Write("{0}    ", number)
          Next   
       End Sub
    End Module
    ' The example displays the following output:
    '    1^1
    '    Adding 1 to the collection...
    '    
    '    2^4
    '    Adding 4 to the collection...
    '    
    '    3^9
    '    Adding 9 to the collection...
    '    
    '    4^16
    '    Adding 16 to the collection...
    '    
    '    5^25
    '    Adding 25 to the collection...
    '    
    '    Elements now in the collection:
    '    1    2    3    4    5    1    4    9    16    25
    

    Si noti che è necessario stabilire il numero di iterazioni prima di eseguire l'iterazione della raccolta usando un contatore all'interno del ciclo che esce dal ciclo in modo appropriato, eseguendo l'iterazione indietro, da Count - 1 a 0 o, come nell'esempio, assegnando il numero di elementi nella matrice a una variabile e usandolo per stabilire il limite superiore del ciclo. In caso contrario, se un elemento viene aggiunto alla raccolta in ogni iterazione, un ciclo infinito restituisce risultati.

  • Se non è necessario aggiungere elementi alla raccolta durante l'iterazione, è possibile archiviare gli elementi da aggiungere in una raccolta temporanea aggiunta al termine dell'iterazione della raccolta. Nell'esempio seguente viene usato questo approccio per aggiungere il quadrato di numeri in un insieme temporaneo e quindi combinare le raccolte in un singolo oggetto matrice.

    using System;
    using System.Collections.Generic;
    
    public class Example
    {
       public static void Main()
       {
          var numbers = new List<int>() { 1, 2, 3, 4, 5 };
          var temp = new List<int>();
    
          // Square each number and store it in a temporary collection.
          foreach (var number in numbers) {
             int square = (int) Math.Pow(number, 2);
             temp.Add(square);
          }
    
          // Combine the numbers into a single array.
          int[] combined = new int[numbers.Count + temp.Count];
          Array.Copy(numbers.ToArray(), 0, combined, 0, numbers.Count);
          Array.Copy(temp.ToArray(), 0, combined, numbers.Count, temp.Count);
    
          // Iterate the array.
          foreach (var value in combined)
             Console.Write("{0}    ", value);
       }
    }
    // The example displays the following output:
    //       1    2    3    4    5    1    4    9    16    25
    
    open System
    open System.Collections.Generic
    
    let numbers = ResizeArray [| 1; 2; 3; 4; 5 |]
    let temp = ResizeArray()
    
    // Square each number and store it in a temporary collection.
    for number in numbers do
        let square = Math.Pow(number, 2) |> int
        temp.Add square
    
    // Combine the numbers into a single array.
    let combined = Array.zeroCreate<int> (numbers.Count + temp.Count)
    Array.Copy(numbers.ToArray(), 0, combined, 0, numbers.Count)
    Array.Copy(temp.ToArray(), 0, combined, numbers.Count, temp.Count)
    
    // Iterate the array.
    for value in combined do
        printf $"{value}    "
    // The example displays the following output:
    //       1    2    3    4    5    1    4    9    16    25
    
    Imports System.Collections.Generic
    
    Module Example
       Public Sub Main()
          Dim numbers As New List(Of Integer)( { 1, 2, 3, 4, 5 } )
          Dim temp As New List(Of Integer)()
          
          ' Square each number and store it in a temporary collection.
          For Each number In numbers
             Dim square As Integer = CInt(Math.Pow(number, 2))
             temp.Add(square)
          Next
        
          ' Combine the numbers into a single array.
          Dim combined(numbers.Count + temp.Count - 1) As Integer 
          Array.Copy(numbers.ToArray(), 0, combined, 0, numbers.Count)
          Array.Copy(temp.ToArray(), 0, combined, numbers.Count, temp.Count)
          
          ' Iterate the array.
          For Each value In combined
             Console.Write("{0}    ", value)
          Next
       End Sub
    End Module
    ' The example displays the following output:
    '       1    2    3    4    5    1    4    9    16    25
    

Ordinamento di una matrice o di una raccolta i cui oggetti non possono essere confrontati

I metodi di ordinamento per utilizzo generico, ad esempio il metodo o il Array.Sort(Array) List<T>.Sort() metodo, in genere richiedono che almeno uno degli oggetti venga ordinato implementare IComparable<T> o l'interfaccia IComparable . In caso contrario, non è possibile ordinare la raccolta o la matrice e il metodo genera un'eccezione InvalidOperationException . L'esempio seguente definisce una Person classe, archivia due Person oggetti in un oggetto generico List<T> e tenta di ordinarli. Come illustrato dall'output dell'esempio, la chiamata al List<T>.Sort() metodo genera un InvalidOperationExceptionoggetto .

using System;
using System.Collections.Generic;

public class Person
{
   public Person(String fName, String lName)
   {
      FirstName = fName;
      LastName = lName;
   }

   public String FirstName { get; set; }
   public String LastName { get; set; }
}

public class Example
{
   public static void Main()
   {
      var people = new List<Person>();

      people.Add(new Person("John", "Doe"));
      people.Add(new Person("Jane", "Doe"));
      people.Sort();
      foreach (var person in people)
         Console.WriteLine("{0} {1}", person.FirstName, person.LastName);
   }
}
// The example displays the following output:
//    Unhandled Exception: System.InvalidOperationException: Failed to compare two elements in the array. --->
//       System.ArgumentException: At least one object must implement IComparable.
//       at System.Collections.Comparer.Compare(Object a, Object b)
//       at System.Collections.Generic.ArraySortHelper`1.SwapIfGreater(T[] keys, IComparer`1 comparer, Int32 a, Int32 b)
//       at System.Collections.Generic.ArraySortHelper`1.DepthLimitedQuickSort(T[] keys, Int32 left, Int32 right, IComparer`1 comparer, Int32 depthLimit)
//       at System.Collections.Generic.ArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)
//       --- End of inner exception stack trace ---
//       at System.Collections.Generic.ArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)
//       at System.Array.Sort[T](T[] array, Int32 index, Int32 length, IComparer`1 comparer)
//       at System.Collections.Generic.List`1.Sort(Int32 index, Int32 count, IComparer`1 comparer)
//       at Example.Main()
type Person(firstName: string, lastName: string) =
    member val FirstName = firstName with get, set
    member val LastName = lastName with get, set

let people = ResizeArray()

people.Add(Person("John", "Doe"))
people.Add(Person("Jane", "Doe"))
people.Sort()
for person in people do
    printfn $"{person.FirstName} {person.LastName}"
// The example displays the following output:
//    Unhandled Exception: System.InvalidOperationException: Failed to compare two elements in the array. --->
//       System.ArgumentException: At least one object must implement IComparable.
//       at System.Collections.Comparer.Compare(Object a, Object b)
//       at System.Collections.Generic.ArraySortHelper`1.SwapIfGreater(T[] keys, IComparer`1 comparer, Int32 a, Int32 b)
//       at System.Collections.Generic.ArraySortHelper`1.DepthLimitedQuickSort(T[] keys, Int32 left, Int32 right, IComparer`1 comparer, Int32 depthLimit)
//       at System.Collections.Generic.ArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)
//       --- End of inner exception stack trace ---
//       at System.Collections.Generic.ArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)
//       at System.Array.Sort[T](T[] array, Int32 index, Int32 length, IComparer`1 comparer)
//       at System.Collections.Generic.List`1.Sort(Int32 index, Int32 count, IComparer`1 comparer)
//       at <StartupCode$fs>.main()
Imports System.Collections.Generic

Public Class Person
   Public Sub New(fName As String, lName As String)
      FirstName = fName
      LastName = lName
   End Sub
   
   Public Property FirstName As String
   Public Property LastName As String
End Class

Module Example
   Public Sub Main()
      Dim people As New List(Of Person)()
      
      people.Add(New Person("John", "Doe"))
      people.Add(New Person("Jane", "Doe"))
      people.Sort()
      For Each person In people
         Console.WriteLine("{0} {1}", person.FirstName, person.LastName)
      Next
   End Sub
End Module
' The example displays the following output:
'    Unhandled Exception: System.InvalidOperationException: Failed to compare two elements in the array. ---> 
'       System.ArgumentException: At least one object must implement IComparable.
'       at System.Collections.Comparer.Compare(Object a, Object b)
'       at System.Collections.Generic.ArraySortHelper`1.SwapIfGreater(T[] keys, IComparer`1 comparer, Int32 a, Int32 b)
'       at System.Collections.Generic.ArraySortHelper`1.DepthLimitedQuickSort(T[] keys, Int32 left, Int32 right, IComparer`1 comparer, Int32 depthLimit)
'       at System.Collections.Generic.ArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)
'       --- End of inner exception stack trace ---
'       at System.Collections.Generic.ArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)
'       at System.Array.Sort[T](T[] array, Int32 index, Int32 length, IComparer`1 comparer)
'       at System.Collections.Generic.List`1.Sort(Int32 index, Int32 count, IComparer`1 comparer)
'       at Example.Main()

È possibile eliminare l'eccezione in uno dei tre modi seguenti:

  • Se è possibile possedere il tipo che si sta tentando di ordinare, ovvero se si controlla il codice sorgente, è possibile modificarlo per implementare o l'interfaccia IComparable<T> IComparable . Ciò richiede l'implementazione del IComparable<T>.CompareTo metodo o del CompareTo metodo. L'aggiunta di un'implementazione dell'interfaccia a un tipo esistente non è una modifica di rilievo.

    Nell'esempio seguente viene usato questo approccio per fornire un'implementazione IComparable<T> per la Person classe. È comunque possibile chiamare il metodo di ordinamento generale della raccolta o della matrice e, come illustrato dall'output dell'esempio, l'ordinamento della raccolta è riuscito.

    using System;
    using System.Collections.Generic;
    
    public class Person : IComparable<Person>
    {
       public Person(String fName, String lName)
       {
          FirstName = fName;
          LastName = lName;
       }
    
       public String FirstName { get; set; }
       public String LastName { get; set; }
    
       public int CompareTo(Person other)
       {
          return String.Format("{0} {1}", LastName, FirstName).
                 CompareTo(String.Format("{0} {1}", other.LastName, other.FirstName));
       }
    }
    
    public class Example
    {
       public static void Main()
       {
          var people = new List<Person>();
    
          people.Add(new Person("John", "Doe"));
          people.Add(new Person("Jane", "Doe"));
          people.Sort();
          foreach (var person in people)
             Console.WriteLine("{0} {1}", person.FirstName, person.LastName);
       }
    }
    // The example displays the following output:
    //       Jane Doe
    //       John Doe
    
    open System
    
    type Person(firstName: string, lastName: string) =
        member val FirstName = firstName with get, set
        member val LastName = lastName with get, set
        
        interface IComparable<Person> with
            member this.CompareTo(other) =
                compare $"{this.LastName} {this.FirstName}" $"{other.LastName} {other.FirstName}"
    
    let people = ResizeArray()
    
    people.Add(new Person("John", "Doe"))
    people.Add(new Person("Jane", "Doe"))
    people.Sort()
    for person in people do
        printfn $"{person.FirstName} {person.LastName}"
    // The example displays the following output:
    //       Jane Doe
    //       John Doe
    
    Imports System.Collections.Generic
    
    Public Class Person : Implements IComparable(Of Person)
       Public Sub New(fName As String, lName As String)
          FirstName = fName
          LastName = lName
       End Sub
       
       Public Property FirstName As String
       Public Property LastName As String
       
       Public Function CompareTo(other As Person) As Integer _
              Implements IComparable(Of Person).CompareTo
          Return String.Format("{0} {1}", LastName, FirstName).
                CompareTo(String.Format("{0} {1}", other.LastName, other.FirstName))    
       End Function
    End Class
    
    Module Example
       Public Sub Main()
          Dim people As New List(Of Person)()
          
          people.Add(New Person("John", "Doe"))
          people.Add(New Person("Jane", "Doe"))
          people.Sort()
          For Each person In people
             Console.WriteLine("{0} {1}", person.FirstName, person.LastName)
          Next
       End Sub
    End Module
    ' The example displays the following output:
    '       Jane Doe
    '       John Doe
    
  • Se non è possibile modificare il codice sorgente per il tipo che si sta tentando di ordinare, è possibile definire una classe di ordinamento a scopo speciale che implementa l'interfaccia IComparer<T> . È possibile chiamare un overload del Sort metodo che include un IComparer<T> parametro. Questo approccio è particolarmente utile se si vuole sviluppare una classe di ordinamento specializzata che può ordinare gli oggetti in base a più criteri.

    Nell'esempio seguente viene usato l'approccio sviluppando una classe personalizzata PersonComparer usata per ordinare Person le raccolte. Passa quindi un'istanza di questa classe al List<T>.Sort(IComparer<T>) metodo.

    using System;
    using System.Collections.Generic;
    
    public class Person
    {
       public Person(String fName, String lName)
       {
          FirstName = fName;
          LastName = lName;
       }
    
       public String FirstName { get; set; }
       public String LastName { get; set; }
    }
    
    public class PersonComparer : IComparer<Person>
    {
       public int Compare(Person x, Person y)
       {
          return String.Format("{0} {1}", x.LastName, x.FirstName).
                 CompareTo(String.Format("{0} {1}", y.LastName, y.FirstName));
       }
    }
    
    public class Example
    {
       public static void Main()
       {
          var people = new List<Person>();
    
          people.Add(new Person("John", "Doe"));
          people.Add(new Person("Jane", "Doe"));
          people.Sort(new PersonComparer());
          foreach (var person in people)
             Console.WriteLine("{0} {1}", person.FirstName, person.LastName);
       }
    }
    // The example displays the following output:
    //       Jane Doe
    //       John Doe
    
    open System
    open System.Collections.Generic
    
    type Person(firstName, lastName) =
        member val FirstName = firstName with get, set
        member val LastName = lastName with get, set
    
    type PersonComparer() =
        interface IComparer<Person> with
            member _.Compare(x: Person, y: Person) =
                $"{x.LastName} {x.FirstName}".CompareTo $"{y.LastName} {y.FirstName}"
    
    let people = ResizeArray()
    
    people.Add(Person("John", "Doe"))
    people.Add(Person("Jane", "Doe"))
    people.Sort(PersonComparer())
    for person in people do
        printfn $"{person.FirstName} {person.LastName}"
    // The example displays the following output:
    //       Jane Doe
    //       John Doe
    
    Imports System.Collections.Generic
    
    Public Class Person
       Public Sub New(fName As String, lName As String)
          FirstName = fName
          LastName = lName
       End Sub
       
       Public Property FirstName As String
       Public Property LastName As String
    End Class
    
    Public Class PersonComparer : Implements IComparer(Of Person)
       Public Function Compare(x As Person, y As Person) As Integer _
              Implements IComparer(Of Person).Compare
          Return String.Format("{0} {1}", x.LastName, x.FirstName).
                 CompareTo(String.Format("{0} {1}", y.LastName, y.FirstName))    
       End Function       
    End Class
    
    Module Example
       Public Sub Main()
          Dim people As New List(Of Person)()
          
          people.Add(New Person("John", "Doe"))
          people.Add(New Person("Jane", "Doe"))
          people.Sort(New PersonComparer())
          For Each person In people
             Console.WriteLine("{0} {1}", person.FirstName, person.LastName)
          Next
       End Sub
    End Module
    ' The example displays the following output:
    '       Jane Doe
    '       John Doe
    
  • Se non è possibile modificare il codice sorgente per il tipo che si sta tentando di ordinare, è possibile creare un Comparison<T> delegato per eseguire l'ordinamento. La firma del delegato è

    Function Comparison(Of T)(x As T, y As T) As Integer  
    
    int Comparison<T>(T x, T y)  
    

    Nell'esempio seguente viene usato l'approccio definendo un PersonComparison metodo che corrisponde alla firma del Comparison<T> delegato. Passa quindi questo delegato al List<T>.Sort(Comparison<T>) metodo.

    using System;
    using System.Collections.Generic;
    
    public class Person
    {
       public Person(String fName, String lName)
       {
          FirstName = fName;
          LastName = lName;
       }
    
       public String FirstName { get; set; }
       public String LastName { get; set; }
    }
    
    public class Example
    {
       public static void Main()
       {
          var people = new List<Person>();
    
          people.Add(new Person("John", "Doe"));
          people.Add(new Person("Jane", "Doe"));
          people.Sort(PersonComparison);
          foreach (var person in people)
             Console.WriteLine("{0} {1}", person.FirstName, person.LastName);
       }
    
       public static int PersonComparison(Person x, Person y)
       {
          return String.Format("{0} {1}", x.LastName, x.FirstName).
                 CompareTo(String.Format("{0} {1}", y.LastName, y.FirstName));
       }
    }
    // The example displays the following output:
    //       Jane Doe
    //       John Doe
    
    open System
    open System.Collections.Generic
    
    type Person(firstName, lastName) =
        member val FirstName = firstName with get, set
        member val LastName = lastName with get, set
    
    let personComparison (x: Person) (y: Person) =
        $"{x.LastName} {x.FirstName}".CompareTo $"{y.LastName} {y.FirstName}"
    
    let people = ResizeArray()
    
    people.Add(Person("John", "Doe"))
    people.Add(Person("Jane", "Doe"))
    people.Sort personComparison
    for person in people do
        printfn $"{person.FirstName} {person.LastName}"
    
    // The example displays the following output:
    //       Jane Doe
    //       John Doe
    
    Imports System.Collections.Generic
    
    Public Class Person
       Public Sub New(fName As String, lName As String)
          FirstName = fName
          LastName = lName
       End Sub
       
       Public Property FirstName As String
       Public Property LastName As String
    End Class
    
    Module Example
       Public Sub Main()
          Dim people As New List(Of Person)()
          
          people.Add(New Person("John", "Doe"))
          people.Add(New Person("Jane", "Doe"))
          people.Sort(AddressOf PersonComparison)
          For Each person In people
             Console.WriteLine("{0} {1}", person.FirstName, person.LastName)
          Next
       End Sub
       
       Public Function PersonComparison(x As Person, y As Person) As Integer
          Return String.Format("{0} {1}", x.LastName, x.FirstName).
                 CompareTo(String.Format("{0} {1}", y.LastName, y.FirstName))    
       End Function
    End Module
    ' The example displays the following output:
    '       Jane Doe
    '       John Doe
    

Cast di un valore Nullable<T> null al tipo sottostante

Il tentativo di eseguire il cast di un valore che corrisponde null al tipo sottostante genera un'eccezione Nullable<T> InvalidOperationException e visualizza il messaggio di errore "L'oggetto Nullable deve avere un valore.

Nell'esempio seguente viene generata un'eccezione quando tenta di eseguire l'iterazione di una InvalidOperationException matrice che include un Nullable(Of Integer) valore.

using System;
using System.Linq;

public class Example
{
   public static void Main()
   {
      var queryResult = new int?[] { 1, 2, null, 4 };
      var map = queryResult.Select(nullableInt => (int)nullableInt);

      // Display list.
      foreach (var num in map)
         Console.Write("{0} ", num);
      Console.WriteLine();
   }
}
// The example displays the following output:
//    1 2
//    Unhandled Exception: System.InvalidOperationException: Nullable object must have a value.
//       at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
//       at Example.<Main>b__0(Nullable`1 nullableInt)
//       at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
//       at Example.Main()
open System
open System.Linq

let queryResult = [| Nullable 1; Nullable 2; Nullable(); Nullable 4 |]
let map = queryResult.Select(fun nullableInt -> nullableInt.Value)

// Display list.
for num in map do
    printf $"{num} "
printfn ""
// The example displays the following output:
//    1 2
//    Unhandled Exception: System.InvalidOperationException: Nullable object must have a value.
//       at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
//       at Example.<Main>b__0(Nullable`1 nullableInt)
//       at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
//       at <StartupCode$fs>.main()
Imports System.Linq

Module Example
   Public Sub Main()
      Dim queryResult = New Integer?() { 1, 2, Nothing, 4 }
      Dim map = queryResult.Select(Function(nullableInt) CInt(nullableInt))
      
      ' Display list.
      For Each num In map
         Console.Write("{0} ", num)
      Next
      Console.WriteLine()   
   End Sub
End Module
' The example displays thIe following output:
'    1 2
'    Unhandled Exception: System.InvalidOperationException: Nullable object must have a value.
'       at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
'       at Example.<Main>b__0(Nullable`1 nullableInt)
'       at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
'       at Example.Main()

Per evitare l'eccezione:

L'esempio seguente esegue entrambe le operazioni per evitare l'eccezione InvalidOperationException .

using System;
using System.Linq;

public class Example
{
   public static void Main()
   {
      var queryResult = new int?[] { 1, 2, null, 4 };
      var numbers = queryResult.Select(nullableInt => (int)nullableInt.GetValueOrDefault());

      // Display list using Nullable<int>.HasValue.
      foreach (var number in numbers)
         Console.Write("{0} ", number);
      Console.WriteLine();

      numbers = queryResult.Select(nullableInt => (int) (nullableInt.HasValue ? nullableInt : -1));
      // Display list using Nullable<int>.GetValueOrDefault.
      foreach (var number in numbers)
         Console.Write("{0} ", number);
      Console.WriteLine();
   }
}
// The example displays the following output:
//       1 2 0 4
//       1 2 -1 4
open System
open System.Linq

let queryResult = [| Nullable 1; Nullable 2; Nullable(); Nullable 4 |]
let numbers = queryResult.Select(fun nullableInt -> nullableInt.GetValueOrDefault())

// Display list using Nullable<int>.HasValue.
for number in numbers do
    printf $"{number} "
printfn ""

let numbers2 = queryResult.Select(fun nullableInt -> if nullableInt.HasValue then nullableInt.Value else -1)
// Display list using Nullable<int>.GetValueOrDefault.
for number in numbers2 do
    printf $"{number} "
printfn ""
// The example displays the following output:
//       1 2 0 4
//       1 2 -1 4
Imports System.Linq

Module Example
   Public Sub Main()
      Dim queryResult = New Integer?() { 1, 2, Nothing, 4 }
      Dim numbers = queryResult.Select(Function(nullableInt) _ 
                                          CInt(nullableInt.GetValueOrDefault()))
      ' Display list.
      For Each number In numbers
         Console.Write("{0} ", number)
      Next
      Console.WriteLine()
      
      ' Use -1 to indicate a missing values.
      numbers = queryResult.Select(Function(nullableInt) _   
                                      CInt(If(nullableInt.HasValue, nullableInt, -1)))
      ' Display list.
      For Each number In numbers
         Console.Write("{0} ", number)
      Next
      Console.WriteLine()
                                      
   End Sub
End Module
' The example displays the following output:
'       1 2 0 4
'       1 2 -1 4

Chiamata di un metodo System.Linq.Enumerable su una raccolta vuota

I Enumerable.Aggregatemetodi , Enumerable.Average, Enumerable.LastEnumerable.FirstEnumerable.MinEnumerable.Max, Enumerable.Single, e Enumerable.SingleOrDefault eseguono operazioni su una sequenza e restituiscono un singolo risultato. Alcuni overload di questi metodi generano un'eccezione InvalidOperationException quando la sequenza è vuota, mentre altri overload restituiscono null. Il Enumerable.SingleOrDefault metodo genera anche un'eccezione InvalidOperationException quando la sequenza contiene più di un elemento.

Nota

La maggior parte dei metodi che generano un'eccezione InvalidOperationException sono overload. Assicurarsi di comprendere il comportamento dell'overload scelto.

Nella tabella seguente sono elencati i messaggi di eccezione degli InvalidOperationException oggetti eccezione generati dalle chiamate ad alcuni System.Linq.Enumerable metodi.

Metodo Messaggio
Aggregate
Average
Last
Max
Min
Sequenza non contiene elementi
First Sequenza non contiene alcun elemento corrispondente
Single
SingleOrDefault
Sequenza contiene più elementi corrispondenti

La modalità di eliminazione o gestione dell'eccezione dipende dai presupposti dell'applicazione e dal metodo specifico chiamato.

  • Quando si chiama deliberatamente uno di questi metodi senza verificare la presenza di una sequenza vuota, si presuppone che la sequenza non sia vuota e che una sequenza vuota sia un'occorrenza imprevista. In questo caso, intercettare o rigenerare l'eccezione è appropriata.

  • Se il controllo di una sequenza vuota non è stato accidentale, è possibile chiamare uno degli overload dell'overload Enumerable.Any per determinare se una sequenza contiene elementi.

    Suggerimento

    La chiamata al Enumerable.Any<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) metodo prima di generare una sequenza può migliorare le prestazioni se i dati da elaborare potrebbero contenere un numero elevato di elementi o se l'operazione che genera la sequenza è costosa.

  • Se è stato chiamato un metodo come Enumerable.First, Enumerable.Lasto Enumerable.Single, è possibile sostituire un metodo alternativo, ad esempio Enumerable.FirstOrDefault, Enumerable.LastOrDefaulto Enumerable.SingleOrDefault, che restituisce un valore predefinito anziché un membro della sequenza.

Gli esempi forniscono dettagli aggiuntivi.

Nell'esempio seguente viene utilizzato il Enumerable.Average metodo per calcolare la media di una sequenza i cui valori sono maggiori di 4. Poiché nessun valore della matrice originale supera 4, nella sequenza non sono inclusi valori e il metodo genera un'eccezione InvalidOperationException .

using System;
using System.Linq;

public class Example
{
   public static void Main()
   {
      int[] data = { 1, 2, 3, 4 };
      var average = data.Where(num => num > 4).Average();
      Console.Write("The average of numbers greater than 4 is {0}",
                    average);
   }
}
// The example displays the following output:
//    Unhandled Exception: System.InvalidOperationException: Sequence contains no elements
//       at System.Linq.Enumerable.Average(IEnumerable`1 source)
//       at Example.Main()
open System
open System.Linq

let data = [| 1; 2; 3; 4 |]
let average = 
   data.Where(fun num -> num > 4).Average();
printfn $"The average of numbers greater than 4 is {average}"
// The example displays the following output:
//    Unhandled Exception: System.InvalidOperationException: Sequence contains no elements
//       at System.Linq.Enumerable.Average(IEnumerable`1 source)
//       at <StartupCode$fs>.main()
Imports System.Linq

Module Example
   Public Sub Main()
      Dim data() As Integer = { 1, 2, 3, 4 }
      Dim average = data.Where(Function(num) num > 4).Average()
      Console.Write("The average of numbers greater than 4 is {0}",
                    average)
   End Sub
End Module
' The example displays the following output:
'    Unhandled Exception: System.InvalidOperationException: Sequence contains no elements
'       at System.Linq.Enumerable.Average(IEnumerable`1 source)
'       at Example.Main()

L'eccezione può essere eliminata chiamando il Any metodo per determinare se la sequenza contiene elementi prima di chiamare il metodo che elabora la sequenza, come illustrato nell'esempio seguente.

using System;
using System.Linq;

public class Example
{
   public static void Main()
   {
       int[] dbQueryResults = { 1, 2, 3, 4 };
       var moreThan4 = dbQueryResults.Where(num => num > 4);

       if(moreThan4.Any())
           Console.WriteLine("Average value of numbers greater than 4: {0}:",
                             moreThan4.Average());
       else
           // handle empty collection
           Console.WriteLine("The dataset has no values greater than 4.");
   }
}
// The example displays the following output:
//       The dataset has no values greater than 4.
open System
open System.Linq

let dbQueryResults = [| 1; 2; 3; 4 |]
let moreThan4 = 
    dbQueryResults.Where(fun num -> num > 4)

if moreThan4.Any() then
    printfn $"Average value of numbers greater than 4: {moreThan4.Average()}:"
else
    // handle empty collection
    printfn "The dataset has no values greater than 4."

// The example displays the following output:
//       The dataset has no values greater than 4.
Imports System.Linq

Module Example
   Public Sub Main()
       Dim dbQueryResults() As Integer = { 1, 2, 3, 4 }
       Dim moreThan4 = dbQueryResults.Where(Function(num) num > 4)
   
       If moreThan4.Any() Then
           Console.WriteLine("Average value of numbers greater than 4: {0}:", 
                             moreThan4.Average())
       Else
           ' Handle empty collection. 
           Console.WriteLine("The dataset has no values greater than 4.")
       End If    
   End Sub
End Module
' The example displays the following output:
'       The dataset has no values greater than 4.

Il Enumerable.First metodo restituisce il primo elemento in una sequenza o il primo elemento in una sequenza che soddisfa una condizione specificata. Se la sequenza è vuota e pertanto non ha un primo elemento, genera un'eccezione InvalidOperationException .

Nell'esempio seguente il Enumerable.First<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) metodo genera un'eccezione InvalidOperationException perché la matrice dbQueryResults non contiene un elemento maggiore di 4.

using System;
using System.Linq;

public class Example
{
   public static void Main()
   {
      int[] dbQueryResults = { 1, 2, 3, 4 };

      var firstNum = dbQueryResults.First(n => n > 4);

      Console.WriteLine("The first value greater than 4 is {0}",
                        firstNum);
   }
}
// The example displays the following output:
//    Unhandled Exception: System.InvalidOperationException:
//       Sequence contains no matching element
//       at System.Linq.Enumerable.First[TSource](IEnumerable`1 source, Func`2 predicate)
//       at Example.Main()
open System
open System.Linq

let dbQueryResults = [| 1; 2; 3; 4 |]

let firstNum = dbQueryResults.First(fun n -> n > 4)

printfn $"The first value greater than 4 is {firstNum}"

// The example displays the following output:
//    Unhandled Exception: System.InvalidOperationException:
//       Sequence contains no matching element
//       at System.Linq.Enumerable.First[TSource](IEnumerable`1 source, Func`2 predicate)
//       at <StartupCode$fs>.main()
Imports System.Linq

Module Example
   Public Sub Main()
      Dim dbQueryResults() As Integer = { 1, 2, 3, 4 }

      Dim firstNum = dbQueryResults.First(Function(n) n > 4)

      Console.WriteLine("The first value greater than 4 is {0}", 
                        firstNum)
   End Sub
End Module
' The example displays the following output:
'    Unhandled Exception: System.InvalidOperationException: 
'       Sequence contains no matching element
'       at System.Linq.Enumerable.First[TSource](IEnumerable`1 source, Func`2 predicate)
'       at Example.Main()

È possibile chiamare il Enumerable.FirstOrDefault metodo invece di Enumerable.First restituire un valore specificato o predefinito. Se il metodo non trova un primo elemento nella sequenza, restituisce il valore predefinito per tale tipo di dati. Il valore predefinito è null per un tipo riferimento, zero per un tipo di dati numerico e DateTime.MinValue per il DateTime tipo .

Nota

L'interpretazione del valore restituito dal Enumerable.FirstOrDefault metodo è spesso complicata dal fatto che il valore predefinito del tipo può essere un valore valido nella sequenza. In questo caso, si chiama il Enumerable.Any metodo per determinare se la sequenza dispone di membri validi prima di chiamare il Enumerable.First metodo .

Nell'esempio seguente viene chiamato il Enumerable.FirstOrDefault<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) metodo per impedire l'eccezione InvalidOperationException generata nell'esempio precedente.

using System;
using System.Linq;

public class Example
{
   public static void Main()
   {
      int[] dbQueryResults = { 1, 2, 3, 4 };

      var firstNum = dbQueryResults.FirstOrDefault(n => n > 4);

      if (firstNum == 0)
         Console.WriteLine("No value is greater than 4.");
      else
         Console.WriteLine("The first value greater than 4 is {0}",
                           firstNum);
   }
}
// The example displays the following output:
//       No value is greater than 4.
open System
open System.Linq

let dbQueryResults = [| 1; 2; 3; 4 |]

let firstNum = dbQueryResults.FirstOrDefault(fun n -> n > 4)

if firstNum = 0 then
    printfn "No value is greater than 4."
else
    printfn $"The first value greater than 4 is {firstNum}"

// The example displays the following output:
//       No value is greater than 4.
Imports System.Linq

Module Example
   Public Sub Main()
      Dim dbQueryResults() As Integer = { 1, 2, 3, 4 }

      Dim firstNum = dbQueryResults.FirstOrDefault(Function(n) n > 4)

      If firstNum = 0 Then
         Console.WriteLIne("No value is greater than 4.")
      Else   
         Console.WriteLine("The first value greater than 4 is {0}", 
                           firstNum)
      End If                     
   End Sub
End Module
' The example displays the following output:
'       No value is greater than 4.

Chiamata di Enumerable.Single o Enumerable.SingleOrDefault in una sequenza senza un elemento

Il Enumerable.Single metodo restituisce l'unico elemento di una sequenza o l'unico elemento di una sequenza che soddisfa una condizione specificata. Se non sono presenti elementi nella sequenza o se è presente più di un elemento , il metodo genera un'eccezione InvalidOperationException .

È possibile utilizzare il Enumerable.SingleOrDefault metodo per restituire un valore predefinito anziché generare un'eccezione quando la sequenza non contiene elementi. Tuttavia, il Enumerable.SingleOrDefault metodo genera comunque un'eccezione InvalidOperationException quando la sequenza contiene più di un elemento.

Nella tabella seguente sono elencati i messaggi di eccezione degli InvalidOperationException oggetti eccezione generati dalle chiamate ai Enumerable.Single metodi e Enumerable.SingleOrDefault .

Metodo Messaggio
Single Sequenza non contiene alcun elemento corrispondente
Single
SingleOrDefault
Sequenza contiene più elementi corrispondenti

Nell'esempio seguente la chiamata al Enumerable.Single metodo genera un'eccezione InvalidOperationException perché la sequenza non ha un elemento maggiore di 4.

using System;
using System.Linq;

public class Example
{
   public static void Main()
   {
       int[] dbQueryResults = { 1, 2, 3, 4 };

       var singleObject = dbQueryResults.Single(value => value > 4);

       // Display results.
       Console.WriteLine("{0} is the only value greater than 4", singleObject);
   }
}
// The example displays the following output:
//    Unhandled Exception: System.InvalidOperationException:
//       Sequence contains no matching element
//       at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source, Func`2 predicate)
//       at Example.Main()
open System
open System.Linq

let dbQueryResults = [| 1; 2; 3; 4 |]

let singleObject = dbQueryResults.Single(fun value -> value > 4)

// Display results.
printfn $"{singleObject} is the only value greater than 4"

// The example displays the following output:
//    Unhandled Exception: System.InvalidOperationException:
//       Sequence contains no matching element
//       at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source, Func`2 predicate)
//       at <StartupCode$fs>.main()
Imports System.Linq

Module Example
   Public Sub Main()
       Dim dbQueryResults() As Integer = { 1, 2, 3, 4 }
   
       Dim singleObject = dbQueryResults.Single(Function(value) value > 4)
   
       ' Display results.
       Console.WriteLine("{0} is the only value greater than 4", 
                         singleObject)
   End Sub
End Module
' The example displays the following output:
'    Unhandled Exception: System.InvalidOperationException: 
'       Sequence contains no matching element
'       at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source, Func`2 predicate)
'       at Example.Main()

L'esempio seguente tenta di impedire l'eccezione InvalidOperationException generata quando una sequenza è vuota chiamando invece il Enumerable.SingleOrDefault metodo . Tuttavia, poiché questa sequenza restituisce più elementi il cui valore è maggiore di 2, genera anche un'eccezione InvalidOperationException .

using System;
using System.Linq;

public class Example
{
   public static void Main()
   {
       int[] dbQueryResults = { 1, 2, 3, 4 };

       var singleObject = dbQueryResults.SingleOrDefault(value => value > 2);

       if (singleObject != 0)
           Console.WriteLine("{0} is the only value greater than 2",
                             singleObject);
       else
           // Handle an empty collection.
           Console.WriteLine("No value is greater than 2");
   }
}
// The example displays the following output:
//    Unhandled Exception: System.InvalidOperationException:
//       Sequence contains more than one matching element
//       at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
//       at Example.Main()
open System
open System.Linq

let dbQueryResults = [| 1; 2; 3; 4 |]

let singleObject = dbQueryResults.SingleOrDefault(fun value -> value > 2)

if singleObject <> 0 then
    printfn $"{singleObject} is the only value greater than 2"
else
    // Handle an empty collection.
    printfn "No value is greater than 2"
    
// The example displays the following output:
//    Unhandled Exception: System.InvalidOperationException:
//       Sequence contains more than one matching element
//       at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
//       at <StartupCode$fs>.main()
Imports System.Linq

Module Example
   Public Sub Main()
       Dim dbQueryResults() As Integer = { 1, 2, 3, 4 }
   
       Dim singleObject = dbQueryResults.SingleOrDefault(Function(value) value > 2)
   
       If singleObject <> 0 Then
           Console.WriteLine("{0} is the only value greater than 2", 
                             singleObject)
       Else
           ' Handle an empty collection.
           Console.WriteLine("No value is greater than 2")
       End If    
   End Sub
End Module
' The example displays the following output:
'    Unhandled Exception: System.InvalidOperationException: 
'       Sequence contains more than one matching element
'       at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
'       at Example.Main()

La chiamata al Enumerable.Single metodo presuppone che una sequenza o la sequenza che soddisfi i criteri specificati contenga un solo elemento. Enumerable.SingleOrDefault presuppone una sequenza con zero o un risultato, ma non più. Se questo presupposto è intenzionale da parte dell'utente e queste condizioni non vengono soddisfatte, rigenerare o intercettare il risultato InvalidOperationException è appropriato. In caso contrario, o se si prevede che si verifichino condizioni non valide con una certa frequenza, è consigliabile usare un altro Enumerable metodo, ad esempio FirstOrDefault o Where.

Accesso dinamico al dominio tra applicazioni

L'istruzione OpCodes.Ldflda MSIL (Microsoft Intermediate Language) genera un'eccezione InvalidOperationException se l'oggetto contenente il campo di cui si sta tentando di recuperare non si trova all'interno del dominio dell'applicazione in cui è in esecuzione il codice. È possibile accedere all'indirizzo di un campo solo dal dominio dell'applicazione in cui risiede.

Generazione di un'eccezione InvalidOperationException

È consigliabile generare un'eccezione InvalidOperationException solo quando lo stato dell'oggetto per qualche motivo non supporta una chiamata di metodo specifica. Ovvero, la chiamata al metodo è valida in alcune circostanze o contesti, ma non è valida in altri.

Se l'errore di chiamata al metodo è dovuto a argomenti non validi o ArgumentException a una delle relative classi ArgumentNullException derivate o ArgumentOutOfRangeException, deve essere generata invece .

Informazioni varie

InvalidOperationException usa il COR_E_INVALIDOPERATION HRESULT, che ha il valore 0x80131509.

Per un elenco di valori di proprietà iniziali per un'istanza di InvalidOperationException, vedere il InvalidOperationException costruttori.

Costruttori

InvalidOperationException()

Inizializza una nuova istanza della classe InvalidOperationException.

InvalidOperationException(SerializationInfo, StreamingContext)

Inizializza una nuova istanza della classe InvalidOperationException con dati serializzati.

InvalidOperationException(String)

Inizializza una nuova istanza della classe InvalidOperationException con un messaggio di errore specificato.

InvalidOperationException(String, Exception)

Inizializza una nuova istanza della classe InvalidOperationException con un messaggio di errore specificato e un riferimento all'eccezione interna che è la causa dell'eccezione corrente.

Proprietà

Data

Ottiene una raccolta di coppie chiave/valore che forniscono informazioni definite dall'utente aggiuntive sull'eccezione.

(Ereditato da Exception)
HelpLink

Ottiene o imposta un collegamento al file della Guida associato all'eccezione.

(Ereditato da Exception)
HResult

Ottiene o imposta HRESULT, un valore numerico codificato che viene assegnato a un'eccezione specifica.

(Ereditato da Exception)
InnerException

Ottiene l'istanza di Exception che ha causato l'eccezione corrente.

(Ereditato da Exception)
Message

Ottiene un messaggio che descrive l'eccezione corrente.

(Ereditato da Exception)
Source

Ottiene o imposta il nome dell'oggetto o dell'applicazione che ha generato l'errore.

(Ereditato da Exception)
StackTrace

Ottiene una rappresentazione di stringa dei frame immediati nello stack di chiamate.

(Ereditato da Exception)
TargetSite

Ottiene il metodo che genera l'eccezione corrente.

(Ereditato da Exception)

Metodi

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetBaseException()

Quando ne viene eseguito l'override in una classe derivata, restituisce l'Exception che è la causa radice di una o più eccezioni successive.

(Ereditato da Exception)
GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetObjectData(SerializationInfo, StreamingContext)

Quando ne viene eseguito l'override in una classe derivata, imposta il controllo SerializationInfo con le informazioni sull'eccezione.

(Ereditato da Exception)
GetType()

Ottiene il tipo di runtime dell'istanza corrente.

(Ereditato da Exception)
MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.

(Ereditato da Object)
ToString()

Crea e restituisce una rappresentazione di stringa dell'eccezione corrente.

(Ereditato da Exception)

Eventi

SerializeObjectState
Obsoleta.

Si verifica quando un'eccezione viene serializzata per creare un oggetto di stato eccezione contenente i dati serializzati relativi all'eccezione.

(Ereditato da Exception)

Si applica a

Vedi anche