A rare occurrence public void foo if datetimenowyear

Info icon This preview shows pages 625–627. Sign up to view the full content.

View Full Document Right Arrow Icon
Example 15-12. A rare occurrence public void Foo() { if (DateTime.Now.Year == 1973) { SomeExternalType.Disco(); } } Unless you run this on a computer whose clock is incredibly inaccurate the body of that if statement is never going to run. Despite this, when you first call Foo , the .NET Framework will ensure that the assembly that contains SomeExternalType is loaded, if it hasn’t already been. Life is significantly simpler for the JIT compiler (and it can therefore do its job faster) if it loads all the types and assemblies a method might use Loading | 601
Image of page 625

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

View Full Document Right Arrow Icon
up front, rather than loading each one on demand. The downside is that assemblies sometimes load slightly earlier than you might expect, but this isn’t usually a problem in practice. Visual Studio can show you exactly when assemblies load. If you run an application in the debugger, it will display a message to the Output panel for each assembly your program loads. If you don’t have the Output panel open, you can show it from the View menu. This can sometimes be useful if you have an application that is taking longer than expected to start up—take a look through the assemblies listed in the Output window, and if you see any you weren’t expecting, perhaps you have some code like Example 15-12 that is unnecessarily loading something you’re not really using. We know when assemblies are loaded. But from where are they loaded? There are many different places they could theoretically come from, but in the vast majority of cases, it’ll be one of two locations: either the same folder the application lives in or something called the GAC. Loading from the Application Folder When you add a reference from one project to another, Visual Studio copies the DLL being referenced into the consuming application’s folder. So, if we look in the bin\Debug folder for the MyProgram example shown earlier in this chapter, we’ll see both MyProgram.exe and a copy of MyLibrary.dll . An obvious upshot of this approach is that each application that uses a particular library will have its own copy. This may seem a little wasteful, and may even seem contrary to the spirit of DLLs—traditionally DLLs have offered a performance benefit by allowing disk space and memory to be shared by applications that use common DLLs. And while that’s true, sharing can cause a lot of problems—installing a new application could end up breaking old applications, because the new application might bring a new version of a shared DLL that turns out not to work with programs expecting the older version. To prevent this, .NET encourages isolation between applications—if each application brings its own copy of the libraries it requires, the chances of things breaking when new applications are installed are much lower. And now that disk and memory are much cheaper than they were back in the 1980s when DLLs were introduced, “not breaking everything” seems like a worthwhile return for using a bit more space.
Image of page 626
Image of page 627
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