Mohammed Omar's Blog

Just another WordPress.com site

Category Archives: C# Topics

Why the Yield return is Important!

You should always use yield when possible specially between layers, actually if it wasn’t for yield many features would be really hard to implement nowadays and to hear you say you don’t use it makes me wonder how you implement those features. Any way lets analysis some code:

Consider the following piece of code:

          public IEnumerable<int> Dolist()
        {
            List<int> nums = new List<int>();
            for (int i = 0; i < 5; i++)
            {
                nums.Add(i);
            }

            return nums;
        }

Looks so simple and straight forward isn’t it? We iterate over 5 digits add them to a list and return them…
So straight forward… now compile your code to dll and then disassemble it then check how the compiler compiled your code:

public class Class1
{
    // Methods
    public IEnumerable<int> Dolist()
    {
        List<int> nums = new List<int>();
        for (int i = 0; i < 5; i++)
        {
            nums.Add(i);
        }
        return nums;
    }
}

Nothing strange here everything is as expected…

Now try this code :

public IEnumerable<int> DoEnum()
        {
            for (int i = 0; i < 5; i++)
            {
                yield return i;
            }
        }

Now check the Compiled code:

public class Class1
{
    // Methods
    public IEnumerable<int> DoEnum()
    {
        <DoEnum>d__0 d__ = new <DoEnum>d__0(-2);
        d__.<>4__this = this;
        return d__;
    }

    // Nested Types
    [CompilerGenerated]
    private sealed class <DoEnum>d__0 : IEnumerable<int>, IEnumerable, IEnumerator<int>, IEnumerator, IDisposable
    {
        // Fields
        private int <>1__state;
        private int <>2__current;
        public Class1 <>4__this;
        private int <>l__initialThreadId;
        public int <i>5__1;

        // Methods
        [DebuggerHidden]
        public <DoEnum>d__0(int <>1__state)
        {
            this.<>1__state = <>1__state;
            this.<>l__initialThreadId = Thread.CurrentThread.ManagedThreadId;
        }

        private bool MoveNext()
        {
            switch (this.<>1__state)
            {
                case 0:
                    this.<>1__state = -1;
                    this.<i>5__1 = 0;
                    while (this.<i>5__1 < 5)
                    {
                        this.<>2__current = this.<i>5__1;
                        this.<>1__state = 1;
                        return true;
                    Label_0046:
                        this.<>1__state = -1;
                        this.<i>5__1++;
                    }
                    break;

                case 1:
                    goto Label_0046;
            }
            return false;
        }

        [DebuggerHidden]
        IEnumerator<int> IEnumerable<int>.GetEnumerator()
        {
            if ((Thread.CurrentThread.ManagedThreadId == this.<>l__initialThreadId) && (this.<>1__state == -2))
            {
                this.<>1__state = 0;
                return this;
            }
            Class1.<DoEnum>d__0 d__ = new Class1.<DoEnum>d__0(0);
            d__.<>4__this = this.<>4__this;
            return d__;
        }

        [DebuggerHidden]
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.System.Collections.Generic.IEnumerable<System.Int32>.GetEnumerator();
        }

        [DebuggerHidden]
        void IEnumerator.Reset()
        {
            throw new NotSupportedException();
        }

        void IDisposable.Dispose()
        {
        }

        // Properties
        int IEnumerator<int>.Current
        {
            [DebuggerHidden]
            get
            {
                return this.<>2__current;
            }
        }

        object IEnumerator.Current
        {
            [DebuggerHidden]
            get
            {
                return this.<>2__current;
            }
        }
    }
}

WHAT!????? So it’s not that you just don’t want to declare a variable to hold numbs! What is it then?

As you can see the .Net compiler would take your Code and compile it to a helper class which serve a State nested Class, to hold the state of every time you retrieve an item of the enumerator, which in other word you don’t get all the items at once! , it LAZY retrieve the items when you for each them which is perfect for LINQ! (isn’t that what LINQ is about?), so what! what advantage that would give me ?

Performance

Try the same implementation with 100 million numbers you will be amazed on the performance you get with a simple keyword (Yield), but guess what this is not all !

Yield return is one of the many keywords that Microsoft put into C# to support LINQ, part of the fact that LINQ is Lazy, let me put this into real example here and show you what you would gain from the Yield return keyword:

Consider this code:

public static class CustomerRepository
    {
        public static IEnumerable<Customer> GetAllNotLazy()
        {
            List<Customer> customers = new List<Customer>();

            for (int i = 0; i < 10; i++)
            {
                customers.Add(new Customer()
                                  {
                                      FirstName = "Mohammed",
                                      LastName = "Umar"
                                  });
            }
            return customers;
        }
        public static IEnumerable<Customer> GetAllLazy()
        {
            for (int i = 0; i < 10; i++)
            {
                yield return new Customer()
                    {
                        FirstName = "Mohammed",
                        LastName = "Umar"
                    };
            }
        }
    }

Now consider you`re in another layer or something with this code:

public static void Main()
        {
            var NotLazy = CustomerRepository.GetAllNotLazy().Where(x =>x.FirstName == "Mohammed");
            var Lazy = CustomerRepository.GetAllLazy().Where(x => x.FirstName == "Mohammed");
            foreach (var customer in Lazy)
            {
                //do stuff
            }
            foreach (var customer in NotLazy)
            {
                //do stuff
            }
        }

With GetAllNoLazy all your objects would be retrieved at once!
With GetAllLazy your objects would be retrieved once you need them!

Imagine you were checking something or applying some filter while your iterating inside the for-each and you decided to break! With lazy you wouldn’t lose much, without being lazy all your objects would be in memory anyway despite the fact that you break or not!

I can go on and on with situation where not using “Yield Return” would be a crime!

But I can sure now you can realize how the yield return was mandatory for LINQ and what situation you must use it otherwise implement your own State-Class…

Advertisements

Evolution Of Delegates Part 2

     So i assume you have read Evolution Of Delegates Part 1, any way lets start off by explaining anonymous delegate   

Anonymous Delegates   

     Anonymous delegates the ability to declare a delegate and the method it points to inline without the need to declare the method separately, well that sound awesome but sure it has  its advantages and disadvantages, like one of the disadvantages is you can not reuse that method in other places any way im sure you can come up with some other disadvantages ;)… so lets jump to how do we implement anonymous delegates?   

Consider this delegate:   

public delegate void MyDelegate(int left, int right);

And this method:   

        public static void sum(int left, int right)
        {
            Console.WriteLine(left + right);
        }

Usually we register it this method with that delegate like that   

      MyDelegate del = new MyDelegate(sum);
      //or using Method group conversion
      del += sum;

Now we want to register same implementation of  this method to that delegate without declaring the method how do we do that ? simple 3 steps:   

1-first copy the whole method and assign it to the delegate object:     

      mydelegate del = public void Sum(int left, int right)
			           {
				             Console.WriteLine(left + right);
			           }; 

2- Delete the method scope and return:

      mydelegate del = Sum(int left, int right)
			     {
				        Console.WriteLine(left + right);
			     }; 

3-Replace the method name with the keyword “delegate”:  

      mydelegate del = delegate(int left, int right)
                         {
                              Console.WriteLine(left + right);
                         }; 

guess what ! you made it now your project can compile no problem !, but wait ! there are some rules you have to know about anonymos delegates:

  1. Anonymous delegate can not access Ref/Out parameters in its defining method.
  2. Anonymous delegate cant declare a variable with the same name as a variable in its defining method.
  3. Anonymous delegate can declare a variable with the same name as member in its class but that gonna hide the outer variable.
  4. prolly there are some more im missing !.

any way in the next post im gonna be talking shortly about the Yield keyword and then we are gonna make a sample which mix all the previous features of C# then come up with how lambda expression and linq came out of all this ! thanks and see you later in the next post….