diff --git a/snippets/fsharp/System/InvalidOperationException/Overview/Enumerable1.fs b/snippets/fsharp/System/InvalidOperationException/Overview/Enumerable1.fs
new file mode 100644
index 00000000000..e1816262e34
--- /dev/null
+++ b/snippets/fsharp/System/InvalidOperationException/Overview/Enumerable1.fs
@@ -0,0 +1,15 @@
+module Enumerable1
+
+//
+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 .main()
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/InvalidOperationException/Overview/Enumerable2.fs b/snippets/fsharp/System/InvalidOperationException/Overview/Enumerable2.fs
new file mode 100644
index 00000000000..2d1aafb7a9c
--- /dev/null
+++ b/snippets/fsharp/System/InvalidOperationException/Overview/Enumerable2.fs
@@ -0,0 +1,19 @@
+module Enumerable2
+
+//
+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.
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/InvalidOperationException/Overview/Enumerable3.fs b/snippets/fsharp/System/InvalidOperationException/Overview/Enumerable3.fs
new file mode 100644
index 00000000000..d1a99e18b08
--- /dev/null
+++ b/snippets/fsharp/System/InvalidOperationException/Overview/Enumerable3.fs
@@ -0,0 +1,18 @@
+module Enumerable3
+
+//
+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 .main()
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/InvalidOperationException/Overview/Enumerable4.fs b/snippets/fsharp/System/InvalidOperationException/Overview/Enumerable4.fs
new file mode 100644
index 00000000000..6a6b2dc5a97
--- /dev/null
+++ b/snippets/fsharp/System/InvalidOperationException/Overview/Enumerable4.fs
@@ -0,0 +1,18 @@
+module Enumerable4
+
+//
+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.
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/InvalidOperationException/Overview/Enumerable5.fs b/snippets/fsharp/System/InvalidOperationException/Overview/Enumerable5.fs
new file mode 100644
index 00000000000..59779225c81
--- /dev/null
+++ b/snippets/fsharp/System/InvalidOperationException/Overview/Enumerable5.fs
@@ -0,0 +1,19 @@
+module Enumerable5
+
+//
+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 .main()
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/InvalidOperationException/Overview/Enumerable6.fs b/snippets/fsharp/System/InvalidOperationException/Overview/Enumerable6.fs
new file mode 100644
index 00000000000..2e8b3f1244f
--- /dev/null
+++ b/snippets/fsharp/System/InvalidOperationException/Overview/Enumerable6.fs
@@ -0,0 +1,22 @@
+module Enumerable6
+
+//
+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 .main()
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/InvalidOperationException/Overview/Iterating1.fs b/snippets/fsharp/System/InvalidOperationException/Overview/Iterating1.fs
new file mode 100644
index 00000000000..26da82184be
--- /dev/null
+++ b/snippets/fsharp/System/InvalidOperationException/Overview/Iterating1.fs
@@ -0,0 +1,23 @@
+module Iterating1
+
+//
+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 .main()
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/InvalidOperationException/Overview/Iterating2.fs b/snippets/fsharp/System/InvalidOperationException/Overview/Iterating2.fs
new file mode 100644
index 00000000000..0a066c5c812
--- /dev/null
+++ b/snippets/fsharp/System/InvalidOperationException/Overview/Iterating2.fs
@@ -0,0 +1,37 @@
+module Iterating2
+
+//
+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
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/InvalidOperationException/Overview/Iterating3.fs b/snippets/fsharp/System/InvalidOperationException/Overview/Iterating3.fs
new file mode 100644
index 00000000000..aa58eb48f19
--- /dev/null
+++ b/snippets/fsharp/System/InvalidOperationException/Overview/Iterating3.fs
@@ -0,0 +1,25 @@
+module Iterating3
+
+//
+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 (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
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/InvalidOperationException/Overview/List_Sort1.fs b/snippets/fsharp/System/InvalidOperationException/Overview/List_Sort1.fs
new file mode 100644
index 00000000000..c963a34db54
--- /dev/null
+++ b/snippets/fsharp/System/InvalidOperationException/Overview/List_Sort1.fs
@@ -0,0 +1,27 @@
+module List_Sort1
+
+//
+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 .main()
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/InvalidOperationException/Overview/List_Sort2.fs b/snippets/fsharp/System/InvalidOperationException/Overview/List_Sort2.fs
new file mode 100644
index 00000000000..84df643b76e
--- /dev/null
+++ b/snippets/fsharp/System/InvalidOperationException/Overview/List_Sort2.fs
@@ -0,0 +1,24 @@
+module List_Sort2
+
+//
+open System
+
+type Person(firstName: string, lastName: string) =
+ member val FirstName = firstName with get, set
+ member val LastName = lastName with get, set
+
+ interface IComparable 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
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/InvalidOperationException/Overview/List_Sort3.fs b/snippets/fsharp/System/InvalidOperationException/Overview/List_Sort3.fs
new file mode 100644
index 00000000000..cba8d389026
--- /dev/null
+++ b/snippets/fsharp/System/InvalidOperationException/Overview/List_Sort3.fs
@@ -0,0 +1,26 @@
+module List_Sort3
+
+//
+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 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
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/InvalidOperationException/Overview/List_Sort4.fs b/snippets/fsharp/System/InvalidOperationException/Overview/List_Sort4.fs
new file mode 100644
index 00000000000..fcc7113db7f
--- /dev/null
+++ b/snippets/fsharp/System/InvalidOperationException/Overview/List_Sort4.fs
@@ -0,0 +1,25 @@
+module List_Sort4
+
+//
+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
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/InvalidOperationException/Overview/Nullable1.fs b/snippets/fsharp/System/InvalidOperationException/Overview/Nullable1.fs
new file mode 100644
index 00000000000..0703a1f7499
--- /dev/null
+++ b/snippets/fsharp/System/InvalidOperationException/Overview/Nullable1.fs
@@ -0,0 +1,21 @@
+module Nullable1
+
+//
+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.b__0(Nullable`1 nullableInt)
+// at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
+// at .main()
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/InvalidOperationException/Overview/Nullable2.fs b/snippets/fsharp/System/InvalidOperationException/Overview/Nullable2.fs
new file mode 100644
index 00000000000..687b360631d
--- /dev/null
+++ b/snippets/fsharp/System/InvalidOperationException/Overview/Nullable2.fs
@@ -0,0 +1,23 @@
+module Nullable2
+
+//
+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.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.GetValueOrDefault.
+for number in numbers2 do
+ printf $"{number} "
+printfn ""
+// The example displays the following output:
+// 1 2 0 4
+// 1 2 -1 4
+//
\ No newline at end of file
diff --git a/snippets/fsharp/System/InvalidOperationException/Overview/fs.fsproj b/snippets/fsharp/System/InvalidOperationException/Overview/fs.fsproj
new file mode 100644
index 00000000000..7ab52c69422
--- /dev/null
+++ b/snippets/fsharp/System/InvalidOperationException/Overview/fs.fsproj
@@ -0,0 +1,24 @@
+
+
+ Exe
+ net6.0
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/xml/System/InvalidOperationException.xml b/xml/System/InvalidOperationException.xml
index 357d825c397..d585ef7e77b 100644
--- a/xml/System/InvalidOperationException.xml
+++ b/xml/System/InvalidOperationException.xml
@@ -143,18 +143,20 @@ In each case, the `threadExampleBtn_Click` event handler calls the `DoSomeWork`
### Changing a collection while iterating it
-The `foreach` statement in C# or `For Each` statement in Visual Basic is used to iterate the members of a collection and to read or modify its individual elements. However, it can't be used to add or remove items from the collection. Doing this throws an exception with a message that is similar to, "**Collection was modified; enumeration operation may not execute.**"
+The `foreach` statement in C#, `for...in` in F#, or `For Each` statement in Visual Basic is used to iterate the members of a collection and to read or modify its individual elements. However, it can't be used to add or remove items from the collection. Doing this throws an exception with a message that is similar to, "**Collection was modified; enumeration operation may not execute.**"
The following example iterates a collection of integers attempts to add the square of each integer to the collection. The example throws an with the first call to the method.
:::code language="csharp" source="~/snippets/csharp/System/InvalidOperationException/Overview/Iterating1.cs" id="Snippet1":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/InvalidOperationException/Overview/Iterating1.fs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.invalidoperationexception/vb/Iterating1.vb" id="Snippet1":::
You can eliminate the exception in one of two ways, depending on your application logic:
-- If elements must be added to the collection while iterating it, you can iterate it by index using the `for` statement instead of `foreach` or `For Each`. The following example uses the for statement to add the square of numbers in the collection to the collection.
+- If elements must be added to the collection while iterating it, you can iterate it by index using the `for` (`for..to` in F#) statement instead of `foreach`, `for...in`, or `For Each`. The following example uses the for statement to add the square of numbers in the collection to the collection.
:::code language="csharp" source="~/snippets/csharp/System/InvalidOperationException/Overview/Iterating2.cs" interactive="try-dotnet" id="Snippet2":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/InvalidOperationException/Overview/Iterating2.fs" id="Snippet2":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.invalidoperationexception/vb/Iterating2.vb" id="Snippet2":::
Note that you must establish the number of iterations before iterating the collection either by using a counter inside the loop that will exit the loop appropriately, by iterating backward, from `Count` - 1 to 0, or, as the example does, by assigning the number of elements in the array to a variable and using it to establish the upper bound of the loop. Otherwise, if an element is added to the collection on every iteration, an endless loop results.
@@ -162,6 +164,7 @@ The `foreach` statement in C# or `For Each` statement in Visual Basic is used to
- If it is not necessary to add elements to the collection while iterating it, you can store the elements to be added in a temporary collection that you add when iterating the collection has finished. The following example uses this approach to add the square of numbers in a collection to a temporary collection, and then to combine the collections into a single array object.
:::code language="csharp" source="~/snippets/csharp/System/InvalidOperationException/Overview/Iterating3.cs" interactive="try-dotnet" id="Snippet3":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/InvalidOperationException/Overview/Iterating3.fs" id="Snippet3":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.invalidoperationexception/vb/Iterating3.vb" id="Snippet3":::
@@ -170,6 +173,7 @@ The `foreach` statement in C# or `For Each` statement in Visual Basic is used to
General-purpose sorting methods, such as the method or the method, usually require that at least one of the objects to be sorted implement the or the interface. If not, the collection or array cannot be sorted, and the method throws an exception. The following example defines a `Person` class, stores two `Person` objects in a generic object, and attempts to sort them. As the output from the example shows, the call to the method throws an .
:::code language="csharp" source="~/snippets/csharp/System/InvalidOperationException/Overview/List_Sort1.cs" id="Snippet12":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/InvalidOperationException/Overview/List_Sort1.fs" id="Snippet12":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.invalidoperationexception/vb/List_Sort1.vb" id="Snippet12":::
You can eliminate the exception in any of three ways:
@@ -179,6 +183,7 @@ General-purpose sorting methods, such as the implementation for the `Person` class. You can still call the collection or array's general sorting method and, as the output from the example shows, the collection sorts successfully.
:::code language="csharp" source="~/snippets/csharp/System/InvalidOperationException/Overview/List_Sort2.cs" interactive="try-dotnet" id="Snippet13":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/InvalidOperationException/Overview/List_Sort2.fs" id="Snippet13":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.invalidoperationexception/vb/List_Sort2.vb" id="Snippet13":::
- If you cannot modify the source code for the type you are trying to sort, you can define a special-purpose sorting class that implements the interface. You can call an overload of the `Sort` method that includes an parameter. This approach is especially useful if you want to develop a specialized sorting class that can sort objects based on multiple criteria.
@@ -186,6 +191,7 @@ General-purpose sorting methods, such as the method.
:::code language="csharp" source="~/snippets/csharp/System/InvalidOperationException/Overview/List_Sort3.cs" interactive="try-dotnet" id="Snippet14":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/InvalidOperationException/Overview/List_Sort3.fs" id="Snippet14":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.invalidoperationexception/vb/List_Sort3.vb" id="Snippet14":::
- If you cannot modify the source code for the type you are trying to sort, you can create a delegate to perform the sorting. The delegate signature is
@@ -201,6 +207,7 @@ General-purpose sorting methods, such as the delegate signature. It then passes this delegate to the method.
:::code language="csharp" source="~/snippets/csharp/System/InvalidOperationException/Overview/List_Sort4.cs" interactive="try-dotnet" id="Snippet15":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/InvalidOperationException/Overview/List_Sort4.fs" id="Snippet15":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.invalidoperationexception/vb/List_Sort4.vb" id="Snippet15":::
@@ -211,6 +218,7 @@ Attempting to cast a value that is `null` to its unde
The following example throws an exception when it attempts to iterate an array that includes a `Nullable(Of Integer)` value.
:::code language="csharp" source="~/snippets/csharp/System/InvalidOperationException/Overview/Nullable1.cs" id="Snippet4":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/InvalidOperationException/Overview/Nullable1.fs" id="Snippet4":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.invalidoperationexception/vb/Nullable1.vb" id="Snippet4":::
To prevent the exception:
@@ -222,6 +230,7 @@ Attempting to cast a value that is `null` to its unde
The following example does both to avoid the exception.
:::code language="csharp" source="~/snippets/csharp/System/InvalidOperationException/Overview/Nullable2.cs" interactive="try-dotnet" id="Snippet5":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/InvalidOperationException/Overview/Nullable2.fs" id="Snippet5":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.invalidoperationexception/vb/Nullable2.vb" id="Snippet5":::
@@ -256,11 +265,13 @@ The , method to compute the average of a sequence whose values are greater than 4. Since no values from the original array exceed 4, no values are included in the sequence, and the method throws an exception.
:::code language="csharp" source="~/snippets/csharp/System/InvalidOperationException/Overview/Enumerable1.cs" id="Snippet6":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/InvalidOperationException/Overview/Enumerable1.fs" id="Snippet6":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.invalidoperationexception/vb/Enumerable1.vb" id="Snippet6":::
The exception can be eliminated by calling the method to determine whether the sequence contains any elements before calling the method that processes the sequence, as the following example shows.
:::code language="csharp" source="~/snippets/csharp/System/InvalidOperationException/Overview/Enumerable2.cs" interactive="try-dotnet" id="Snippet7":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/InvalidOperationException/Overview/Enumerable2.fs" id="Snippet7":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.invalidoperationexception/vb/Enumerable2.vb" id="Snippet7":::
The method returns the first item in a sequence or the first element in a sequence that satisfies a specified condition. If the sequence is empty and therefore does not have a first element, it throws an exception.
@@ -268,6 +279,7 @@ The , method throws an exception because the dbQueryResults array doesn't contain an element greater than 4.
:::code language="csharp" source="~/snippets/csharp/System/InvalidOperationException/Overview/Enumerable3.cs" id="Snippet8":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/InvalidOperationException/Overview/Enumerable3.fs" id="Snippet8":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.invalidoperationexception/vb/Enumerable3.vb" id="Snippet8":::
You can call the method instead of to return a specified or default value. If the method does not find a first element in the sequence, it returns the default value for that data type. The default value is `null` for a reference type, zero for a numeric data type, and for the type.
@@ -278,6 +290,7 @@ The , method to prevent the exception thrown in the previous example.
:::code language="csharp" source="~/snippets/csharp/System/InvalidOperationException/Overview/Enumerable4.cs" interactive="try-dotnet" id="Snippet9":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/InvalidOperationException/Overview/Enumerable4.fs" id="Snippet9":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.invalidoperationexception/vb/Enumerable4.vb" id="Snippet9":::
@@ -297,11 +310,13 @@ The method
In the following example, the call to the method throws an exception because the sequence doesn't have an element greater than 4.
:::code language="csharp" source="~/snippets/csharp/System/InvalidOperationException/Overview/Enumerable5.cs" id="Snippet10":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/InvalidOperationException/Overview/Enumerable5.fs" id="Snippet10":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.invalidoperationexception/vb/Enumerable5.vb" id="Snippet10":::
The following example attempts to prevent the exception thrown when a sequence is empty by instead calling the method. However, because this sequence returns multiple elements whose value is greater than 2, it also throws an exception.
:::code language="csharp" source="~/snippets/csharp/System/InvalidOperationException/Overview/Enumerable6.cs" id="Snippet11":::
+ :::code language="fsharp" source="~/snippets/fsharp/System/InvalidOperationException/Overview/Enumerable6.fs" id="Snippet11":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.invalidoperationexception/vb/Enumerable6.vb" id="Snippet11":::
Calling the method assumes that either a sequence or the sequence that meets specified criteria contains only one element. assumes a sequence with zero or one result, but no more. If this assumption is a deliberate one on your part and these conditions are not met, rethrowing or catching the resulting is appropriate. Otherwise, or if you expect that invalid conditions will occur with some frequency, you should consider using some other method, such as or .