3

I've done some searching but cannot seem to find an exact match on this question. If I've missed it, please redirect me.

In Delphi / Object Pascal you have two concepts:

  1. Class Method - allows you to invoke the method without requiring a class instances. However these methods still allow overriding in derived class (thus in some way still carrying some class information).
  2. Static Method - marks the method as static - thus no longer allowing virtual / override keywords - no class information / no inheritance "knowledge"

*You may correct me on the above.

Question: What is the equivalent of Delphi's class method (not static) in C#? I'd like to be able to declare a method that I can invoke without having an instance of the class - but I would like to be able to mark the base method as virtual and override it in derived classes.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Danie van Eeden
  • 105
  • 1
  • 8

3 Answers3

3

Class Method - allows you to invoke the method without requiring a class instances. However these methods still allow overriding in derived class (thus in some way still carrying some class information).

Are you sure?

Regardless, the closest thing we have is a class with a static method.

public class MyLovelyHorse
{
    public static int HowManyLegs()
    {
        return 4;
    }
}

but I would like to be able to mark the base method as virtual and override it in derived class

Sorry, no can do. There is no facility in C# to do this.

This is about the time you should probably take a tour of Classes and Structs (C# Programming Guide)

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • Thanks for the quick response. I'm relatively sure, unless my eyes are playing tricks on me. Test: I have an instance method in base class This instance method calls the class method (not marked as static) in the base class. Stepping through in debug mode shows that the derived class's class method is invoked. Precisely what I want, never expected this to happen though. And yes, will be gong through .net docs again. – Danie van Eeden Oct 25 '18 at 06:49
  • I think you might need to be a bit tighter with your key words here. Instance method are methods which require an object of its class to be created before it can be called. Static methods are the methods in C# that can be called without creating an object of class – TheGeneral Oct 25 '18 at 06:52
  • Hi thx again. Yes I agree with the meaning of the keywords. I think, though, that I have come to realize the reason fr the behaviour. I am calling an instance method first (instantiated object), then the instance method makes a call to a class method. Because I am not specifying the classname as part of the call (simply calling the function name), the class method now behaves as an instance method. Great..just never knew it would do that. ** should have posted code, would have made it easier. apologies) – Danie van Eeden Oct 25 '18 at 07:00
  • 3
    @TheGeneral in Delphi, `class` methods still have a `Self` parameter, but it points to the class type instead of an object pointer. And yes, `class` methods can be `virtual` and overriden. `static` methods, on the other hand, have no `Self` parameter, which is why they can't be `virtual`. – Remy Lebeau Oct 25 '18 at 07:50
  • 2
    @Danie: no, it still behaves as a class method, even if you call it from an instance. In an instance method of the class, you just don't have to qualify it with the class name, this is implied. – Rudy Velthuis Oct 25 '18 at 09:47
  • 2
    @TheGeneral: in Delphi, even *constructors* can be virtual. This is something you'll only see in Object Pascal derived languages. The virtuality of a constructor is useful if you call it from a metaclass. A metaclass contains the type of a class and calling a virtual constructor means you call the constructor of the actual class contained in the metaclass. In C#, this can only be *simulated* using GetType() and reflection, AFAIK. – Rudy Velthuis Oct 25 '18 at 09:53
  • @RudyVelthuis yeah i used to use delphi 5 and 6 back in the day, but i am struggling to remember all these details – TheGeneral Oct 25 '18 at 10:09
  • @ Rudy Velthuis yes, makes perfect sense. The implied qualifier for the class method call is the name of the current instance. The behaviour of the function doesn't change. – Danie van Eeden Oct 25 '18 at 11:44
1

Can't do that in C# - the thinking is that the static methods are always invoked against a particular type, there is a never an instance. Which is not true as you can pass type info of a derived class to a function that takes the basic class type...

If the function is not supposed to do anything fancy (e.g. it just returns a static value), you can get away with using a class attribute instead of a virtual static method.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • Thx for the quick response. Yes this would be a good work around for C# I think. Passing the type info along. Was just wondering as to whether this is implicitly supported. – Danie van Eeden Oct 25 '18 at 06:51
  • I don't think so - there is no C# equivalent of "class of" in Delphi .You can use generics with the "where" restriction on type, but generics are evaluated at compile time, they are not dynamic. – Dmitry Streblechenko Oct 25 '18 at 13:53
0

Delphi and C# have some similarities. They both dont support multiple inheritance.

Coming to your questions, for the methods where you intend to hide the method implementation of base class, you can use the new keyword.

The static in Delphi works the same way as C#. You cant override the static method. If you wish to enforce the same, C# also comes with a static and sealed (for the class) keyword.

Gauravsa
  • 6,330
  • 2
  • 21
  • 30
  • This is really about virtual class methods, which C# simply doesn't have. In Delphi, there are normal class methods, which also have a Self/this parameter, but for class methods, Self is the class, not an instance. These class methods can be virtual. But you can also mark them as static, which is then the same as in C#. Hiding with `new` is what Delphi does with `reintroduce`. – Rudy Velthuis Oct 25 '18 at 09:59
  • Don't forget that the main architect of C#, Anders Hejlsberg, was also the main architect of Delphi, in his Borland days. – Rudy Velthuis Oct 25 '18 at 10:01