That means there are some situations where you cant

Info icon This preview shows pages 714–717. Sign up to view the full content.

View Full Document Right Arrow Icon
saying “I have no idea what sort of thing this is.” That means there are some situations where you can’t use it—you can’t derive a class from dynamic , for example, and typeof(dynamic) will not compile. But aside from the places where it would be mean- ingless, you can use it as you’d use any other type. 690 | Chapter 18: Dynamic
Image of page 714

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

View Full Document Right Arrow Icon
To see the dynamic behavior in action, we can try passing in a few different things to the AddAnything method from Example 18-4 , as Example 18-5 shows. Example 18-5. Passing different types Console.WriteLine(AddAnything("Hello", "world").GetType().Name); Console.WriteLine(AddAnything(31, 11).GetType().Name); Console.WriteLine(AddAnything("31", 11).GetType().Name); Console.WriteLine(AddAnything(31, 11.5).GetType().Name); AddAnything prints the value it calculates, and Example 18-5 then goes on to print the type of the returned value. This produces the following output: Helloworld String 42 Int32 3111 String 42.5 Double The + operator in AddAnything has behaved differently (dynamically, as it were) de- pending on the type of data we provided it with. Given two strings, it appended them, producing a string result. Given two integers, it added them, returning an integer as the result. Given some text and a number, it converted the number to a string, and then appended that to the first string. And given an integer and a double, it converted the integer to a double and then added it to the other double. If we weren’t using dynamic , every one of these would have required C# to generate quite different code. If you use the + operator in a situation where the compiler knows both types are strings, it generates code that calls the String.Concat method. If it knows both types are integers, it will instead generate code that performs arithmetic addition. Given an integer and a double, it will generate code that converts the integer to a double, followed by code to perform arithmetic addition. In all of these cases, it would uses the static information it has about the types to work out what code to generate to represent the expression a + b . Clearly C# has done something quite different with Example 18-4 . There’s only one method, meaning it had to produce a single piece of code that is somehow able to execute any of these different meanings for the + operator. The compiler does this by generating code that builds a special kind of object that represents an addition opera- tion, and this object then applies similar rules at runtime to those the compiler would have used at compile time if it knew what the types were. (This makes dynamic very different from var —see the sidebar on the next page.) The dynamic Type | 691
Image of page 715
dynamic Versus var At first glance, the difference between dynamic and var may not be entirely obvious. With either, you do not have to tell the compiler explicitly what type of data you’re working with—the compiler ultimately ensures that the right thing happens. For ex- ample, whether using dynamic or var , the + operator has the same effect that it would have if you had used it with explicitly typed variables. So why do we need both?
Image of page 716

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

View Full Document Right Arrow Icon
Image of page 717
This is the end of the preview. Sign up to access the rest of the document.

{[ 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