tags:

views:

405

answers:

8

I'm wondering how many classes you can inherit from in .net. There are several backend cs 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.

+12  A: 

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

Tom Vervoort
While this is correct, I don't think actually answered the question cfarm54 was asking.
Zachary Yates
The question was: Is there a way to inherit multiple classes? Although I didn't offer a solution, I DID answer the question correctly.
Tom Vervoort
+5  A: 

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.

ChrisF
(-1)"You can achieve the same ends using interfaces." False. The user explicitly stated that he would like to 'share separate static methods'. Interfaces do not achieve the same end. I'm not supporting multiple inheritance, I'm just saying that your statement is wrong.
@devinb - answer updated.
ChrisF
@Chris downvote removed.
+3  A: 

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.

Claudio Redi
+12  A: 

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.

j0rd4n
@jordan most of the methods that I will be using from one of the classes are utility methods. if this is the case, how would you recommend that I go about using/not using inheritance.
cfarm54
In this case just do, StaticClassName.StaticMethodName(...) inside of the class that is using it. You don't need inheritance for this scenario. A simple call will work just fine. Inheritance should only be used to create new classes that are a more "specialized" type of the parent class - NOT to grant access to methods outside of the client class.
j0rd4n
+7  A: 

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.

Zafer
@Zafer thanks this is the answer I was looking for paired with @jordan
cfarm54
You're welcome...
Zafer
+1  A: 

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();
    }
}
XIII
+2  A: 

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.

Jay
This technique is called delegation. As Jay correctly states, it doesn't buy you much here.
Zachary Yates
A: 

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.