18

Click here to load reader

09 advanced c#

  • Upload
    eleksdev

  • View
    1.391

  • Download
    16

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: 09 advanced c#

Advanced C#

Page 2: 09 advanced c#

Delegates• We can use interfaces instead of

delegates.• Delegates – we need it to separate

implementation and interface.

Page 3: 09 advanced c#

Anonymous functionWe don’t need to set name for function each time.

Page 4: 09 advanced c#

Lambda + Closures• Lambda – anonymous function with

short syntax.

• Closure - the function captures variables from the scope.

Page 5: 09 advanced c#

Problem

Page 6: 09 advanced c#

Solving- Iterators & foreach loop

Page 7: 09 advanced c#

Iterator pattern

Page 8: 09 advanced c#

IEnumerable<T>• IEnumerable<T> requires only one

method to be implemented – IEnumerator<T> GetEnumerator()

• Do not modify collection in the iteration process.

• Remember that IEnumerables are lazy

Page 9: 09 advanced c#

IEnumerator<T>• Current returns the same object until

MoveNext is called. MoveNext sets Current to the next element.

• Initially, the enumerator is positioned before the first element in the collection.

• If MoveNext() == false – Current is undefined.

• You cannot set Current to the first element of the collection again; you must create a new enumerator instance instead.

http://msdn.microsoft.com/ru-ru/library/s793z9y2(v=vs.110).aspx

Page 10: 09 advanced c#

Usage

Page 11: 09 advanced c#

Foreach loop• The foreach statement repeats a

group of embedded statements for each element in an array or an object collection that implements the IEnumerable<T>

Page 12: 09 advanced c#

Yield• Method should returns

IEnumerable<T>• Yield return one value and do

MoveNext() = true• Yield break do MoveNext() = false

Page 13: 09 advanced c#

LINQ• LINQ - Language-Integrated Query

• Extensions methods was implemented for LINQ

Page 14: 09 advanced c#

What we can to do with linq?

- Filter/Transform/Aggregate/Generate data

Page 15: 09 advanced c#

LINQ and LINQ extensions

Page 16: 09 advanced c#

RecursionRecursion – see recursion

Why we need it?- Tree traversing- Working with Linked Lists- etc…

Page 17: 09 advanced c#
Page 18: 09 advanced c#

Sum up• Use LINQ and LINQ extension methods – to declaratively specify what you want – to simplify your code– to generalize your code– to create complex abstractions from

simple ones• Remember that IEnumerables are lazy• Use delegates to separate

implementation and interface