C# Inheritance Question
The following are two simple questions related to C# inheritance.
Please try to give an answer without executing the code, otherwise where is the fun part ?
// Question #1
class Program
{
static void Main(string[] args)
{
int i = 13;
string Result = B.Get(i);
Console.WriteLine(Result);
}
}
public class A
{
public static string Get(int Value) {
return "A";
}
}
public class B : A
{
public static string Get(float Value)
{
return "B";
}
}
// Question #2
class Program
{
static void Main(string[] args)
{
int i = 13;
string Result = B.Get(i);
Console.WriteLine(Result);
}
}
public class B
{
public static string Get(float Value)
{
return "B";
}
public static string Get(int Value) {
return "A";
}
}
What will be the result of the above code parts ?
Categories: ASP.NET, C#, Interview Questions, Programming, Tips & Tricks


Both will result in “A” !?
The first will result in a “B” and the second in an “A”
That’s because you can pass an int into a float variable but not vice versa.
You are correct, but you can do the same thing in the B class – you have an overloaded Get in both cases.
This question is actually about priorities of inherited overloaded methods.
First “B” Second “A”
I think it is more about method overloading than inheritance.
@Kaveh Shahbazian
I have to disagree with you.
Overloading is something very obvious (in most cases) and as you can see it works exactly as expected in Question #2.
Overloading has it’s own priorities, for example <T> priority is higher than “object”.
My example is about how inheritance affects the “obvious” overloading.
What’s this got to do with inheritance? You are using static methods which are not included in inheritance relationships in C# anyway. This is about overloading.
Bob, static methods are just like instance methods ( except you can’t make it virtual) and i used them to avoid instance creation.
You can easily remove the “static” modifier and create an instance of “B” before calling the “Get” method, and you will get the same result.
What’s this got to do with inheritance ? Well, this is exactly what i am trying to show here. First level methods are prioritized over any inherited method in case of parameters suitability.
In general, you should avoid creating more abstract/”bigger” overloads in a derived class. In your derived classes, i.e., smaller/more specific classes, your method overloads should be smaller/more specific as well.
(I might be misusing the terms bigger and smaller, which are really about covariance and contravariance, but that’s how I think about it. By bigger, I don’t just mean supertype.)
If A has Get(string value), think carefully before you create an overload in B Get(object value). Also, if A has Get(string[] values), you should be cautious creating overload Get(object[] values) in B.
Of course there are exceptions to this rule, but I think it helps avoid this type of confusion.
And I get your point in this context, but there’s more difference with static methods than simply not being able to make them virtual.
You right, but this question is not about Good design or Bad design – it’s about how well can you read and understand the code.
Most of the tricky questions can’t be categorized as a Good Design.
For example, are you familiar with the ‘@’ prefix ?
The above line will compile and for my opinion it’s a terrible thing to use in your code, but despite all of that a developer must know about it’s existence and what it suppose to do.
It’s a interesting question,
I guess it was caused by the predefined implicit conversion of int in the inheritance.
http://msdn.microsoft.com/en-us/library/5kzh1b5w%28VS.80%29.aspx
So, if change the float to long,double or decimal,
it sure be has the same problem.
Ans of First Question is ‘B’ Because in inheritance Always First Hit the drive Class Method then Base Class if Both Drive And Base Class Have Same Method name then it first Call The Drive Class Method and Hide The Base Class Method if Want To Call Base Class Method Then You Have Use base KeyWord With Method.
Ans Of Second Question is A in this Type of Datatype you have Pass in method is called First
Nice Question.
I have done it.
Bvenkat Jeewesh