21
Feb 14

Yield return and dynamic gotchas with explicit interface implementation

This week while I’m doing a little demo to measure the time using a yield return method vs return a static list. I decided to use the keyword dynamic to simplify my code and avoid  the methods overload. Quickly i found when I called the yield return method it simple crashed with a kinda gotchas between dynamic and IEnumerable<T>. It fails when yield code explicitly implements its interfaces. The solution is to help the execution-time overload resolution to find the most specific method. 

Code demo:

using System;
using System.Collections.Generic;
using System.Collections;
using System.Diagnostics;

namespace YieldDemo
{
    public class States1
    {
          public static IEnumerable<string> GetNames()
        {
            yield return "Alabama";
            yield return "Alaska";
            yield return "Arizona";
            yield return "Arkansas";
            yield return "California";
            yield return "Others ...";
        }
    }

    public class States2
    {
        private static readonly IList<string> _names;

        static States2()
        {
            _names = new List<string>() {"Alabama", 
                                     "Alaska",
                                     "Arizona",
                                     "Arkansas",
                                     "California",
                                     "Others ..." };

        }

        public static IList<string> GetNames()
        {
            return _names;
        }
    }

    public class Program
    {

        static void Main()
        {
            Test(States2.GetNames());
            Test(States1.GetNames());

            Console.ReadLine();
        }

        public static void Test(dynamic state)
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            Iterate(state);            
            stopwatch.Stop();

            Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed);
        }

        public static void Iterate(dynamic itemList)
        {
            var enumerator = itemList.GetEnumerator();

            while (enumerator.MoveNext())
            {
                Console.WriteLine(enumerator.Current);
            }
        }
    }
}

When i call the following method:

Test(States1.GetNames())

I received the error “Additional information: ‘object’ does not contain a definition for ‘GetEnumerator'”.

Without any clue why this is happening. I posted a question in StackOverflow and I got some help.

“The problem is that the iterator block implementation uses explicit interface implementation to implement IEnumerable<T>… and explicit interface implementation doesn’t play nicely with dynamic typing in general. (You don’t need to use iterator blocks to see that. See my article on Gotchas in Dynamic Typing for more details.)”

“It fails because the IEnumerable<string> class generated by your yield code explicitly implements its interfaces (including the GetEnumerator you’re trying to use).”

So …

One way of avoiding the explicit interface implementation issue is to use dynamic typing to perform execution-time overload resolution to find the most specific method … the compiler won’t always know which overload to choose.

Solution 1:

Using the foreach to iterate.

public static void Iterate(dynamic itemList)
{
    foreach (dynamic item in itemList)
    {
        Console.WriteLine(item);
    }
}

Solution 2:

public static void Iterate(dynamic itemList)
{
    var enumerator = ((IEnumerable)itemList).GetEnumerator();

    while (enumerator.MoveNext())
    {
        Console.WriteLine(enumerator.Current);
    }
}

Solution 3:

public static void Iterate(IEnumerable itemList)
{
    var enumerator = itemList.GetEnumerator();

    while (enumerator.MoveNext())
    {
        Console.WriteLine(enumerator.Current);
    }
}

Solution 4:

public static void Iterate<T>(IEnumerable<T> itemList)
{
    var enumerator = itemList.GetEnumerator();

    while (enumerator.MoveNext())
    {
        Console.WriteLine(enumerator.Current);
    }
}

27
Dec 13

More about Visual Studio 2013 Part 2

From Microsoft:

What’s New in Visual Studio 2013: (01) What’s New in the IDE
Authored by:  Christopher Harrison and Susan Ibach

What’s New in Visual Studio 2013: (02) New Features for Web Developers
Authored by:  Christopher Harrison and Susan Ibach

What’s New in Visual Studio 2013: (03) Application Lifecycle Management
Authored by:  Christopher Harrison and Susan Ibach

What’s New in Visual Studio 2013: (04) What’s New for App Developers
Authored by:  Christopher Harrison and Susan Ibach

Link: Visual Studio 2013 Part 1


15
Nov 13

More about Visual Studio 2013 Part 1

From Microsoft:

Keynote: Transforming software development in a world of devices and services
Authored by: S. Somasegar, Brian Harry, Scott Guthrie, Scott Hanselman

Visual Studio 2013 Launch Keynote Recap with S. Somasegar
Authored by: S. Somasegar, Brian Keller

What’s New for Web Developers in Visual Studio 2013
Authored by: Scott Hunter, Mads Kristensen, Damian Edwards

What’s New in the Visual Studio 2013 IDE
Authored by: David Starr, Cathy Sullivan

From Pluralsight

Mastering Visual Studio 2013

This course covers the newest features of Visual Studio including new IDE features, code editor improvements, XAML coding enhancements, updated debugging features, and the awesome new CodeLens.

Authored by: Deborah Kurata
Duration: 2h 50m
Level: Intermediate
Released: 10/22/2013

Click here to check the course!


25
Jul 13

Hacker Tools You Should Know and Worry About

When we investigate cybercrime we often find traces of commonly known hacker tools, trojans, etc. In this highly interactive demo session, we show you the hacking tools of today, the damage they can do, and how you can recognize them when they are used to attack your infrastructure. This is an extremely valuable and important session for anybody interested in protecting their own IT environment.

Click here to visit original post!


23
Jul 13

What’s New in .NET Development Build 2013

The latest version of .NET delivers a number of new innovations while maintaining a high compatibility bar. In this presentation, we will walk through the latest features in .NET that focus on Developer Productivity, Application Performance and Continuous Delivery. Examples will include diagnostics improvements, library enhancements and new features in Windows Store development.

Developer Productivity

64-bit Edit and continue
Method return value inspection
Async Debugging Enhancements
Windows Store development improvements
EF/ADO.NET Connection Resilency

Application Performance

ASP.NET application suspension
Multi-core JIT improvements
On-demand large object heap compaction
Consistent performance before and after servicing the .NET Framework

Continuous Innovation

.NET Framework updates
NuGet releases
Curated.NET Framework NuGet packages

Click here to visit original post!