0

C# unlike Java don't allows to define classes inside an interface. What is the correct place for defining classes that are used only in interface functions?

Currently I'm using the following architecture

namespace MyNameSpace
{
    public class MyReturnType 
    {
        // Contents
    }
    public class MyArgsType 
    {
        // Contents
    }
    public interface ICalibrationCheckController
    {
        MyReturnType PerformCalibrationCheck(MyArgsType arg);
    }
}

But this architecture requires a separate namespaces for each interface definition. Otherwise if I'll define in the same namespace the other interface there will be difficult to separate what classes are used by the first interface and what by the other interface.

Ideally I'd like to define necessary classes inside interface definition but it is not allowed.

namespace MyNameSpace
{
    public interface ICalibrationCheckController
    {
        public class MyReturnType 
        {
            // Contents
        }
        public class MyArgsType 
        {
            // Contents
        }
        MyReturnType PerformCalibrationCheck(MyArgsType arg);
    }
}

What approach are you using and what is correct?

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Anton Bakulev
  • 286
  • 2
  • 7
  • 3
    "But this architecture requires a separate namespaces for each interface definition." - Why does it? – ProgrammingLlama Jul 23 '18 at 09:27
  • 2
    In most cases I've seen it, people do it the way shown in your first code sample (i.e. sharing a single namespace rather than separate namespace per interface). – mjwills Jul 23 '18 at 09:27
  • 1
    "the other interface there will be difficult to separate what classes are used by the first interface and what by the other interface" I don´t see a huge problem on this. But anyway there´s not much difference on your interface in java, as it serves as kind of a namespace for your class anyway. So in fact there´s no difference on `ICalibrationCheckController.MyReturnType` (JAVA) and `MyNamespace.MyReturnType` (C#). – MakePeaceGreatAgain Jul 23 '18 at 09:37
  • @john, If I'll define in the same namespace the other interface there will be difficult to separate what data type classes are used by the first interface and what by the other interface. – Anton Bakulev Jul 23 '18 at 10:11

1 Answers1

2

I can´t see a huge difference here. In JAVA you have to provide the name of the interface to qualify the class, whereby in C# you´d use a namespace. In fact the JAVA-syntax thus serves as some kind of a namespace. So the only thing you really save is some typing-afford to type namespace MyNamespace.

Having said this you could do this in JAVA:

interface MyInterface
{
    MyClass DoSomething();
    class MyClass { }
}
interface AnotherInterface
{
    MyClass DoSomething();
    class MyClass { }
}

In C# the similar approach is this - as you´ve already mentioned:

namespace MyNamespace
{
    interface MyInterface { MyClass DoSomething(); };
    class MyClass { }
}
namespace AnotherNamespace
{
    interface AnotherInterface { MyClass DoSomething(); };
    class MyClass { }
}

Using the types in JAVA would be as follows:

MyInterface.MyClass m = interfaceInstance.DoSomething();

in C# the following would be valid:

MyNamespace.MyClass m = interfaceInstance.DoSomething();

In both languages you´d have to qualify the complete name of the class. In C# you´re doing this via its namespace, in JAVA via its interface (which serves as namespace however).

So there´s not really a huge difference, you have to provide the namespace/interface anyway. Only little difference I can see is that you need to type namespace in C# and some braces.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111