简体   繁体   中英

How many classes can you inherit from in C#?

How many classes can you inherit from in .NET ?

There are several backend C# files that I would like to share separate static methods, but they are all in different classes. Is there a way to inherit multiple classes?

Example code would be much appreciated.

C# does not support multiple inheritance (meaning a single class inherits from multiple classes). You can, however, implement multiple interfaces in a single class.

As far as chaining together inherited classes, there isn't a limit per-se. Just keep in mind the complexity you will introduce to your system. When using inheritance, make sure you are using it in a "is a" scenario. A cheeta is an animal. A mazda is a car. Otherwise, your inheritance tightly couples your classes with a design that becomes much more difficult to maintain.

If they are static "utility" methods, just invoke them directly without inheriting. Unless those methods belong to the entity you are creating, you should not use inheritance.

You can only inherit from a single class. It is however possible to implement multiple interfaces.

Let's say you have Utils.cs:

internal class Utils {
    public static void Log(string str) {
        // log str to a file...
    }
}

and there is a DB.cs that uses Log method and in the same namespace with Utils class:

public class DB {
    public void SaveToDB() {
        // some db operations
        Utils.Log("DB operation successful");
    }
}

Since Log method is static, you don't need to initialize Utils class to use this method.

As a conclusion, you cannot use multiple inheritance but for your case, you don't also need that. You can use static methods without multiple inheritance.

You can't do multiple inheritance in C#.

You could achieve the same ends using interfaces if it weren't for the fact that you'd like to share static methods.

You can inherit in "chain" as many classes you want but multiple inheritance is not allowed on .NET languages.

Your only option for multiple inheritance in .NET are interfaces.

As long as the accessors are set appropriately, static methods are more or less shared anyway (in fact, they use the keyword Shared in VB); it makes no difference what class they are in because they are disconnected from any instantiated object.

To call a static method, you preface it with the name of the class on which it is declared:

MyClass.MyUtilityMethod();

The accessor must be public or it can be internal if the caller and method are in the same project/assembly.

If your goal is merely to create a single point of access for the static methods, you create a class that forwards the calls as appropriate:

public static class SharedMethods
{
    public static void SharedMethod1()
    {
        ClassA.SharedMethod1();
    }

    public static string GetName()
    {
        return NameProvider.GetName();
    }

    public static int GetCount()
    {
        return CountClass.GetCount();
    }
}

This way you can access everything through SharedMethods.GetName() etc.

In .NET you can only inherit from one base class but implement multiple interfaces.

For an ASP.NET specific situation like you're in you can do the following:

public class BasePage : Page
{
    public string SomeMethod() { //Magic happens here }
}

and have your webforms inherit from BasePage in codebehind.

Another often followed course is to make a custom static class which has all the static methods on it. Be sure if you're working with ASP.NET specific things to prefix them with HttpContext.Current.

For example:

public static class HelperClass
{
    public static string GetUsernameFromSession()
    {
        return HttpContext.Current.Session["Username"].ToString();
    }
}

You cannot inherit from multiple classes. However, you can string together classes that inherit off of each other.

Class Employee

Class Manager : Employee

class RegionalDirector : Manager

So if you have a whole naming/employee ID/contact info scheme going on within your program, you can inherit that from employee. Then, elaborate with information for the manager, and then even more info for regional director.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM