Example 15 5 public type internal constructor public

Info icon This preview shows pages 620–623. Sign up to view the full content.

Example 15-5. Public type, internal constructor public class MyType { internal MyType() { } } 596 | Chapter 15: Assemblies
Image of page 620

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

This would enable MyProgram to declare variables of type MyType , which it was not able to do before we made the class public . But it’s still unable to construct a new MyType . So, in Example 15-6 , the first line will compile, but we will get an error on the second line because there are no accessible constructors. Example 15-6. Using the type and using its members MyType o; // Compiles OK o = new MyType(); // Error This is more useful than it might seem. This has enabled MyLibrary to define a type as part of its public API, but to retain control over how instances of that type are created. This lets it force users of the library to go through a factory method, which can be useful for several reasons: Some objects require additional work after construction—perhaps you need to register the existence of an object with some other part of your system. If your objects represent specific real entities, you might want to ensure that only code you trust gets to create new objects of a particular type. You might sometimes want to create a derived type, choosing the exact class at runtime. Example 15-7 shows a very simple factory method which does none of the above, but crucially our library has reserved the right to do any or all of these things in the future. We’ve chosen to expose this factory method from the other type in the library project, Class1 . This class gets to use the internal constructor for MyType because it lives in the same assembly. Example 15-7. Factory method for a public type with an internal constructor public class Class1 { public static MyType MakeMeAnInstance() { return new MyType(); } } Our MyProgram project can then use this method to get Class1 to construct an instance of MyType on its behalf, as Example 15-8 shows. Example 15-8. Using a type with an internal constructor from outside MyType o = Class1.MakeMeAnInstance(); .NET Components: Assemblies | 597
Image of page 621
Example 15-7 shows another reason it can be useful to have a public class with no public constructors. Class1 offers a public static method, meaning the class is useful even if we never construct it. In fact, as it stands, there’s never any reason to construct a Class1 , because it con- tains no instance members. Classes that offer public static members but which are never constructed are rather common, and we can make it clear that they’re not meant to be constructed by putting the keyword static before class . This would prevent even code in the MyLibrary project from constructing an instance of Class1 . Occasionally, it can be useful to make the internal features of an assembly accessible to one or more other specific assemblies. If you write a particularly large class library, it might be useful to split it into multiple assemblies much like the .NET Framework class library. But you might want to let these all use one another’s internal features, without exposing those features to code that uses your library. Another particularly
Image of page 622

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

Image of page 623
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