In C#, is inheritance from a method possible?
Asked Answered
N

5

5

Now, if the title sounds weird to you, consider this code, I was reviewing today (simplified):

public class MyService(
    IHttpClientFactory httpClientFactory) : 
        BaseService(), // WTF? a Method?
        IMyService
{
    //no explicit constructor, has default constructor
    //.... 
}

This actually compiles! The MyService class inherits something from BaseService, apparently a method, and implements the IMyService interface.

BaseService is an abstract class, only defining some static objects and some literal strings:

public abstract class BaseService
{
    public static List<MaintenanceWindow> RegisteredMaintenanceWindows = new List<MaintenanceWindow>();
    protected static string CERT_SERVICE_URI = "https://cert.example.com";
    public static string DEFAULT_DOMAIN = "default.example.com";
    // ....
}

What does MyService inherit here?

  • There is no actual method in the base class
  • Does it inherit the (default) constructor?

I have never observed this kind of C# inheritance, and also these inheritance docs from Microsoft do not mention it.

Neutron answered 20/8, 2024 at 7:27 Comment(3)
You need to read up on primary constructorsKiwi
Personally I do learn most new C# language features by using ReSharper, applying its suggestions to my code and later researching about what "this new thing" does. Most of the time, I'm very excited and happy about such discovered new features.Sligo
A pointless and confusing feature, but whatever...Diagnostics
L
5

The class is not inheriting a method.

Notice that MyService has a primary constructor. After the class name, there is a parameter list (IHttpClientFactory httpClientFactory).

As with all constructors, the primary constructor needs to call a base class constructor before it can do anything. The () after BaseService is the syntax to call a base class constructor in the primary constructor. The () is a(n empty) parameter list.

It is as if you have declared a constructor like this

public MyService(IHttpClientFactory httpClientFactory): base() {
    // ...
}

Similar to regular constructors, explicitly calling the base class constructor is not necessary here, because BaseService has a parameterless constructor. : base() can be omitted in the above code snippet. Similarly, class MyService(...): BaseService is also valid.

If the base class has no parameterless constructor, then it is necessary to add the (...) after BaseService, and pass the required parameters.

For more info, see the Initialise Base Class section.

Lifeboat answered 20/8, 2024 at 7:43 Comment(1)
I am choosing this as the answer over others, because it specifically addresses the "Method-Style" syntax.Neutron
F
7

This got possible after the introduction of primary constructors.

You can define a class like:

public class Foo(int arg1, int arg2)
{
    // ....
}

which is (almost) equivalent to

public class Foo
{
    public Foo(int arg1, int arg2)
    {
        // ...
    }
}

Now one also needs a way of calling the base class constructor from a primary constructor, and that's what's happening here. In my example, a derived class could read like

public class Derived() : Foo(2,3)
{
    // ....
}

Note that it's necessary to also create a primary constructor on the derived class when using this syntax. The () are not optional.

But you could of course again use the old style:

public class Derived2 : Foo
{
    public Derived2()
        : base(2, 3)
    {
        // ....
    }
}
Fredfreda answered 20/8, 2024 at 7:40 Comment(0)
F
5

It was added with .net 8 + C#12 Now you can use primary constructors.

// previous
public abstract class BaseService 
{
    private readonly int _someParam;
    public BaseService(int someParam) 
    {
        // some code
        _someParam = someParam;
    } 
}
// same code with primary constructor
public abstract class BaseService(int someParam) 
{
    private readonly int _someParam = someParam;
}

So if you are not doing any method calls in your constructor, you can now write it in that way.

So it is not any method inheritance. It is like it was before plain class inheritance with a bit more syntactic sugar.

Feer answered 20/8, 2024 at 7:41 Comment(0)
L
5

The class is not inheriting a method.

Notice that MyService has a primary constructor. After the class name, there is a parameter list (IHttpClientFactory httpClientFactory).

As with all constructors, the primary constructor needs to call a base class constructor before it can do anything. The () after BaseService is the syntax to call a base class constructor in the primary constructor. The () is a(n empty) parameter list.

It is as if you have declared a constructor like this

public MyService(IHttpClientFactory httpClientFactory): base() {
    // ...
}

Similar to regular constructors, explicitly calling the base class constructor is not necessary here, because BaseService has a parameterless constructor. : base() can be omitted in the above code snippet. Similarly, class MyService(...): BaseService is also valid.

If the base class has no parameterless constructor, then it is necessary to add the (...) after BaseService, and pass the required parameters.

For more info, see the Initialise Base Class section.

Lifeboat answered 20/8, 2024 at 7:43 Comment(1)
I am choosing this as the answer over others, because it specifically addresses the "Method-Style" syntax.Neutron
N
4

What you are seeing is new feature of C# - primary constructors. It allows to define class and also define constructor inline with the class definition:

public class MyClass(string message, string etc)
{
    // ... some stuff
}

This naturally expands on invoking base class constructor. In old way it used to be:

public LineOfCreditAccount(string accountID, string owner, decimal creditLimit) 
    : base(accountID, owner)
{
    _creditLimit = creditLimit;
}

With new approach it is

public class SavingsAccount(string accountID, string owner, decimal interestRate) 
    : BankAccount(accountID, owner)
{
    // ...
}

In your case, base class constructor is just parameterless constructor.

So, this is still good old class inheritance, not a method inheritance.

Reference, from which examples were taken: Tutorial: Explore primary constructors

Nicholas answered 20/8, 2024 at 7:40 Comment(0)
D
0

Q: In C#, is inheritance from a method possible?

A: No, it's impossible.

In your example, it is not method inheritance, this is normal class inheritance.

C# 12 introduces primary constructors, a concise syntax to declare constructors whose parameters are available anywhere in the body of the type.

For more details, please see the Microsoft article - Primary constructors.

Deirdredeism answered 20/8, 2024 at 8:29 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.