diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~3/fs/Delegate.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~3/fs/Delegate.fs new file mode 100644 index 00000000000..95c2a5e5baa --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~3/fs/Delegate.fs @@ -0,0 +1,20 @@ +module Delegate + +// +type ExtractMethod = delegate of string * int -> string [] + +let extractWords (phrase: string) limit = + let delimiters = [| ' ' |] + if limit > 0 then + phrase.Split(delimiters, limit) + else + phrase.Split delimiters + +// Instantiate delegate to reference extractWords function +let extractMeth = ExtractMethod extractWords +let title = "The Scarlet Letter" + +// Use delegate instance to call extractWords function and display result +for word in extractMeth.Invoke(title, 5) do + printfn $"{word}" +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~3/fs/Example.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~3/fs/Example.fs new file mode 100644 index 00000000000..71be45cd8f1 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~3/fs/Example.fs @@ -0,0 +1,14 @@ +module Example + +// +open System +open System.Linq + +let predicate = Func(fun str index -> str.Length = index) + +let words = [ "orange"; "apple"; "Article"; "elephant"; "star"; "and" ] +let aWords = words.Where predicate + +for word in aWords do + printfn $"{word}" +// \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~3/fs/Func3.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~3/fs/Func3.fs new file mode 100644 index 00000000000..aa2cc56ada3 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~3/fs/Func3.fs @@ -0,0 +1,20 @@ +module Func3 + +// +open System + +let extractWords (phrase: string) limit = + let delimiters = [| ' ' |] + if limit > 0 then + phrase.Split(delimiters, limit) + else + phrase.Split delimiters + +// Instantiate delegate to reference extractWords function +let extractMethod = Func extractWords +let title = "The Scarlet Letter" + +// Use delegate instance to call extractWords function and display result +for word in extractMethod.Invoke(title, 5) do + printfn $"{word}" +// \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~3/fs/Lambda.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~3/fs/Lambda.fs new file mode 100644 index 00000000000..4f56da2fe4b --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~3/fs/Lambda.fs @@ -0,0 +1,20 @@ +module Lambda + +// +open System + +let separators = [| ' ' |] + +let extract = + Func (fun s i -> + if i > 0 then + s.Split(separators, i) + else + s.Split separators) + +let title = "The Scarlet Letter" + +// Use Func instance to call lambda expression and display result +for word in extract.Invoke(title, 5) do + printfn $"{word}" +// diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~3/fs/fs.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~3/fs/fs.fsproj new file mode 100644 index 00000000000..f2633e15899 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~3/fs/fs.fsproj @@ -0,0 +1,12 @@ + + + Exe + net6.0 + + + + + + + + \ No newline at end of file diff --git a/xml/System/Func`3.xml b/xml/System/Func`3.xml index 343f3e95d87..49663c23544 100644 --- a/xml/System/Func`3.xml +++ b/xml/System/Func`3.xml @@ -94,25 +94,28 @@ You can use this delegate to represent a method that can be passed as a parameter without explicitly declaring a custom delegate. The encapsulated method must correspond to the method signature that is defined by this delegate. This means that the encapsulated method must have two parameters, each of which is passed to it by value, and that it must return a value. > [!NOTE] -> To reference a method that has two parameters and returns `void` (or in Visual Basic, that is declared as a `Sub` rather than as a `Function`), use the generic delegate instead. +> To reference a method that has two parameters and returns `void` (`unit` in F#) (or in Visual Basic, that is declared as a `Sub` rather than as a `Function`), use the generic delegate instead. When you use the delegate you do not have to explicitly define a delegate that encapsulates a method with two parameters. For example, the following code explicitly declares a delegate named `ExtractMethod` and assigns a reference to the `ExtractWords` method to its delegate instance. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~3/cs/Delegate.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~3/fs/Delegate.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~3/vb/Delegate.vb" id="Snippet1"::: The following example simplifies this code by instantiating a delegate instead of explicitly defining a new delegate and assigning a named method to it. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~3/cs/Func3.cs" interactive="try-dotnet" id="Snippet2"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~3/fs/Func3.fs" id="Snippet2"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~3/vb/Func3.vb" id="Snippet2"::: You can use the delegate with anonymous methods in C#, as the following example illustrates. (For an introduction to anonymous methods, see [Anonymous Methods](/dotnet/csharp/programming-guide/statements-expressions-operators/anonymous-methods).) :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~3/cs/Anon.cs" interactive="try-dotnet" id="Snippet3"::: - You can also assign a lambda expression to a delegate, as the following example illustrates. (For an introduction to lambda expressions, see [Lambda Expressions](/dotnet/visual-basic/programming-guide/language-features/procedures/lambda-expressions) and [Lambda Expressions](/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions).) + You can also assign a lambda expression to a delegate, as the following example illustrates. (For an introduction to lambda expressions, see [Lambda Expressions (VB)](/dotnet/visual-basic/programming-guide/language-features/procedures/lambda-expressions), [Lambda Expressions (C#)](/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions) and [Lambda Expressions (F#)](/dotnet/fsharp/language-reference/functions/lambda-expressions-the-fun-keyword).) :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~3/cs/Lambda.cs" interactive="try-dotnet" id="Snippet4"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~3/fs/Lambda.fs" id="Snippet4"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~3/vb/lambda.vb" id="Snippet4"::: The underlying type of a lambda expression is one of the generic `Func` delegates. This makes it possible to pass a lambda expression as a parameter without explicitly assigning it to a delegate. In particular, because many methods of types in the namespace have parameters, you can pass these methods a lambda expression without explicitly instantiating a delegate. @@ -123,13 +126,16 @@ The following example demonstrates how to declare and use a delegate. This example declares a variable and assigns it a lambda expression that takes a value and an value as parameters. The lambda expression returns `true` if the length of the parameter is equal to the value of the parameter. The delegate that encapsulates this method is subsequently used in a query to filter strings in an array of strings. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~3/cs/Example.cs" interactive="try-dotnet" id="Snippet5"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~3/fs/Example.fs" id="Snippet5"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~3/vb/Example.vb" id="Snippet5"::: ]]> Lambda Expressions (C# Programming Guide) + Lambda Expressions: The fun Keyword (F#) Lambda Expressions Delegates (C# Programming Guide) + Delegates (F#) Delegates in Visual Basic