Callback - is a method passed as argument and called at specific tume (after/before some kind of event)
Examples:
1: 2: 3: 4: 5: 6: 7: 8: 9: |
|
Callback methods can be implemented in C# using delegates, events and lambda expressions
Delegate is a reference type which holds:
Declaration example:
1: 2: |
|
Invoke instance method example
Invokation list example
Real word example
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: |
|
Assume we have a delegate and a method:
1: 2: 3: |
|
Assign method to delegate:
1: 2: 3: 4: |
|
Remove method from delegate:
1:
|
|
Anonymous methods let you declare a method body without giving it a name
Anonymous method without parameters:
1: 2: 3: 4: |
|
Anonymous method with parameters:
1: 2: 3: 4: |
|
Keyword delegate might be avoided when anonymous method declared, instead use sign '=>':
1: 2: 3: 4: 5: 6: 7: |
|
1: 2: 3: 4: 5: 6: 7: |
|
Closure - is a data structure storing a function together with an environment (c) Wiki
A closure is attached to its parent method means that variables defined in parent's method body can be referenced from within the anonymous method.
1: 2: 3: 4: 5: 6: 7: |
|
event keyword is a modifier for a delegate instantiation that allows:
An event can be included in an interface declaration, whereas a field cannot
1: 2: 3: 4: 5: 6: 7: 8: 9: |
|
An event can be invoked from within the class that declared it
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: |
|
Family of generic delegates which does NOT return a value
1: 2: 3: 4: 5: 6: 7: 8: |
|
Family of generic delegates which DOES return a value
1: 2: 3: 4: 5: 6: 7: 8: |
|
Defines a method which returns bool and one generic parameter
Used as a function to affirm or deny input for specific operation
1:
|
|
Example:
1: 2: 3: 4: 5: 6: 7: 8: 9: |
|