PrepAway - Latest Free Exam Questions & Answers

You need to ensure that new instances of Connection can be created only by other classes by calling the Create

You have the following code (line numbers are included for reference only):

You need to ensure that new instances of Connection can be created only by other classes by calling
the Create method. The solution must allow classes to inherit from Connection.
What should you do?

PrepAway - Latest Free Exam Questions & Answers

A.
Option A

B.
Option B

C.
Option C

D.
Option D

Explanation:
The following list provides the main features of a static class:
* Contains only static members.
* Cannot be instantiated.
* Is sealed.
* Cannot contain Instance Constructors.
Creating a static class is therefore basically the same as creating a class that contains only static
members and a private constructor. A private constructor prevents the class from being instantiated.

Static Classes and Static Class Members (C# Programming Guide)
https://msdn.microsoft.com/en-us/library/79b3xss3.aspx

10 Comments on “You need to ensure that new instances of Connection can be created only by other classes by calling the Create

    1. Heitor says:

      you are right about the private constructor, but C is wrong because of the inheritance detail:

      The solution must allow classes to inherit from Connection.

      by using a protected constructor you allow inherited classes to have a parameterless ctor

      the right answer is D




      1



      0
  1. harsh says:

    Yes answer should be D
    A–>you get compile error at 05: can not create an instance of abstract class
    B–>static classes can not be inherited
    C–> class is inaccessible due to its protection level




    1



    0
  2. Lord Vader says:

    answer is D.
    The keyword “protects” the class from having its’ constructor called by external classes. However unlike the private keyword, protected will allow derived classes to access the class member.

    So what good is it? Classes that use it will employ other means to create instances of the class. A static “CreateInstance” method within the class can call the protected constructor and return an instance.




    1



    0

Leave a Reply