The dynamic type 699 worse this would prevent the c

Info icon This preview shows pages 723–725. Sign up to view the full content.

The dynamic Type | 699
Image of page 723

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Worse, this would prevent the C# compiler from being able to optimize your project references. Normally, C# detects when your project has a reference to an assembly that your code never uses, and it removes any such references at compile time. But if your program made any dynamic method calls, it would need to keep references to appa- rently unused assemblies, just in case they turn out to be necessary to resolve an ex- tension method call at runtime. So while it would have been possible for Microsoft to make this work, there would be a significant price to pay. And it would probably have provided only marginal value, because it wouldn’t even be useful for the most widely used extension methods. The biggest user of extension methods in the .NET Framework class library is LINQ—that Sum method is a standard LINQ operator, for example. It’s one of the simpler ones. Most of the operators take arguments, many of which expect lambdas. For those to compile, the C# compiler depends on static type information to create a suitable del- egate. For example, there’s an overload of the Sum operator that takes a lambda, enabling you to compute the sum of a value calculated from the underlying data, rather than merely summing the underlying data itself. Example 18-16 uses this overload to cal- culate the sum of the squares of the numbers in the list. Example 18-16. Lambdas and types int total = numbers.Sum(x => x * x); When the numbers variable has a static type ( IEnumerable<int> in our case) this works just fine. But if numbers is dynamic , the compiler simply doesn’t have enough information to know what code it needs to generate for that lambda. Given sufficiently heroic efforts from the compiler, it could embed enough information to be able to generate all the necessary code at runtime, but for what benefit? LINQ is designed for a statically typed world, and dynamic is designed mainly for interop. So Microsoft decided not to support these kinds of scenarios with dynamic —stick with static typing when using LINQ. Objects from other dynamic languages The dynamic keyword uses an underlying mechanism that is not unique to C#. It de- pends on a set of libraries and conventions known as the DLR—the Dynamic Language Runtime. The libraries are built into the .NET Framework, so these services are avail- able anywhere .NET 4 or later is available. This enables C# to work with dynamic objects from other languages. Earlier in this chapter, we mentioned that in the Ruby programming language, it’s possible to write code that decides at runtime what methods a particular object is going to offer. If you’re using an implementation of Ruby that uses the DLR (such as Iron- Ruby), you can use these kinds of objects from C#. The DLR website provides open † This optimization doesn’t occur for Silverlight projects, by the way. The way Silverlight uses control libraries from Xaml means Visual Studio has to be conservative about project references.
Image of page 724
Image of page 725
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '15

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern