Understanding for C #

xiaoxiao2021-03-06  113

When reading a C #, I finally saw the class. I saw the class event. The book was very simple. It didn't understand it. It's no wonder that the C foundation is not strong. I think so many kinds of classes in the object program, I have an incident in the middle, entrust such a strange concept, and naturally read a fogwater. So I checked MSDN, saying that I first see the entrustment - have a few experiences, trust MSDN, first go to MSDN, I didn't expect to see it is an afternoon. Let's write your own understanding, it is not to be inspired for others, just for your own small one-step record:

The introduction of the secondary, MSDN is the entrusted: C # commissioned in C or C function pointers in C or C . Use the entrustment to make the programmer to encapsulate the method within the delegate object. You can then pass the delegate object to the code that can be called, without having to know which method will be called when compiling. Unlike the function pointers in C or C , the delegate is object-oriented, safe, and is safe.

The delegate declaration defines a type that uses a specific parameter and the return type package method. For static methods, the method of delegate the object package to be called. For example methods, the delegate object simultaneously encapsulates an example and a method on this instance. If you have a delegate object and a set of appropriate parameters, you can call the delegate with these parameters.

A interesting and useful attribute entrusted is that it does not know or not care about the object of the object you reference. Any object can be; just the parameter type and return type of the method must match the parameter type and return type of the delegate. This makes the commission fully suitable for "anonymous" calls. To be honest, I still don't fully understand these words, now I know is just: commissioning similar to the function pointer. Below this, combine an example given by MSDN to talk about your own analysis and understanding. After the example, the analysis is as follows: In this example, the following entities are defined:

Structural BOOK, indicating a book information; class bookdb, represents the book's database class prerespondotaller to calculate the total price and average price class TEST of the book in the database, for testing the principal of this concept, ProcityBookdeLegate I also know from it, it is also known to handle a book (price, of course, there is a more powerful function)

Their role, I think it is in this sentence: BookDb.ProcessPaperbackBooks (New ProcessBookDelegate (Totaller.AddbooktotAl); I don't really get in touch with object-oriented programming, reading this sentence is really hard, I tried This sentence decomposition: ProcessBookDelegate PBD = New ProcessBookDelegate

PBD (Totaller.Addbooktototal);

BookDb.ProcessPaperbackBooks (PBD);

The above is pseudo code, three lines represent three decomposition action: Create a delegate, the method of associating the class TOTALLER (here is the instance Totaller) method addbooktototal (), database bookDB to delegate PBACKBOOKS (). Database BookDB Method ProcessPaperbackBooks () Simply calls PrcessBook, and passed to one parameter: Book structure B in the database. ProcessBook () is the method of TOTALLER (actually actually totaller), and proceeds to execute this method and passes the parameter B to it. In this way, actually "work" is the method of Totaller (), and the originality of the program is the method of class BookDB call class Totaller addbooktototal (), and passed to it a parameter b. ProcessBookDelegate is just a "commission" role. Public void processpaperbackbooks (processbookdelegate processbook) {foreach (book b in list) {if (b.paperback) // Calling the delegate: ProcessBook (b);}} Why do you do this? MSDN gives a bunch of reasons:

Why do you do this? MSDN gives a bunch of reasons:

{

Foreach (Book B in List)

{

IF (B.PaperBack)

// Calling the delegate:

ProcessBook (b);

}

}

Why do you do this? MSDN gives a bunch of reasons:

Entrust it in the following cases:

· Call a single method

· A class may wish to have a number of implementations that have a method specification.

· I hope to allow the use of static methods to implement specifications.

· I hope that similar events are designed (for more information, see Event Tutorial).

· The caller does not need to know or obtain an object to define the method there.

· The implementation of the implementation is only to implement only the "distribution" specification for the minority component.

· It takes a convenient combination.

As usual, I didn't fully understand it at all, and the purpose of this article is to explain the work method of the commission. There is no more painting here.

Talk about your own understanding on the other two points mentioned in MSDN:

The delegate declaration defines a type that uses a specific parameter and the return type package method. For static methods, the method of delegate the object package to be called. For example methods, the delegate object simultaneously encapsulates an example and a method on this instance. If you have a delegate object and a set of appropriate parameters, you can call the delegate with these parameters.

The above mentioned only a method of entrusting a way, the PBD is actually associated with an addbooktototal () method on an instance Totaller on a specific class. I think this is the MSDN refers to the "entrusted object simultaneously encapsulates an example and one method on this instance." In fact, the commission can also be directly associated with a certain method without having to create a specific instance, such as:

BookDb.ProcessPaperbackBookBookBooks (PRINTTISSBOOKDELEGATE (PrintTitle)); this is probably the "method for static methods, entrusted object package to be called". In fact, I think this sentence is better, if the entrusted object is to directly encapsulate the method to be called, the method must be a way. A interesting and useful attribute entrusted is that it does not know or not care about the object of the object you reference. Any object can be; just the parameter type and return type of the method must match the parameter type and return type of the delegate. This makes the commission fully suitable for "anonymous" calls.

This paragraph is to say that after a commission, you can use this delegate to reference (here "reference", the concept of different, non-aligned mechanisms. I think it is better to call more than any other method, And don't have to take this class to have any relationship with yourself. Of course, the premise is that the parameters and return types of this method match the parameters and return types of the delegate itself.

Source program:

// BookStore.cs

Using system;

// a set of classes for Handling A Bookstore:

Namespace bookstore

{

Using system.collections;

// Describes a book in The Book List:

Public struct book

{

Public String Title; // Title of The Book.

Public String Author; // Author of the book.

Public Decimal Price; // Price of the Book.

Public bool paperback; // is it paperback?

Public book (String Title, String Author, Decimal Price, Bool Paperback)

{

Title = Title;

Author = author;

Price = price;

Paperback = paperback;

}

}

// Declare a delegate Type for processing a book:

Public Delegate Void ProcessBookDelegate (Book book);

// maintains a book data.

Public class bookdb

{

// List of all books in the database:

ArrayList List = New ArrayList ();

// 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 (ProcessBookDelegate ProcessBook)

{

Foreach (Book B in List)

{

IF (B.PaperBack)

// Calling the delegate:

ProcessBook (b);

}

}

}

}

// using the bookstore classes:

Namespace booktestclient {

Using bookstore;

// Class to Total and Average prices of books:

Class PrerespondoTotaller

{

INT countryBooks = 0;

Decimal pricebooks =

0.0m

;

Internal Void AddbooktotAl (Book Book)

{

Countbooks = 1;

PriceBooks = BOOK.PRICE;

}

INTERNAL DECIMAL AVERAGEPRICE ()

{

Return Pricebooks / CountBooks and COUNTBOOks;

}

}

// Class to Test The Book Database:

Class test

{

// Print The title of the book.

Static void PrinTtitle (Book B)

{

Console.writeline ("{0}", b.title);

}

// Execution starts here.

Static void main ()

{

BookDB boxdb = 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.ProcessPaperbackBookBookBookBooks (New ProcessBookDelegate (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 Addbooktotal on The Object Totaller:

BookDb.ProcessPaperbackBookBooks (New ProcessBookDELEGATE (Totaller.Addbooktotal);

Console.Writeline ("Average Paperback Book Price: $ {0: #. ##}",

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

转载请注明原文地址:https://www.9cbs.com/read-102789.html

New Post(0)