hwak hwak - 18 days ago 5
C# Question

What child-class caused static parent method? Or mission impossible?

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 true
A.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.

Answer

Maybe:

//This _assumes_ you're allowed to use Generics, seems impossible otherwise as A.GetName would compile to C.GetName
void Main()
{
    A.GetName().Dump(); //A
    B.GetName().Dump(); //B
    new A();new A();new A();new A();
    new B();
    //For part 2 I'm not 100% sure what number is wanted    
    baseC.TotalCount.Dump(); //5 
    A.Count.Dump(); //4
    B.Count.Dump(); //1
}

abstract class baseC 
//cant think of any way to stop something other than C inheriting unless in own assembly
// if TotalCount is not the one wanted this can be dropped
{
    public static int TotalCount {get; protected set;} //Must be in the non-generic part so only one copy
}

abstract class C<T> : baseC where T: C<T> //Hopefully enough to FORCE A to inherit C<A> etc
{
    public static int Count {get; private set;}
    public static string GetName() 
    {
        return typeof(T).Name;
    }
    protected C() 
    { 
        TotalCount++;
        Count++;
    }
}