I got a test task during the interview for the post of C# programmer, but was never able to solve, because I do not understand what is required.
I write this because I want to improve, to finish the begun business, to understand and know what to didn't understand or didn't know to the extent possible.
With the words "this is very simple test" interviewer sent me message on email. I quote the statement:
Classes "A" and "B" do not include the implementation of the properties, fields and methods that are not attributed and are heirs of a common ancestor "C"
1) not creating instances of the described classes, you should implement the "C" class static method with the signature
public static string GetName()
for which is trueA.GetName() == "A" && B.GetName() == "B"
2) implement a single count-variable of the number of instances of all the heirs of class with the signature
public static int Count
in the class, not liaise outside the line of succession
I ask the question interviewer what is meant by constants "A" and "B" - and he answered that it is the class names. Ie the GetName() method should return the name of the descendant class.
In the process of trying to implement exactly as described in the brief and furious googling were found material from stackoverflow: How to get the class Type in a base class static method in .NET?
and
Get inherited caller type name in base static class
Where, as I understand, argues that to get the name of the descendant class from a static method is not possible without hacks.
I considered a few other options (e.g. creating a new class that is not described in the job), but they all involve attempts to circumvent the conditions, that does not fit with the words interviewer that there is a specific solution, I think it should be fair and simple, since he initially claimed.
I was able to do just that, but I know that it's wrong, because the GetName method non-static and instantiated class:
1:
using System;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine ( new A().GetName() == "A" && new B().GetName() == "B" );
}
}
class C {
public string GetName() {
return this.GetType().Name;
}
}
class A : C{}
class B : C{}
}
2:
using System;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
var a = new A();
var b = new B();
var c = new C();
}
}
public class C {
public static int Count;
public C () {
if ( this is A
|| this is B )
Count++;
Console.WriteLine ( "Count is: " + Count );
}
}
public class A : C{}
public class B : C{}
}
Response of interviewer:
he:good day, I see, but have to disappoint, the problem has a concrete solution
me:ie, neither of the two points invalid?
he:the second depends on the first
he:appropriately, the first is not true, as create copies can not
Please help me to solve this problem. It seems to me that in the formulation there are contradictions, the more it is used the double negative that it is difficult to understand.