0

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.

Community
  • 1
  • 1
hwak
  • 280
  • 1
  • 3
  • 18
  • 2
    You're already doing better than me. I can't understand the question! – John3136 Nov 18 '16 at 10:10
  • 2
    The questions of the interviewer are not clear to me. Was this translated into English from another language? Is the common ancestor for `A` and `b` (why lower-case `b`) identical to the mentioned class `C`? You cannot "implement" a `static` method. You can __hide__ a `static` method inherited from a base class by __another__ static method with the same name and signature, _but that is a bad practice_. If you want the syntax `A.GetName()` where `A` is the class name, then this is what you want. If you have _an instance_ `a`, then an extension method call `a.GetName()` could make more sense. – Jeppe Stig Nielsen Nov 18 '16 at 10:28
  • Sorry, my english is badly. – hwak Nov 18 '16 at 10:32
  • 1
    If you're only allowed to add `GetName` to `C`, and `C` isn't already generic, then this part cannot be done. – Lasse V. Karlsen Nov 18 '16 at 12:05

2 Answers2

1

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++;
    }
}
tolanj
  • 3,651
  • 16
  • 30
0

Thank you all. Thanks to the user https://ru.stackoverflow.com/users/186999/grundy and own research I found the answer to first part this question:

using System;
namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine ( A.GetName() == "A" && B.GetName() == "B" );
        }
    }
    class C<T> {
        public static string GetName(){
            return typeof(T).Name;
        }
    }
    class A : C<A>{ }
    class B : C<B> { }

}

but I still need your help to know the answer to the second part of question.

Community
  • 1
  • 1
hwak
  • 280
  • 1
  • 3
  • 18