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

Skip to content

Commit 9a6f72e

Browse files
authored
Func<T1,T2,T3,T4,TResult> F# snippets (#7623)
1 parent fcbb191 commit 9a6f72e

File tree

5 files changed

+78
-2
lines changed

5 files changed

+78
-2
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Delegate
2+
3+
// <Snippet1>
4+
open System
5+
6+
type Searcher = delegate of (string * int * int * StringComparison) -> int
7+
8+
let title = "The House of the Seven Gables"
9+
let finder = Searcher title.IndexOf
10+
let mutable position = 0
11+
12+
while position > -1 do
13+
let characters = title.Length - position
14+
position <-
15+
finder.Invoke("the", position, characters, StringComparison.InvariantCultureIgnoreCase)
16+
if position >= 0 then
17+
position <- position + 1
18+
printfn $"'The' found at position {position} in {title}."
19+
// </Snippet1>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Func5
2+
3+
// <Snippet2>
4+
open System
5+
6+
let indexOf (s: string) s2 pos chars comparison =
7+
s.IndexOf(s2, pos, chars, comparison)
8+
9+
let title = "The House of the Seven Gables"
10+
let finder = Func<string, int, int, StringComparison, int>(indexOf title)
11+
let mutable position = 0
12+
13+
while position > -1 do
14+
let characters = title.Length - position
15+
position <-
16+
finder.Invoke("the", position, characters, StringComparison.InvariantCultureIgnoreCase)
17+
if position >= 0 then
18+
position <- position + 1
19+
printfn $"'The' found at position {position} in {title}."
20+
// </Snippet2>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Lambda
2+
3+
// <Snippet4>
4+
open System
5+
6+
let title = "The House of the Seven Gables"
7+
8+
let finder =
9+
Func<string, int, int, StringComparison, int>(fun s pos chars typ -> title.IndexOf(s, pos, chars, typ))
10+
11+
let mutable position = 0
12+
13+
while position > -1 do
14+
let characters = title.Length - position
15+
position <- finder.Invoke("the", position, characters, StringComparison.InvariantCultureIgnoreCase)
16+
17+
if position >= 0 then
18+
position <- position + 1
19+
printfn $"'The' found at position {position} in {title}."
20+
// </Snippet4>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="Delegate.fs" />
8+
<Compile Include="Func5.fs" />
9+
<Compile Include="Lambda.fs" />
10+
</ItemGroup>
11+
</Project>

xml/System/Func`5.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,25 +110,28 @@
110110
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.
111111
112112
> [!NOTE]
113-
> 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.
113+
> 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.
114114
115115
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.
116116
117117
:::code language="csharp" source="~/samples/snippets/csharp/System/FuncT1,T2,T3,T4,TResult/Overview/Delegate.cs" id="Snippet1":::
118+
:::code language="fsharp" source="~/samples/snippets/fsharp/System/FuncT1,T2,T3,T4,TResult/Overview/Delegate.fs" id="Snippet1":::
118119
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~5/vb/Delegate.vb" id="Snippet1":::
119120
120121
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.
121122
122123
:::code language="csharp" source="~/samples/snippets/csharp/System/FuncT1,T2,T3,T4,TResult/Overview/Func5.cs" id="Snippet2":::
124+
:::code language="fsharp" source="~/samples/snippets/fsharp/System/FuncT1,T2,T3,T4,TResult/Overview/Func5.fs" id="Snippet2":::
123125
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~5/vb/Func5.vb" id="Snippet2":::
124126
125127
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).)
126128
127129
:::code language="csharp" source="~/samples/snippets/csharp/System/FuncT1,T2,T3,T4,TResult/Overview/Anon.cs" id="Snippet3":::
128130
129-
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).)
131+
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).)
130132
131133
:::code language="csharp" source="~/samples/snippets/csharp/System/FuncT1,T2,T3,T4,TResult/Overview/Lambda.cs" id="Snippet4":::
134+
:::code language="fsharp" source="~/samples/snippets/fsharp/System/FuncT1,T2,T3,T4,TResult/Overview/Lambda.fs" id="Snippet4":::
132135
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~5/vb/Lambda.vb" id="Snippet4":::
133136
134137
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.
@@ -139,13 +142,16 @@
139142
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.
140143
141144
:::code language="csharp" source="~/samples/snippets/csharp/System/FuncT1,T2,TResult/Overview/Example.cs" interactive="try-dotnet" id="Snippet5":::
145+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~3/fs/Example.fs" id="Snippet5":::
142146
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~3/vb/Example.vb" id="Snippet5":::
143147
144148
]]></format>
145149
</remarks>
146150
<related type="Article" href="/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions">Lambda Expressions (C# Programming Guide)</related>
151+
<related type="Article" href="/dotnet/fsharp/language-reference/functions/lambda-expressions-the-fun-keyword">Lambda Expressions: The fun Keyword (F#)</related>
147152
<related type="Article" href="/dotnet/visual-basic/programming-guide/language-features/procedures/lambda-expressions">Lambda Expressions</related>
148153
<related type="Article" href="/dotnet/csharp/programming-guide/delegates/">Delegates (C# Programming Guide)</related>
154+
<related type="Article" href="/dotnet/fsharp/language-reference/delegates/">Delegates (F#)</related>
149155
<related type="Article" href="/dotnet/visual-basic/programming-guide/language-features/delegates/">Delegates in Visual Basic</related>
150156
</Docs>
151157
</Type>

0 commit comments

Comments
 (0)