Home » Microsoft » 70-483 v.2 » What should you do?
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?
A. Option A
B. Option B
C. Option C
D. Option D
Correct Answer: B
Explanation/Reference:
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.
Incorrect:
Not A: An abstract method is a method that is declared without an implementation.
Not C: Private methods can be called from derived classes.
Reference: Static Classes and Static Class Members (C# Programming Guide)
https://msdn.microsoft.com/en-us/library/79b3xss3.aspx
Correct “D”
UPDATED : Option 4 is answer. Try to run this program. only this case runs.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
//public abstract class Connection
//public static class Connection
public class Connection
{
//private Connection(){}
protected Connection(){}
public static Connection Create()
{
return new Connection();
}
}
public class Program : Connection
{
public static void Main(string[] args)
{
Connection abc = Connection.Create();
//Your code goes here
Console.WriteLine(“Hello, world!”);
}
}
}
correct answer:
Protected
I kinda thought C as well. There’s no private constructor defined. This code (if it’s to be a singleton connection) would need it. It would also need a static connection field. The Q is a bit lame.
Correct answer is A.
“The solution must allow classes to inherit from Connection.”, this rules out B as no classes can inherit from static classes.
“You need to ensure that new instances of Connection can be created only by other classes by calling the Create method.”. This is a tricky statement but C ensures no other class can see it. D would allow inherited classes to call it (similar to abstract class) and abstract still has access to it. A would enforce other classes to call to create it, but static classes can still call there own static methods. So I would go back to A.
Abstract class can not have implementation
C. private constructor
Class using private constructor cannot be inherited.
Option 4 is answer. Try to run this program. only this case runs.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
//public abstract class Connection
//public static class Connection
public class Connection
{
private Connection(){}
//protected Connection(){}
public static Connection Create()
{
return new Connection();
}
}
public class Program : Connection
{
public static void Main(string[] args)
{
Connection abc = Connection.Create();
//Your code goes here
Console.WriteLine(“Hello, world!”);
}
}
}