From 348ba8f59152c2c6beeb28093e43f88d27452370 Mon Sep 17 00:00:00 2001 From: albert-du <52804499+albert-du@users.noreply.github.com> Date: Fri, 4 Mar 2022 19:09:19 -0800 Subject: [PATCH] Multicast Delegate F# snippet --- .../Overview/delegatestring.fs | 124 ++++++++++++++++++ .../MulticastDelegate/Overview/fs.fsproj | 10 ++ xml/System/MulticastDelegate.xml | 1 + 3 files changed, 135 insertions(+) create mode 100644 snippets/fsharp/System/MulticastDelegate/Overview/delegatestring.fs create mode 100644 snippets/fsharp/System/MulticastDelegate/Overview/fs.fsproj diff --git a/snippets/fsharp/System/MulticastDelegate/Overview/delegatestring.fs b/snippets/fsharp/System/MulticastDelegate/Overview/delegatestring.fs new file mode 100644 index 00000000000..87409a193a0 --- /dev/null +++ b/snippets/fsharp/System/MulticastDelegate/Overview/delegatestring.fs @@ -0,0 +1,124 @@ +// +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. +[] +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 +// \ No newline at end of file diff --git a/snippets/fsharp/System/MulticastDelegate/Overview/fs.fsproj b/snippets/fsharp/System/MulticastDelegate/Overview/fs.fsproj new file mode 100644 index 00000000000..5bf46eb82f9 --- /dev/null +++ b/snippets/fsharp/System/MulticastDelegate/Overview/fs.fsproj @@ -0,0 +1,10 @@ + + + Exe + net6.0 + + + + + + \ No newline at end of file diff --git a/xml/System/MulticastDelegate.xml b/xml/System/MulticastDelegate.xml index bb0fd13dbd1..1c102d1996c 100644 --- a/xml/System/MulticastDelegate.xml +++ b/xml/System/MulticastDelegate.xml @@ -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"::: ]]>