C # Delegates, Events, and Lambda Expr for New Students

My pointed chef asked me to train our new cooperatives (trainees) in C # methods. I have about ~ 30 minutes to cover the topics of delegates, events, and lambda expressions. The time limit is quite strict, and the topics are wide. Since I am not a C # guru, I need some tips and pointers.

  • Since it is my short time, what points should be made with respect to the three topics listed above?
  • What good is Do and Dont when using these three things?
  • I might have time for a short demonstration of the Lambda Expr. What is the most common use of LExpr (perhaps a Select (). Where () clause for an enumeration?) That I could demonstrate?

Thanks.

EDITOR: Students have knowledge of C ++ and Java.

+4
source share
3 answers

If your boss is serious about this, at least ask for one 30-minute session for each topic . This will give you time to introduce it (very briefly), give them a quick demonstration, and possibly answer a few questions. After that, they need to give time (or give them homework;)).

How long should you prepare for this? As mentioned in an Anthony Pegram comment , it would be helpful for you to introduce these topics through the Jon Skeet book C # In Depth, 2nd Edition . You can always get an e-book if you do not have time to get a hard copy (I would recommend getting a paper copy, because this is a great reference and a coupon for the free version of the book is included).

In the book, he describes the evolution of C #. For example, it spans the evolution from explicit delegates to built-in delegate actions with anonymous methods for lambda expressions as delegates (part of the evolution to make LINQ a reality).

I would recommend reading the following:

  • chapter 2

    • section 2.1 (Delegates - C # 1 ... basic knowledge of what they are).
    • In subsection 2.1.3, it describes events (and the erroneous assertion that events and delegates are one and the same).

  • Chapter 5

    • sections 5.1 to 5.6
    • In section 5.4, it introduces built-in delegates and anonymous functions. This is a good precursor to lambda expressions.

  • Chapter 9

    • section 9.1 talks about lambda expressions as delegates.

Of course, if you can’t get a copy of this book or you don’t have enough time to prepare (good luck!), Then here is a great article that is on par with John’s book. It is quite long, but covers everything you need. It is very clearly written and has good code samples. You can review this and then introduce it to your interns.

+3
source

As @Shark pointed out, this is not enough time. All you can do is hit the main points and point them to resources.

I used LINQPad to demonstrate and encouraged the audience to download and experiment with the free tool. It includes many samples.

Suggest that the company acquire LINQ Pocket References for everyone. That $ 10 (per copy) was well spent.

To demonstrate LINQ, I follow one syntax - either a query or a free one, depending on what you like best. (Fluent is better for demonstrating lambda expressions.) Use LINQ to Objects; just indicate that there are other providers. Again, check out the samples that come with LINQPad. "Chains of query operators" might be a good choice - Where, OrderBy, and Select in a string array.

Compare delegates to what they already know. Multicast are more or less pointers to functions.

Skip the whole conversation about anonymous delegates and go straight to lambda expressions. The syntax is shorter.

Be sure to include the Func<T> family of general delegates. They are very convenient as predicates and factories.

Also suppose they read Closing a loop variable considered harmful .

For events draw . NET recommendations . In particular, stress makes a temporary copy of the event in the uplift method to avoid a race condition.

+1
source

Some examples of delegates and Lambda expr. good luck :)

http://msdn.microsoft.com/en-us/library/bb397687.aspx

0
source

Source: https://habr.com/ru/post/1389575/


All Articles