diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~2/fs/Delegate.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~2/fs/Delegate.fs new file mode 100644 index 00000000000..f9c79351608 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~2/fs/Delegate.fs @@ -0,0 +1,15 @@ +module Delegate + +// +type ConvertMethod = delegate of string -> string + +let uppercaseString (inputString: string) = + inputString.ToUpper() + +// Instantiate delegate to reference uppercaseString function +let convertMeth = ConvertMethod uppercaseString +let name = "Dakota" + +// Use delegate instance to call uppercaseString function +printfn $"{convertMeth.Invoke name}" +// \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~2/fs/Example.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~2/fs/Example.fs new file mode 100644 index 00000000000..199973fcf74 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~2/fs/Example.fs @@ -0,0 +1,26 @@ +module Example + +// +open System +open System.Linq + +// Declare a Func variable and assign a lambda expression to the +// variable. The function takes a string and converts it to uppercase. +let selector = Func(fun str -> str.ToUpper()) + +// Create a list of strings. +let words = [ "orange"; "apple"; "Article"; "elephant" ] + +// Query the list and select strings according to the selector function. +let aWords = words.Select selector + +// Output the results to the console. +for word in aWords do + printfn $"{word}" + +// This code example produces the following output: +// ORANGE +// APPLE +// ARTICLE +// ELEPHANT +// \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~2/fs/Func2_1.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~2/fs/Func2_1.fs new file mode 100644 index 00000000000..5de0626f422 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~2/fs/Func2_1.fs @@ -0,0 +1,18 @@ +module Func2_1 + +// +open System + +let uppercaseString (inputString: string) = + inputString.ToUpper() + +// Instantiate delegate to reference uppercaseString function +let convertMethod = Func uppercaseString +let name = "Dakota" + +// Use delegate instance to call uppercaseString function +printfn $"{convertMethod.Invoke name}" + +// This code example produces the following output: +// DAKOTA +// \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~2/fs/Lambda.fs b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~2/fs/Lambda.fs new file mode 100644 index 00000000000..5ff983b3bd6 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~2/fs/Lambda.fs @@ -0,0 +1,13 @@ +module Lambda + +// +open System + +let convert = Func(fun s -> s.ToUpper()) + +let name = "Dakota" +printfn $"{convert.Invoke name}" + +// This code example produces the following output: +// DAKOTA +// \ No newline at end of file diff --git a/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~2/fs/fs.fsproj b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~2/fs/fs.fsproj new file mode 100644 index 00000000000..8218aa06ee6 --- /dev/null +++ b/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~2/fs/fs.fsproj @@ -0,0 +1,12 @@ + + + Exe + net6.0 + + + + + + + + \ No newline at end of file diff --git a/xml/System/Func`2.xml b/xml/System/Func`2.xml index 278a5a75099..af05082dc46 100644 --- a/xml/System/Func`2.xml +++ b/xml/System/Func`2.xml @@ -91,20 +91,23 @@ When you use the delegate, you do not have to explicitly define a delegate that encapsulates a method with a single parameter. For example, the following code explicitly declares a delegate named `ConvertMethod` and assigns a reference to the `UppercaseString` method to its delegate instance. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~2/cs/Delegate.cs" interactive="try-dotnet" id="Snippet1"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~2/fs/Delegate.fs" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~2/vb/Delegate.vb" id="Snippet1"::: The following example simplifies this code by instantiating the 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~2/cs/Func2_1.cs" interactive="try-dotnet-method" id="Snippet2"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~2/fs/Func2_1.fs" id="Snippet2"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~2/vb/Func2_1.vb" id="Snippet2"::: You can also 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~2/cs/Anon.cs" interactive="try-dotnet-method" 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~2/cs/Lambda.cs" interactive="try-dotnet-method" id="Snippet4"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~2/fs/Lambda.fs" id="Snippet4"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~2/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. @@ -115,14 +118,17 @@ The following example demonstrates how to declare and use a delegate. This example declares a variable and assigns it a lambda expression that converts the characters in a string to uppercase. The delegate that encapsulates this method is subsequently passed to the method to change the strings in an array of strings to uppercase. :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~2/cs/Example.cs" interactive="try-dotnet-method" id="Snippet5"::: + :::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~2/fs/Example.fs" id="Snippet5"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~2/vb/Example.vb" id="Snippet5"::: :::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Func~2/cpp/Example.cpp" id="Snippet6"::: ]]> Lambda Expressions (C# Programming Guide) + Lambda Expressions: The fun Keyword (F#) Lambda Expressions (Visual Basic) Delegates (C# Programming Guide) + Delegates (F#) Delegates (Visual Basic)