-2

I write a simple class such as:

public class Pusher : IPusher,Hub
{
}

I get compile error( Interface definition is expected) because we should first inherit from class and then other interfaces.

But what is difference between public class Pusher : Hub,IPusher and public class Pusher : IPusher,Hub

We can see like this convection also in method parameter that has default value

public void ExampleMethod(int required, string optionalstr = "default string")

that we should define default value parametr last of parametes.

Is that related with CLR?

Community
  • 1
  • 1
  • 2
    To answer your first question, there is no difference, it's the same thing twice. – Pete Garafano Mar 09 '13 at 17:33
  • 1
    I have no idea what you are really asking here. I don't see the connection between putting the base class in front of the interfaces on a class definition and optional parameters. They are different things. – Colin Mackay Mar 09 '13 at 17:34
  • 1
    http://en.csharp-online.net/ECMA-334%3a_17.1.2_Class_base_specification – Tim Schmelter Mar 09 '13 at 17:35
  • Simular question is here: http://stackoverflow.com/questions/2059425/in-c-can-a-class-inherit-from-another-class-and-an-interface – Daniel Dušek Mar 09 '13 at 17:37
  • Because C# only has single inheritance and the derived class expects the base class to be inherited before any interfaces. – Dustin Kingen Mar 09 '13 at 17:59

2 Answers2

2

No, it's not related to the CLR, those are language specific limitations.

I can't find exactly why the inherited class has to be placed before the interfaces, but that is probably done for clarity. The class can only inherit from one other class, but it can implement multiple interfaces. It's easier to see what's happening if you know that the class that is inherited is always placed first.

The ordering of optional parameters is of a more practical nature. C# doesn't allow empty parameters as placeholders for optional parameters, so you have to put them last (otherwise it would only be possible to call the method with named parameters). You can't call the method like this:

ExampleMethod(42, );

To call with an optional value, you just omit it from the call:

ExampleMethod(42);

Thus, if the optional parameter was not last, it would not be optional, because you can't make a call like this:

ExampleMethod(, 42);
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • @ShahroozJefri: Yes, that is the rule, but it doesn't say anything about the reason for it. I haven't found any reason in the MSDN documentation either. – Guffa Mar 09 '13 at 17:41
1

Well, in the first case it's just a convention. The class should be first for better readability and for simplicity of compile time check I hope. But the arguments are other case. Imagine that you specify default value for the first argument and not the latest arguments. How the compiler is supposed to detect what you wanted to say.

Example: int func(int a, int b = 10, int c) and you call func(10, 20); What is this supposed to do? It's not possible to say that.

Edit (Example 2): int func(int a, int b = 1, int c, int d = 2, int e) and call func(1,2,3,4); In this case is not clear which variable has been omited. It could be both B and D.

Petr Kalandra
  • 335
  • 2
  • 7
  • I'm just going to be the devils advocate here. Couldn't `func(10, 20)` just call `func(a: 10, c: 20)`? – antonijn Mar 09 '13 at 17:38
  • Well in this simple case... Yes... But there are cases it is not possible. Using the syntax with explicitly named arguments is much better idea. – Petr Kalandra Mar 09 '13 at 17:41
  • @Antonijn Yes, but that's a different concept altogether. C# supports both optional and named parameters, but I can use optional params on their own without having to name them, and that would break. – Michael Stum Mar 09 '13 at 17:45
  • Got another example: int func(int a, int b = 1, int c, int d = 2, int e) and call func(1,2,3,4); And what now? – Petr Kalandra Mar 09 '13 at 17:45
  • @MichaelStum That was not my point. My point was that it could call the function, assigning the parameters given to the named parameters **first**, then assign optional parameters last. My point was not about named parameters. – antonijn Mar 09 '13 at 18:14
  • @PetrKalandra That was the example I was looking for. Could you perhaps update the answer? – antonijn Mar 09 '13 at 18:18