Coding Challenge 2004125-2

In the C# language, when you overload a function in a class, each overload has to have different function arguments, you can't just overload based on return type. Given this fact:
  1. Why is the compiler allowing me to have two Test class methods named FunctionA with the same number (zero) of parameters?
  2. With respect to the previous question: why does the compiler allow me to have two FunctionA methods with the same number of parameters, but differing return types?
  3. If I change the FunctionA method in the Test class to return void, will this code still work? Why?

using System;

 

namespace TestNamespace {

    interface ITest {

        void FunctionA();

        void FunctionB();

    }

 

    class Test : TestNamespace.ITest {

        public int FunctionA() {

            Console.WriteLine("FunctionA as a word sounds derogatory!");

            return 0;

        }

 

        void ITest.FunctionA() {

            Console.WriteLine("FunctionA as a word sounds derogatory!");

        }

 

        void ITest.FunctionB() {

            Console.WriteLine("FunctionB sounds fine.");

        }

    }

     

    class App {

        [STAThread]

        static void Main(string[] args) {

            Test t = new Test();

            int x = t.FunctionA();

 

            ITest it = new Test();

            it.FunctionA();

            it.FunctionB();

        }

    }

}

Too challenging? :-)

my best shot at it:

There is really no overloading going on and the compiler allows you to implement both FunctionA() and ITest.FunctionA() because they have different fully qualified names.

So as long as you avoid name collision, you are all set.

  1. with the exception of the return value of FunctionA() and the casting problem found in Main, yes.

NERD ALERT NERD ALERT NERD ALERT

Nerd alert? Totally....and it pays well.

Matt, you are correct...can you be more specific as to why? It has something to do with interfaces in general.

i assume it has something to do with allowing a class to implement multiple interfaces and how C# allows a progammer to specify which method they are implementing to resolve name collisions.

this is a guess so i don't think i can be more specific, i was hoping you would fill in what i am missing.

My class is implementing the interface, but it is doing it explicitly and notice how by default methods are private. So the class Test only has the method "public int FunctionA()" available to it...but when the Test class is cast to the interface ITest, it has "void ITest.FunctionA()" and "void ITest.FunctionB()" available to it. There are a few classes in the .NET Framework that do this. For example, the Array class implements IList so that it can be used for databinding, etc...but the actual IList members which are required to be implemented by the Array class are done so privately (for performance reasons with the compiler) so when cast to IList it can be used for databinding or whatever services IList provides.

What I was explaining in my last post with respect to System.Array implementing IList might make more sense to see in code, so here's a quick example:



using System;

 

class App

{

      [STAThread]

      static void Main(string[] args)

      {

        Array x = Array.CreateInstance(typeof(string), new int[]{2});

 

        System.Collections.IList list

            = (System.Collections.IList) x;

 

        list[0] = "Hello";

        list[1] = "World";

        list.Clear();

       

        //Can't do this because Clear() is not a method of

        //the type System.Array

        //x.Clear(); 

      }

}