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

Skip to content

System.Func<T1,T2,T3,T4,TResult> F# snippets #7623

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 24, 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,19 @@
module Delegate

// <Snippet1>
open System

type Searcher = delegate of (string * int * int * StringComparison) -> int

let title = "The House of the Seven Gables"
let finder = Searcher title.IndexOf
let mutable position = 0

while position > -1 do
let characters = title.Length - position
position <-
finder.Invoke("the", position, characters, StringComparison.InvariantCultureIgnoreCase)
if position >= 0 then
position <- position + 1
printfn $"'The' found at position {position} in {title}."
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Func5

// <Snippet2>
open System

let indexOf (s: string) s2 pos chars comparison =
s.IndexOf(s2, pos, chars, comparison)

let title = "The House of the Seven Gables"
let finder = Func<string, int, int, StringComparison, int>(indexOf title)
let mutable position = 0

while position > -1 do
let characters = title.Length - position
position <-
finder.Invoke("the", position, characters, StringComparison.InvariantCultureIgnoreCase)
if position >= 0 then
position <- position + 1
printfn $"'The' found at position {position} in {title}."
// </Snippet2>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Lambda

// <Snippet4>
open System

let title = "The House of the Seven Gables"

let finder =
Func<string, int, int, StringComparison, int>(fun s pos chars typ -> title.IndexOf(s, pos, chars, typ))

let mutable position = 0

while position > -1 do
let characters = title.Length - position
position <- finder.Invoke("the", position, characters, StringComparison.InvariantCultureIgnoreCase)

if position >= 0 then
position <- position + 1
printfn $"'The' found at position {position} in {title}."
// </Snippet4>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Delegate.fs" />
<Compile Include="Func5.fs" />
<Compile Include="Lambda.fs" />
</ItemGroup>
</Project>
10 changes: 8 additions & 2 deletions xml/System/Func`5.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,25 +110,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 four parameters, each of which is passed to it by value, and that it must return a value.

> [!NOTE]
> To reference a method that has four parameters and returns `void` (or in Visual Basic, that is declared as a `Sub` rather than as a `Function`), use the generic <xref:System.Action%604> delegate instead.
> To reference a method that has four 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 <xref:System.Action%604> delegate instead.

When you use the <xref:System.Func%605> delegate, you do not have to explicitly define a delegate that encapsulates a method with four parameters. For example, the following code explicitly declares a generic delegate named `Searcher` and assigns a reference to the <xref:System.String.IndexOf%2A> method to its delegate instance.

:::code language="csharp" source="~/samples/snippets/csharp/System/FuncT1,T2,T3,T4,TResult/Overview/Delegate.cs" id="Snippet1":::
:::code language="fsharp" source="~/samples/snippets/fsharp/System/FuncT1,T2,T3,T4,TResult/Overview/Delegate.fs" id="Snippet1":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~5/vb/Delegate.vb" id="Snippet1":::

The following example simplifies this code by instantiating the <xref:System.Func%605> delegate instead of explicitly defining a new delegate and assigning a named method to it.

:::code language="csharp" source="~/samples/snippets/csharp/System/FuncT1,T2,T3,T4,TResult/Overview/Func5.cs" id="Snippet2":::
:::code language="fsharp" source="~/samples/snippets/fsharp/System/FuncT1,T2,T3,T4,TResult/Overview/Func5.fs" id="Snippet2":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~5/vb/Func5.vb" id="Snippet2":::

You can use the <xref:System.Func%605> 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/System/FuncT1,T2,T3,T4,TResult/Overview/Anon.cs" id="Snippet3":::

You can also assign a lambda expression to a <xref:System.Func%605> 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%603> 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/System/FuncT1,T2,T3,T4,TResult/Overview/Lambda.cs" id="Snippet4":::
:::code language="fsharp" source="~/samples/snippets/fsharp/System/FuncT1,T2,T3,T4,TResult/Overview/Lambda.fs" id="Snippet4":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~5/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 `Func` parameters, you can pass these methods a lambda expression without explicitly instantiating a `Func` delegate.
Expand All @@ -139,13 +142,16 @@
The following example demonstrates how to declare and use a <xref:System.Func%603> delegate. This example declares a <xref:System.Func%603> variable and assigns it a lambda expression that takes a <xref:System.String> value and an <xref:System.Int32> value as parameters. The lambda expression returns `true` if the length of the <xref:System.String> parameter is equal to the value of the <xref:System.Int32> 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/System/FuncT1,T2,TResult/Overview/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":::

]]></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</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 in Visual Basic</related>
</Docs>
</Type>