Why can not I have abstract fixed approaches in C#?

I've been collaborating with providers a reasonable little bit recently, and also I found an intriguing scenario where I intended to have an abstract class that had an abstract fixed method. I read a couple of blog posts on the subject, and also it type of made good sense, yet exists a wonderful clear description?

0
2019-05-09 11:05:56
Source Share
Answers: 5

We in fact bypass fixed approaches (in delphi), it is a little bit hideous, yet it functions simply great for our demands.

We utilize it so the courses can have a checklist of their readily available things without the class instance, as an example, we have a method that resembles this :

class function AvailableObjects: string; override;
begin
  Result := 'Object1, Object2';
end; 

It is hideous yet essential, in this manner we can instantiate simply what is required, as opposed to having all the courses instantianted simply to look for the readily available things.

This was a straightforward instance, yet the application itself patronizes - web server application which has all the courses readily available in simply one web server, and also numerous various customers which could not require every little thing the web server has and also will certainly never ever require an object instance.

So this is a lot easier to keep than having one various web server application for each and every customer.

Hope the instance was clear.

0
2019-12-05 02:25:47
Source

Another participant (McDowell) claimed that polymorphism just benefits object circumstances. That need to be certified ; there are languages that do deal with courses as circumstances of a "Class" or "Metaclass" type. These languages do sustain polymorphism for both instance and also class (fixed) approaches.

C#, like Java and also C+npls prior to it, is not such a language ; the static search phrase is made use of clearly to represent that the method is statically - bound as opposed to dynamic/virtual.

0
2019-05-20 23:38:45
Source

Static approaches can not be acquired or bypassed, which is why they can not be abstract. Given that fixed approaches are specified on the type, not the instance, of a class, they have to be called clearly on that particular type. So when you intend to call a method on a youngster class, you require to utilize its name to call it. This makes inheritance unnecessary.

Think you could, for a minute, acquire fixed approaches. Visualize this circumstance :

public static class Base
{
    public static virtual int GetNumber() { return 5; }
}

public static class Child1 : Base
{
    public static override int GetNumber() { return 1; }
}

public static class Child2 : Base
{
    public static override int GetNumber() { return 2; }
}

If you call Base.GetNumber (), which method would certainly be called? Which value returned? Its rather very easy to see that without developing circumstances of things, inheritance is instead tough. Abstract approaches without inheritance are simply approaches that do not have a body, so can not be called.

0
2019-05-10 06:14:57
Source

To include in the previous descriptions, fixed method telephone calls are bound to a details method at compile - time , which instead dismisses polymorphic actions.

0
2019-05-10 06:08:59
Source

Static approaches are not instantiated thus, they're simply readily available without an object reference.

A phone call to a fixed method is done via the class name, not via an object reference, and also the Intermediate Language (IL) code to call it will certainly call the abstract method via the name of the class that specified it, not always the name of the class you made use of.

Allow me show an instance.

With the adhering to code :

public class A
{
    public static void Test()
    {
    }
}

public class B : A
{
}

If you call B.Test, similar to this :

class Program
{
    static void Main(string[] args)
    {
        B.Test();
    }
}

Then the real code inside the Main method is as adheres to :

.entrypoint
.maxstack 8
L0000: nop 
L0001: call void ConsoleApplication1.A::Test()
L0006: nop 
L0007: ret 

As you can see, the call is made to A.Test, due to the fact that it was the A class that specified it, and also not to B.Test, despite the fact that you can write the code in this way.

If you had class kinds , like in Delphi, where you can make a variable describing a type and also not an object, you would certainly have extra make use of for digital and also hence abstract fixed approaches (as well as additionally erectors), yet they aren't readily available and also hence fixed telephone calls are non - digital in.NET.

I understand that the IL developers can permit the code to be assembled to call B.Test, and also settle the call at runtime, yet it still would not be digital, as you would certainly still need to write some sort of class name there.

Digital approaches, and also hence abstract ones, are just valuable when you're making use of a variable which, at runtime, can have several sorts of things, and also you hence intend to call the right method for the existing object you have in the variable. With fixed approaches you require to experience a class name anyhow, so the specific method to call is recognized at compile time due to the fact that it can not and also will not transform.

Hence, virtual/abstract fixed approaches are not readily available in.NET.

0
2019-05-10 05:28:47
Source