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

Skip to content

Commit fcbb191

Browse files
authored
Func<T1,T2,T3,TResult> F# snippets (#7622)
1 parent 396f86c commit fcbb191

File tree

5 files changed

+70
-2
lines changed

5 files changed

+70
-2
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module Delegate
2+
3+
// <Snippet1>
4+
open System
5+
open System.Globalization
6+
7+
type ParseNumber<'T> = delegate of (string * NumberStyles * IFormatProvider) -> 'T
8+
9+
let numericString = "-1,234"
10+
let parser = ParseNumber<int> Int32.Parse
11+
12+
parser.Invoke(
13+
numericString,
14+
NumberStyles.Integer ||| NumberStyles.AllowThousands,
15+
CultureInfo.InvariantCulture )
16+
|> printfn "%i"
17+
// </Snippet1>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Func4
2+
3+
// <Snippet2>
4+
open System
5+
open System.Globalization
6+
7+
let parseInt (str: string) styles format = Int32.Parse(str, styles, format)
8+
9+
let numericString = "-1,234"
10+
11+
let parser =
12+
Func<string, NumberStyles, IFormatProvider, int> parseInt
13+
14+
parser.Invoke(
15+
numericString,
16+
NumberStyles.Integer ||| NumberStyles.AllowThousands,
17+
CultureInfo.InvariantCulture )
18+
|> printfn "%i"
19+
// </Snippet2>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Lambda
2+
3+
// <Snippet4>
4+
open System
5+
open System.Globalization
6+
7+
let numericString = "-1,234"
8+
let parser = Func<string, NumberStyles, IFormatProvider, int>(fun s sty p ->
9+
Int32.Parse(s, sty, p))
10+
11+
parser.Invoke(numericString,
12+
NumberStyles.Integer ||| NumberStyles.AllowThousands,
13+
CultureInfo.InvariantCulture)
14+
|> printfn "%i"
15+
// </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="Func4.fs" />
9+
<Compile Include="Lambda.fs" />
10+
</ItemGroup>
11+
</Project>

xml/System/Func`4.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,25 +102,28 @@
102102
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 three parameters, each of which is passed to it by value, and that it must return a value.
103103
104104
> [!NOTE]
105-
> To reference a method that has three 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%603> delegate instead.
105+
> To reference a method that has three 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%603> delegate instead.
106106
107107
When you use the <xref:System.Func%604> delegate, you do not have to explicitly define a delegate that encapsulates a method with three parameters. For example, the following code explicitly declares a generic delegate named `ParseNumber` and assigns a reference to the <xref:System.Int32.Parse%2A> method to its delegate instance.
108108
109109
:::code language="csharp" source="~/samples/snippets/csharp/System/FuncT1,T2,T3,TResult/Overview/Delegate.cs" interactive="try-dotnet" id="Snippet1":::
110+
:::code language="fsharp" source="~/samples/snippets/fsharp/System/FuncT1,T2,T3,TResult/Overview/Delegate.fs" id="Snippet1":::
110111
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~4/vb/Delegate.vb" id="Snippet1":::
111112
112113
The following example simplifies this code by instantiating the <xref:System.Func%604> delegate instead of explicitly defining a new delegate and assigning a named method to it.
113114
114115
:::code language="csharp" source="~/samples/snippets/csharp/System/FuncT1,T2,T3,TResult/Overview/Func4.cs" interactive="try-dotnet" id="Snippet2":::
116+
:::code language="fsharp" source="~/samples/snippets/fsharp/System/FuncT1,T2,T3,TResult/Overview/Func4.fs" id="Snippet2":::
115117
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~4/vb/Func4.vb" id="Snippet2":::
116118
117119
You can use the <xref:System.Func%604> 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).)
118120
119121
:::code language="csharp" source="~/samples/snippets/csharp/System/FuncT1,T2,T3,TResult/Overview/Anon.cs" interactive="try-dotnet" id="Snippet3":::
120122
121-
You can also assign a lambda expression to a <xref:System.Func%604> 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).)
123+
You can also assign a lambda expression to a <xref:System.Func%604> 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).)
122124
123125
:::code language="csharp" source="~/samples/snippets/csharp/System/FuncT1,T2,T3,TResult/Overview/Lambda.cs" interactive="try-dotnet" id="Snippet4":::
126+
:::code language="fsharp" source="~/samples/snippets/fsharp/System/FuncT1,T2,T3,TResult/Overview/Lambda.fs" id="Snippet4":::
124127
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~4/vb/Lambda.vb" id="Snippet4":::
125128
126129
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.
@@ -131,13 +134,16 @@
131134
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.
132135
133136
:::code language="csharp" source="~/samples/snippets/csharp/System/FuncT1,T2,TResult/Overview/Example.cs" interactive="try-dotnet" id="Snippet5":::
137+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~3/fs/Example.fs" id="Snippet5":::
134138
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~3/vb/Example.vb" id="Snippet5":::
135139
136140
]]></format>
137141
</remarks>
138142
<related type="Article" href="/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions">Lambda Expressions (C# Programming Guide)</related>
139143
<related type="Article" href="/dotnet/visual-basic/programming-guide/language-features/procedures/lambda-expressions">Lambda Expressions</related>
144+
<related type="Article" href="/dotnet/fsharp/language-reference/functions/lambda-expressions-the-fun-keyword">Lambda Expressions: The fun Keyword (F#)</related>
140145
<related type="Article" href="/dotnet/csharp/programming-guide/delegates/">Delegates (C# Programming Guide)</related>
146+
<related type="Article" href="/dotnet/fsharp/language-reference/delegates/">Delegates (F#)</related>
141147
<related type="Article" href="/dotnet/visual-basic/programming-guide/language-features/delegates/">Delegates in Visual Basic</related>
142148
</Docs>
143149
</Type>

0 commit comments

Comments
 (0)