Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 444 Vote(s) - 3.57 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How many classes can you inherit from in C#?

#1
How many classes can you inherit from in [.NET][1]?

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.

[1]:

[To see links please register here]

Reply

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

#3
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.
Reply

#4
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.
Reply

#5

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.
Reply

#6
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();
}
}
Reply

#7
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.
Reply

#8
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.
Reply

#9
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.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through