Thanks to visit codestin.com
Credit goes to github.com

Skip to content

System.Func<T,TResult> F# snippets #7611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Delegate

// <Snippet1>
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}"
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Example

// <Snippet5>
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<string, string>(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
// </Snippet5>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Func2_1

// <Snippet2>
open System

let uppercaseString (inputString: string) =
inputString.ToUpper()

// Instantiate delegate to reference uppercaseString function
let convertMethod = Func<string, string> uppercaseString
let name = "Dakota"

// Use delegate instance to call uppercaseString function
printfn $"{convertMethod.Invoke name}"

// This code example produces the following output:
// DAKOTA
// </Snippet2>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Lambda

// <Snippet4>
open System

let convert = Func<string, string>(fun s -> s.ToUpper())

let name = "Dakota"
printfn $"{convert.Invoke name}"

// This code example produces the following output:
// DAKOTA
// </Snippet4>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Delegate.fs" />
<Compile Include="Func2_1.fs" />
<Compile Include="Lambda.fs" />
<Compile Include="Example.fs" />
</ItemGroup>
</Project>
8 changes: 7 additions & 1 deletion xml/System/Func`2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,23 @@
When you use the <xref:System.Func%602> 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 <xref:System.Func%602> 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 <xref:System.Func%602> 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 <xref:System.Func%602> 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 <xref:System.Func%602> 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 <xref:System.Linq> namespace have <xref:System.Func%602> parameters, you can pass these methods a lambda expression without explicitly instantiating a <xref:System.Func%602> delegate.
Expand All @@ -115,14 +118,17 @@
The following example demonstrates how to declare and use a <xref:System.Func%602> delegate. This example declares a <xref:System.Func%602> 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 <xref:System.Linq.Enumerable.Select%2A?displayProperty=nameWithType> 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":::

]]></format>
</remarks>
<related type="Article" href="/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions">Lambda Expressions (C# Programming Guide)</related>
<related type="Article" href="/dotnet/fsharp/language-reference/functions/lambda-expressions-the-fun-keyword">Lambda Expressions: The fun Keyword (F#)</related>
<related type="Article" href="/dotnet/visual-basic/programming-guide/language-features/procedures/lambda-expressions">Lambda Expressions (Visual Basic)</related>
<related type="Article" href="/dotnet/csharp/programming-guide/delegates/">Delegates (C# Programming Guide)</related>
<related type="Article" href="/dotnet/fsharp/language-reference/delegates/">Delegates (F#)</related>
<related type="Article" href="/dotnet/visual-basic/programming-guide/language-features/delegates/">Delegates (Visual Basic)</related>
</Docs>
</Type>