3 Replies - 496 Views - Last Post: 18 June 2013 - 11:30 AM Rate Topic: -----

#1 Jeff H  Icon User is offline

  • D.I.C Regular

Reputation: 112
  • View blog
  • Posts: 308
  • Joined: 30-January 11

What the Duck - ForEach, IEnumerable, IEnumerator DuckTyping

Posted 17 June 2013 - 10:03 PM

I found this interesting and never knew how much duck typing was going on under hood.
Do not have to implement IEnumerable or IEnumerator.
For a for each loop to work on a object just needs a method called GetEnumerator that returns an Object that has method MoveNext that returns a bool and a property Current that returns any type.
Had it return StreamReader, etc......

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
namespace WhatTheDuck
{
    class Program
    {
        static void Main(string[] args)
        {
            Duck d = new Duck();
 
            foreach(var i in d)
            {
                Console.WriteLine("{0}{1}{0}", "*Duck*", i);
            }
            Console.ReadLine();
        }
    }
    public class Duck 
    {
        public Quack GetEnumerator()
        {
            return new Quack();            
        }
        
    }
    public class Quack
    {
        static int counter = 0;
        public int Current
        {
            get
            {
                counter++;
                Console.WriteLine("*Quack*Current*Quack*");
                return counter;
                //return new StreamReader(@"C:\Testing\SSM.txt");
            }
        }
        public bool MoveNext()
        {
            Console.WriteLine("*Quack*MoveNext*Quack*");
            return counter < 10;
        }
        //public void Reset()
        //{
        //    counter = 1;
        //}
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: What the Duck - ForEach, IEnumerable, IEnumerator DuckTyping

#2 coder3788  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 62
  • Joined: 06-November 12

Re: What the Duck - ForEach, IEnumerable, IEnumerator DuckTyping

Posted 18 June 2013 - 01:23 AM

The Collections chapter in C# 5.0 in a Nutshell is a very good reference for what you're saying here.
Foreach moves the enumerator trough the collection until MoveNext returns false, and IEnumerable is just a way to make sure you got one.
Was This Post Helpful? 0
  • +
  • -

#3 Jeff H  Icon User is offline

  • D.I.C Regular

Reputation: 112
  • View blog
  • Posts: 308
  • Joined: 30-January 11

Re: What the Duck - ForEach, IEnumerable, IEnumerator DuckTyping

Posted 18 June 2013 - 03:47 AM

Thanks,

Probably need to add that to Kindle collection, I just noticed in some MSIL that it did not call the methods through the interface.
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: What the Duck - ForEach, IEnumerable, IEnumerator DuckTyping

Posted 18 June 2013 - 11:30 AM

I doubt it is Duck Typing, which use Dynamic dispatching and dynamic member lookup.

It is just simple sub-typing (inheritance) , every Object in .net inherits from System.Object. So hence why .Current with a return type of Object can be used to return any object. But it comes at price, if you where expecting it to say return only Apples and a Banana a is return it could screw things up.
Then there the issue of boxing and unboxing.

Also IEnumerable is incompatible with LINQ, you need the generic version IEnumerable(Of T)

This post has been edited by AdamSpeight2008: 18 June 2013 - 11:32 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1