A simple class class mytype public string text get

Info icon This preview shows pages 719–722. Sign up to view the full content.

View Full Document Right Arrow Icon
Example 18-10. A simple class class MyType { public string Text { get; set; } public int Number { get; set; } public override string ToString() { return Text + ", " + Number; } public void SetBoth(string t, int n) { The dynamic Type | 695
Image of page 719

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

View Full Document Right Arrow Icon
Text = t; Number = n; } public static MyType operator + (MyType left, MyType right) { return new MyType { Text = left.Text + right.Text, Number = left.Number + right.Number }; } } We can use objects of this through a dynamic variable, as Example 18-11 shows. Example 18-11. Using a simple object with dynamic dynamic a = new MyType { Text = "One", Number = 123 }; Console.WriteLine(a.Text); Console.WriteLine(a.Number); Console.WriteLine(a.Problem); The lines that call Console.WriteLine all use the dynamic variable a with normal C# property syntax. The first two do exactly what you’d expect if the variable had been declared as MyType or var instead of dynamic : they just print out the values of the Text and Number properties. The third one is more interesting—it tries to use a property that does not exist. If the variable had been declared as either MyType or var , this would not have compiled—the compiler would have complained at our attempt to read a property that it knows is not there. But because we’ve used dynamic , the compiler does not even attempt to check this sort of thing at compile time. So it compiles, and instead it fails at runtime—that third line throws a RuntimeBinderException , with a message com- plaining that the target type does not define the Problem member we’re looking for. This is one of the prices we pay for the flexibility of dynamic behavior: the compiler is less vigilant. Certain programming errors that would be caught at compile time when using the static style do not get detected until runtime. And there’s a related price: IntelliSense relies on the same compile-time type information that would have noticed this error. If we were to change the variable in Example 18-11 ’s type to either MyType or var , we would see IntelliSense pop ups such as those shown in Figure 18-1 while writing the code. Visual Studio is able to show the list of available methods because the variable is stat- ically typed—it will always refer to a MyType object. But with dynamic , we get much less help. As Figure 18-2 shows, Visual Studio simply tells us that it has no idea what’s available. In this simple example, you could argue that it should be able to work it out—although we’ve declared the variable to be dynamic , it can only ever be a MyType at this point in the program. But Visual Studio does not attempt to perform this sort of analysis for a couple of reasons. First, it would work for only relatively trivial scenarios such as these, and would fail to work anywhere you were truly exploiting the dynamic 696 | Chapter 18: Dynamic
Image of page 720
nature of dynamic —and if you don’t really need the dynamism, why not just stick with statically typed variables? Second, as we’ll see later, it’s possible for a type to customize its dynamic behavior, so even if Visual Studio knows that a dynamic variable always refers to a MyType
Image of page 721

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

View Full Document Right Arrow Icon
Image of page 722
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