I don't quite get this method signature in c#

public someMethod(scData data): base(data)
{
}

Saw this method in some code I was looking at today and don’t understand what it’s meaning. Can someone please explain?

It’s a constructor with a call to the base class constructor.

2 Likes

Thank you.
Really? is that a normal convention? Seems like bad design. Isn’t base called before the actual method this way?

This is actually the recommended practice because you actually cannot call base() yourself, even there in the constructor… it is not analogous to calling base class methods.

I’m not a language weenie but I think the idea with inheritance in general is that you could not reliably do anything in the derived class constructor without first having the base part(s) of the object constructed. This at least makes sense to me.

1 Like

I’m not 100% sure what you mean by that, sorry. But yeah I get what you’re saying about needing the base constructed.
Edit: also why don’t you have to put the type like scData in the call to base?

All I mean is that with an overridden method you can do this:

public override void foo()
{
// possible stuff
  base.foo();
// possible stuff
}

Rivers of digital ink have been spilled on what is the best design: first, middle, last. It’s pretty easy to have a strong opinion one way or another. Opinions are like that. :slight_smile:

Even right now there are subtle undiscovered bugs out in production code because someone didn’t do it correctly for whatever codebase they were working in, including if they just wrote it moments earlier.

Inheritance is sharp knives all the way, requiring exquisite discipline and attention to detail. One slip of the finger (“I’ll just jam this line into the top of all our object constructors”) and boom, you’re off to the weird random crash races.

Because even though it’s grafted out at the end of the declaration, it is still a function call, so like function call sites you don’t say the type where you invoke it.

2 Likes

In a sense and slightly deviating from the question, no. C# “convention” has class and method names in PascalCase, not camelCase. Of course nobody forces it onto you, it still looks odd to most C# programmers.

2 Likes

Man, I feel dumb sometimes. Thanks much Kurt, always appreciate your knowledge. Makes sense that the call to base is a function call, it just feels a bit weird being where it is.

1 Like

LOL, nice one. Thanks.