Reputation: 105
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:
*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.
Upvotes: 3
Views: 613
Reputation: 6524
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.
Upvotes: 0
Reputation: 66255
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.
Upvotes: 1
Reputation: 81503
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)
Upvotes: 3