-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Review delegate content in the C# programming guide (#44046)
* Move delegate samples * Fix the open issues - Add an example using an anonymouse method - Remove the references to C++ function pointers - Rewrite one heading * Proofread Refactor code, proofread and grammar check. Remove one example. It more or less repeated the previous full example. * Update docs/csharp/programming-guide/delegates/using-delegates.md Co-authored-by: Genevieve Warren <[email protected]> --------- Co-authored-by: Genevieve Warren <[email protected]>
- Loading branch information
1 parent
ffb3a5c
commit fd386d1
Showing
14 changed files
with
497 additions
and
735 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 5 additions & 10 deletions
15
...arp/programming-guide/delegates/how-to-combine-delegates-multicast-delegates.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 25 additions & 60 deletions
85
...rp/programming-guide/delegates/how-to-declare-instantiate-and-use-a-delegate.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
docs/csharp/programming-guide/delegates/snippets/BookStore.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
// A set of classes for handling a bookstore: | ||
namespace Bookstore; | ||
|
||
// Describes a book in the book list: | ||
public record struct Book(string Title, string Author, decimal Price, bool Paperback); | ||
|
||
// Declare a delegate type for processing a book: | ||
public delegate void ProcessBookCallback(Book book); | ||
|
||
// Maintains a book database. | ||
public class BookDB | ||
{ | ||
// List of all books in the database: | ||
List<Book> list = new(); | ||
|
||
// Add a book to the database: | ||
public void AddBook(string title, string author, decimal price, bool paperBack) => | ||
list.Add(new Book(title, author, price, paperBack)); | ||
|
||
// Call a passed-in delegate on each paperback book to process it: | ||
public void ProcessPaperbackBooks(ProcessBookCallback processBook) | ||
{ | ||
foreach (Book b in list) | ||
{ | ||
if (b.Paperback) | ||
{ | ||
// Calling the delegate: | ||
processBook(b); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Using the Bookstore classes: | ||
|
||
// Class to total and average prices of books: | ||
class PriceTotaller | ||
{ | ||
private int countBooks = 0; | ||
private decimal priceBooks = 0.0m; | ||
|
||
internal void AddBookToTotal(Book book) | ||
{ | ||
countBooks += 1; | ||
priceBooks += book.Price; | ||
} | ||
|
||
internal decimal AveragePrice() => priceBooks / countBooks; | ||
} | ||
|
||
// Class to test the book database: | ||
class Test | ||
{ | ||
// Print the title of the book. | ||
static void PrintTitle(Book b) => Console.WriteLine($" {b.Title}"); | ||
|
||
// Execution starts here. | ||
static void Main() | ||
{ | ||
BookDB bookDB = new BookDB(); | ||
|
||
// Initialize the database with some books: | ||
AddBooks(bookDB); | ||
|
||
// Print all the titles of paperbacks: | ||
Console.WriteLine("Paperback Book Titles:"); | ||
|
||
// Create a new delegate object associated with the static | ||
// method Test.PrintTitle: | ||
bookDB.ProcessPaperbackBooks(PrintTitle); | ||
|
||
// Get the average price of a paperback by using | ||
// a PriceTotaller object: | ||
PriceTotaller totaller = new PriceTotaller(); | ||
|
||
// Create a new delegate object associated with the nonstatic | ||
// method AddBookToTotal on the object totaller: | ||
bookDB.ProcessPaperbackBooks(totaller.AddBookToTotal); | ||
|
||
Console.WriteLine($"Average Paperback Book Price: ${totaller.AveragePrice():#.##}"); | ||
} | ||
|
||
// Initialize the book database with some test books: | ||
static void AddBooks(BookDB bookDB) | ||
{ | ||
bookDB.AddBook("The C Programming Language", "Brian W. Kernighan and Dennis M. Ritchie", 19.95m, true); | ||
bookDB.AddBook("The Unicode Standard 2.0", "The Unicode Consortium", 39.95m, true); | ||
bookDB.AddBook("The MS-DOS Encyclopedia", "Ray Duncan", 129.95m, false); | ||
bookDB.AddBook("Dogbert's Clues for the Clueless", "Scott Adams", 12.00m, true); | ||
} | ||
} | ||
/* Output: | ||
Paperback Book Titles: | ||
The C Programming Language | ||
The Unicode Standard 2.0 | ||
Dogbert's Clues for the Clueless | ||
Average Paperback Book Price: $23.97 | ||
*/ |
11 changes: 11 additions & 0 deletions
11
docs/csharp/programming-guide/delegates/snippets/DelegateExamples.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>true</ImplicitUsings> | ||
|
||
<StartupObject>Bookstore.Test</StartupObject> | ||
</PropertyGroup> | ||
</Project> |
48 changes: 48 additions & 0 deletions
48
docs/csharp/programming-guide/delegates/snippets/HowToDeclareAndUse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace DelegateExamples; | ||
public class HowToDeclareAndUse | ||
{ | ||
//<DeclareNamedDelegate> | ||
// Declare a delegate. | ||
delegate void NotifyCallback(string str); | ||
|
||
// Declare a method with the same signature as the delegate. | ||
static void Notify(string name) | ||
{ | ||
Console.WriteLine($"Notification received for: {name}"); | ||
} | ||
//</DeclareNamedDelegate> | ||
|
||
|
||
public static void Examples() | ||
{ | ||
//<CreateNamedInstance> | ||
// Create an instance of the delegate. | ||
NotifyCallback del1 = new NotifyCallback(Notify); | ||
//</CreateNamedInstance> | ||
|
||
//<MethodGroup> | ||
NotifyCallback del2 = Notify; | ||
//</MethodGroup> | ||
|
||
//<AnonymousMethod> | ||
// Instantiate NotifyCallback by using an anonymous method. | ||
NotifyCallback del3 = delegate (string name) | ||
{ | ||
Console.WriteLine($"Notification received for: {name}"); | ||
}; | ||
//</AnonymousMethod> | ||
|
||
//<LambdaExpression> | ||
// Instantiate NotifyCallback by using a lambda expression. | ||
NotifyCallback del4 = name => Console.WriteLine($"Notification received for: {name}"); | ||
//</LambdaExpression> | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.