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

Skip to content

System.MulticastDelegate F# snippet #7794

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
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
124 changes: 124 additions & 0 deletions snippets/fsharp/System/MulticastDelegate/Overview/delegatestring.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// <Snippet1>
module Test

open System

// Define a delegate to handle string display.
type CheckAndDisplayDelegate = delegate of string -> unit

type StringContainer() =
// A generic ResizeArray object that holds the strings.
let container = ResizeArray()

// A method that adds strings to the collection.
member _.AddString(str) =
container.Add str

// Iterate through the strings and invoke the method(s) that the delegate points to.
member _.DisplayAllQualified(displayDelegate: CheckAndDisplayDelegate) =
for str in container do
displayDelegate.Invoke str

// This module defines some functions to display strings.
module StringExtensions =
// Display a string if it starts with a consonant.
let conStart (str: string) =
match str[0] with
| 'a' | 'e' | 'i' | 'o' | 'u' -> ()
| _ -> printfn $"{str}"

// Display a string if it starts with a vowel.
let vowelStart (str: string) =
match str[0] with
| 'a' | 'e' | 'i' | 'o' | 'u' -> printfn $"{str}"
| _ -> ()

// Demonstrate the use of delegates, including the Remove and
// Combine methods to create and modify delegate combinations.
[<EntryPoint>]
let main _ =
// Declare the StringContainer class and add some strings
let container = StringContainer()
container.AddString "This"
container.AddString "is"
container.AddString "a"
container.AddString "multicast"
container.AddString "delegate"
container.AddString "example"

// Create two delegates individually using different methods.
let conStart = CheckAndDisplayDelegate StringExtensions.conStart
let vowelStart = CheckAndDisplayDelegate StringExtensions.vowelStart

// Get the list of all delegates assigned to this MulticastDelegate instance.
let delegateList = conStart.GetInvocationList()
printfn $"conStart contains {delegateList.Length} delegate(s)."
let delegateList = vowelStart.GetInvocationList()
printfn $"vowelStart contains {delegateList.Length} delegate(s).\n"

// Determine whether the delegates are System.Multicast delegates.
if box conStart :? System.MulticastDelegate && box vowelStart :? System.MulticastDelegate then
printfn "conStart and vowelStart are derived from MulticastDelegate.\n"

// Execute the two delegates.
printfn "Executing the conStart delegate:"
container.DisplayAllQualified conStart
printfn "\nExecuting the vowelStart delegate:"
container.DisplayAllQualified vowelStart
printfn ""

// Create a new MulticastDelegate and call Combine to add two delegates.
let multipleDelegates =
Delegate.Combine(conStart, vowelStart) :?> CheckAndDisplayDelegate

// How many delegates does multipleDelegates contain?
let delegateList = multipleDelegates.GetInvocationList()
printfn $"\nmultipleDelegates contains {delegateList.Length} delegates.\n"

// Pass this multicast delegate to DisplayAllQualified.
printfn "Executing the multipleDelegate delegate."
container.DisplayAllQualified multipleDelegates

// Call remove and combine to change the contained delegates.
let multipleDelegates = Delegate.Remove(multipleDelegates, vowelStart) :?> CheckAndDisplayDelegate
let multipleDelegates = Delegate.Combine(multipleDelegates, conStart) :?> CheckAndDisplayDelegate

// Pass multipleDelegates to DisplayAllQualified again.
printfn "\nExecuting the multipleDelegate delegate with two conStart delegates:"
printfn $"{multipleDelegates}"
0
// The example displays the following output:
// conStart contains 1 delegate(s).
// vowelStart contains 1 delegate(s).
//
// conStart and vowelStart are derived from MulticastDelegate.
//
// Executing the conStart delegate:
// This
// multicast
// delegate
//
// Executing the vowelStart delegate:
// is
// a
// example
//
//
// multipleDelegates contains 2 delegates.
//
// Executing the multipleDelegate delegate.
// This
// is
// a
// multicast
// delegate
// example
//
// Executing the multipleDelegate delegate with two conStart delegates:
// This
// This
// multicast
// multicast
// delegate
// delegate
// </Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/MulticastDelegate/Overview/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="delegatestring.fs" />
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions xml/System/MulticastDelegate.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@

:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/Multicast Delegate Introduction/CPP/delegatestring.cpp" id="Snippet1":::
:::code language="csharp" source="~/snippets/csharp/System/MulticastDelegate/Overview/delegatestring.cs" interactive="try-dotnet" id="Snippet1":::
:::code language="fsharp" source="~/snippets/fsharp/System/MulticastDelegate/Overview/delegatestring.fs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/Multicast Delegate Introduction/VB/delegatestring.vb" id="Snippet1":::

]]></format>
Expand Down