When you call a method or constructor the c compiler

Info icon This preview shows pages 115–117. Sign up to view the full content.

View Full Document Right Arrow Icon
When you call a method (or constructor), the C# compiler always emits a complete call—the compiled code passes a full set of arguments to the method, even if your source code left some arguments out. For example, in our Plane example, if you wrote: SendMessage("SomeMessage"); but the Plane class only has the method shown in Example 3-34 , the compiler actually generates code equivalent to this: SendMessage("SomeMessage", default(TimeSpan)); In other words, it plugs in the default value at compile time. This means if you’re using some external library that uses default values, and a newer version of the library comes out that changes the default values for some method or constructor, your code won’t pick up those new values unless you recompile your code. There’s also a subtler problem you can run into. Some parts of the .NET Framework require you to provide a particular constructor overload. For example, it you write a custom control for WPF, and you want to use it from Xaml, it must have a default constructor. (WPF and Xaml are described in Chapter 20 .) If all your constructors take parameters, then even if you provide default values for all the parameters, that’s not good enough. You can write, say, new MyControl() in C#, but only because the C# compiler is implicitly passing the missing values for you. Not everything in the world of .NET understands the concept of default arguments. (C# itself didn’t until version 4.0.) Sometimes only a genuine no-arguments constructor will do. Overloading | 91
Image of page 115

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

View Full Document Right Arrow Icon
This is not just limited to normal methods—you can use this same syntax to provide default values for parameters in your constructors, if you wish. Being forced to delete the extra constructor we tried to add back in Example 3-31 was a little disappointing—we’re constraining the number of ways users of our type can initialize it. Named arguments and default values have helped, but can we do more? Object Initializers Until C# 3.0, the only real solution to this was to write one or more factory methods . These are described in the sidebar below. But now we have another option. Factory Methods A factory method is a static method that builds a new object. There’s no formal support for this in C#, it’s just a common solution to a problem—a pattern , as popular idioms are often called in programming. We can get around the overload ambiguity problems by providing factory methods with different names. And the names can make it clear how we’re initializing the instance: public static PolarPoint3D FromDistanceAndAngle( double distance, double angle) { return new PolarPoint3D(distance, angle, 0); } public static PolarPoint3D FromAngleAndAltitude( double angle, double altitude) { return new PolarPoint3D(0, angle, altitude); } We rather like this approach, although some people frown on it as insufficiently dis- coverable . (Most developers aren’t expecting to find static methods that act rather like constructors, and if nobody finds these methods, we’re wasting our time in providing them.) However, this pattern is used all over the .NET Framework libraries—
Image of page 116
Image of page 117
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