Skip to content

Commit 9e47ac6

Browse files
authored
Func<T,TResult> F# snippets (#7611)
1 parent f2312fc commit 9e47ac6

File tree

6 files changed

+91
-1
lines changed

6 files changed

+91
-1
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Delegate
2+
3+
// <Snippet1>
4+
type ConvertMethod = delegate of string -> string
5+
6+
let uppercaseString (inputString: string) =
7+
inputString.ToUpper()
8+
9+
// Instantiate delegate to reference uppercaseString function
10+
let convertMeth = ConvertMethod uppercaseString
11+
let name = "Dakota"
12+
13+
// Use delegate instance to call uppercaseString function
14+
printfn $"{convertMeth.Invoke name}"
15+
// </Snippet1>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module Example
2+
3+
// <Snippet5>
4+
open System
5+
open System.Linq
6+
7+
// Declare a Func variable and assign a lambda expression to the
8+
// variable. The function takes a string and converts it to uppercase.
9+
let selector = Func<string, string>(fun str -> str.ToUpper())
10+
11+
// Create a list of strings.
12+
let words = [ "orange"; "apple"; "Article"; "elephant" ]
13+
14+
// Query the list and select strings according to the selector function.
15+
let aWords = words.Select selector
16+
17+
// Output the results to the console.
18+
for word in aWords do
19+
printfn $"{word}"
20+
21+
// This code example produces the following output:
22+
// ORANGE
23+
// APPLE
24+
// ARTICLE
25+
// ELEPHANT
26+
// </Snippet5>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Func2_1
2+
3+
// <Snippet2>
4+
open System
5+
6+
let uppercaseString (inputString: string) =
7+
inputString.ToUpper()
8+
9+
// Instantiate delegate to reference uppercaseString function
10+
let convertMethod = Func<string, string> uppercaseString
11+
let name = "Dakota"
12+
13+
// Use delegate instance to call uppercaseString function
14+
printfn $"{convertMethod.Invoke name}"
15+
16+
// This code example produces the following output:
17+
// DAKOTA
18+
// </Snippet2>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Lambda
2+
3+
// <Snippet4>
4+
open System
5+
6+
let convert = Func<string, string>(fun s -> s.ToUpper())
7+
8+
let name = "Dakota"
9+
printfn $"{convert.Invoke name}"
10+
11+
// This code example produces the following output:
12+
// DAKOTA
13+
// </Snippet4>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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="Func2_1.fs" />
9+
<Compile Include="Lambda.fs" />
10+
<Compile Include="Example.fs" />
11+
</ItemGroup>
12+
</Project>

xml/System/Func`2.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,23 @@
9191
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.
9292
9393
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~2/cs/Delegate.cs" interactive="try-dotnet" id="Snippet1":::
94+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~2/fs/Delegate.fs" id="Snippet1":::
9495
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~2/vb/Delegate.vb" id="Snippet1":::
9596
9697
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.
9798
9899
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~2/cs/Func2_1.cs" interactive="try-dotnet-method" id="Snippet2":::
100+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~2/fs/Func2_1.fs" id="Snippet2":::
99101
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~2/vb/Func2_1.vb" id="Snippet2":::
100102
101103
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).)
102104
103105
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~2/cs/Anon.cs" interactive="try-dotnet-method" id="Snippet3":::
104106
105-
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).)
107+
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).)
106108
107109
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~2/cs/Lambda.cs" interactive="try-dotnet-method" id="Snippet4":::
110+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~2/fs/Lambda.fs" id="Snippet4":::
108111
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~2/vb/Lambda.vb" id="Snippet4":::
109112
110113
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.
@@ -115,14 +118,17 @@
115118
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.
116119
117120
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Func~2/cs/Example.cs" interactive="try-dotnet-method" id="Snippet5":::
121+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.Func~2/fs/Example.fs" id="Snippet5":::
118122
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Func~2/vb/Example.vb" id="Snippet5":::
119123
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR_System/system.Func~2/cpp/Example.cpp" id="Snippet6":::
120124
121125
]]></format>
122126
</remarks>
123127
<related type="Article" href="/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions">Lambda Expressions (C# Programming Guide)</related>
128+
<related type="Article" href="/dotnet/fsharp/language-reference/functions/lambda-expressions-the-fun-keyword">Lambda Expressions: The fun Keyword (F#)</related>
124129
<related type="Article" href="/dotnet/visual-basic/programming-guide/language-features/procedures/lambda-expressions">Lambda Expressions (Visual Basic)</related>
125130
<related type="Article" href="/dotnet/csharp/programming-guide/delegates/">Delegates (C# Programming Guide)</related>
131+
<related type="Article" href="/dotnet/fsharp/language-reference/delegates/">Delegates (F#)</related>
126132
<related type="Article" href="/dotnet/visual-basic/programming-guide/language-features/delegates/">Delegates (Visual Basic)</related>
127133
</Docs>
128134
</Type>

0 commit comments

Comments
 (0)