As I know, abstract methods can be ONLY exist in abstract classes.
In spite of this, I can see Microsoft did this (in Xamarin.Forms):
public static class DependencyService
{
….
public static void Register<T>() where T : class;
….
}
This is an abstract method in a NON abstract class. How can it be?
This is an abstract method in a NON abstract class
No? It is not. Where do you get the idea it is an abstract method? It is a open type method that you can call by providing the type (Register()) and has NOTHING to do with abstract.
Now, you say - it has no body. Sure? It looks a lot more like you just do not SEE the body because you do not have access to the code and this could be a "open reference" style code without access to the source code style (showing signature but not body).
An abstract method must have the abstract keyword specified.
The method signature in your question:
public static void Register<T>() where T : class;
does NOT have the abstract keyword.
The Xamarin.Forms is an open-source project, you can look at the source code in their GitHub repositroy.
Click here to see the actual code of the method you're referring to.
Related
I'm tring to do :
public abstract class Base
{
public abstract Task Execute();
}
public abstract class Concrete<T> : Base where T : class
{
new public abstract Task<T> Execute();
}
But for some reason I am getting the compiler error :
CS0533 'Concrete.Execute()' hides inherited abstract member 'Program.Base.Execute()
I've hidden plenty of members in the past but never met this side case and I'm quite puzzled here. Spent a long time on MSDN and the web but couldn't find anything about this behaviour.
I would really appreciate any insight on the issue.
Here's the fiddle.
The problem is that the base method is abstract. A class inheriting from Concrete<T> would have to override Base.Execute(), but it could not override it, because it is hidden by Derived<T>.Execute(). So, Concrete<T> would be an abstract class that can't possibly have any implementation (at least not in C#), at thus it would be useless. So, the C# compiler does not let you write it.
If Base was an interface, you could work around this by using an explicit interface implementation. But there is nothing like explicit base class implementation, so I don't think there is any way to have this kind of code, at least not without renaming one of the two methods.
From MSDN :
An abstract method declaration introduces a new virtual method but
does not provide an implementation of that method. Instead,
non-abstract derived classes are required to provide their own
implementation by overriding that method
Well, the reason for this error is how abstractions work in C#, abstraction can be inherited, it can be implemented but it cannot be hidden or replaced by another abstraction.
Besides, consider the code :
public abstract class Base
{
public abstract Task Execute();
public abstract Task<int> Execute();
}
This will not compile, because :
Type 'Base' already defines a member called 'Execute' with the same
parameter types
So why should it work when we move the second method to a derived abstract class ?
Wait, before you start thinking, I would like to clear that I am NOT going to ask the routine differences between Interface and Abstract.
I had gone through the difference between Abstract and Interface in MSDN.
It is said :
By updating the base class, all inheriting classes are automatically updated with the change.
Interfaces, on the other hand, cannot be changed once created. If a new version of
an interface is required, you must create a whole new interface.
See this : -
Can anyone prove this using following example: -
abstract class WashingMachine
{
abstract public void Wash();
}
class Philips : WashingMachine
{
public Philips() { }
override public void Wash(){//Wash code here....}
}
class Samsung : WashingMachine
{
public Samsung() { }
override public void Wash(){//Wash code here....}
}
class Videocon : WashingMachine
{
public Videocon() { }
override public void Wash(){//Wash code here....}
}
Now, If I added following new abstract method in WashingMachine : -
abstract public void Rinse(int loadSize);
How all inheriting classes (i.e. Philips/Samsung/Videocon) will automatically get updated with the change?
They won't get updated - you still have to manually add an implementation of Rinse to each and every class that inherits from WashingMachine.
What I believe the MSDN says is that if you have a non-abstract method defined in an abstract class and you change that method, all classes that inherit from the abstract class will benefit from the change automatically.
Your concrete derived types all need to provide an implementation of any inherited abstract member...thus your types will still need to provide a Rinse implementation when you modified the base.
http://msdn.microsoft.com/en-us/library/sf985hc5(v=vs.71).aspx
This is a confusion programmers normally get when read difference between interface and abstract class. the line you refer is only true if the change in the abstract class is concrete. what i mean is you added a property or a method with implementation. Or if i am more specific, any change in base abstract class which does not make any concrete class abstract. Such change is possible in abstract classes and there is no way you can add such change in interface.
How all inheriting classes (i.e. Philips/Samsung/Videocon) will automatically get updated with the change?
They will suddenly become non-compileable, isn't that an update?
But I'd like to point out that there's in fact no difference between abstract method in abstract class and method in interface. Main difference lies in the fact that abstract classes can have methods with implementation and interfaces can not.
In fact, I'd say that I don't like MSDN definition and find it confusing and somewhat misleading.
Unfortunately, I can't show code, but here's the story:
I'm supposed to learn how a program we use at work works. I traced the flow of data from a user interface element into the deep internals of a function. But now, inside of a class definition I got stuck. The data I'm tracking is passed to a function. In the class there's a line with a function signature for that function, but no implementation.
How do I go about finding the implementation? All the code (except for Microsoft's) was developed in house and should reside within the project, but Go To Definition only brings me back to the signature.
We're using C# and .Net 4.0.
Here's the line:
public abstract class SomethingDoer : SomethingElse
// ...
protected abstract void DoSomething(T1 param1, T2 param2, T3 param3);
Now I'm looking for the implementing class by looking for References to SomethingDoer, but unfortunately the break point isn't hitting. Do I have the wrong class or am I missing something about abstract functions?
Without code this is really hard to answer. A function definition without implementation is usually an interface or abstract. Interfaces can have only definitions, while abstract can mix both:
public interface ISomeInterface {
void SomeMethod();
}
public abstract SomeAbstractClass {
public abstract void SomeMethod();
public void AwesomeMethod() {
// I do awesome things; look at my method body!
}
}
If you're really looking at the source code, it could be
an abstract method
a partial method
an extern method
In the first case, the implementation is in the class deriving from this class. In the second case, the implementation is in another "part" of the definition of this class, probably in another file. In the third case the implementation is inside some (native) DLL that is being imported.
Another possibility is that you're not actually looking at the source code, but only at metadata generated from an assembly reference in your C# project file.
So which of the keywords abstract, partial, or extern do you see with the method?
It's mean you have only compiled class without sorces. May be some DLLs?
Are you possibly looking at an interface?
Interfaces have the defined functions but no implementation. It's used to show that they exist and must conform to a spec.
Can't you post sample code and change the wording for us to see?
Is there a way of putting a static method in an abstract class that can return the derived type?
Does a static method even know what type it is even being called from in C#?
For example, a base class could be
public abstract class MyBase
{
public static IEnumerable<TDerivedType> LoadAll()
{
//functionality here
}
}
Then if MyDerivedType inherits MyBase, I'd like to be able to call MyDerivedType.LoadAll()
Nothing too important - I'm currently using a generic static method and calling MyBase.LoadAll<MyDerivedType>(), which works fine but it doesn't look quite as 'pretty' as this would be.
Static members aren't inherited, so the static method has to be told in some way what the derived type is. Your solution is one way. Another is the following:
public abstract class MyBase<T> where T : MyBase<T> {
public static IEnumerable<T> LoadAll() { }
}
Then:
class Derived : MyBase<Derived> { }
var all = MyBase<Derived>.LoadAll();
That said, I think there is something wrong with your model. MyBase represents something in your domain (of which they are more specific derived types) AND it knows how to load all of those objects? That's two responsibilities, and that ain't cool yo.
No, there currently isn't a way to do this. I'd possibly use a factory in this case
var all = MyClassFactory.LoadAll<MyDerivedType>();
An abstract class can never be instantiated(that's the whole point) so any static methods would have to be implemented in each child class.
From an MSDN Thread
Static methods can be defined in an abstract class. However, you cannot force a derived class to implement a static method. If you think about it, such a method would be useless. Static methods are invoked using type names, not instance variables. If I call MyBaseClass.MyMethod, then MyBaseClass.MyMethod will always be invoked. How would it do you any good to force MyChildClass, which inherits from MyBaseClass, to also a implement a static MyMethod?
(Note: edited implemented to instantiated in the first sentence.)
There is nothing wrong with the way you are doing this. In fact most of MS generic extension methods are designed like this.
As for:
"Does a static method even know what type it is even being called from in C#?"
Its not a question of the static method knowing, its a question of the compiler knowing. When the code is scanned by the compiler this is when all the types are consolidated. At this point it can work out what code calls what functions and what types need to be returned. This is also the reason that a var type cannot be returned from a function.
I have a MustInherit class with some MustOveride Methods in it. When i inherit form that class, I automatically get the MustOveride Methods or properties.
My question is, I want to be able, to inherit from a class, get my MustOveride functions and methods, but then with some code already in it. I've once seen a .net class that did it, when I inherited from that class, I got the methods, with some comments in it.
Does anybody have an idea what i mean? (It a bit hard to describe ;-) )
I think what you described is known as Template Method Pattern:
public abstract class MyClass
{
public void SomeMethod()
{
// code
MustInherit();
// code
}
protected abstract void MustInherit();
}
Create a method which will not be overridden SomeMethod in this sample and stuff all common code into this class. Then create an abstract method which must be overridden.
If you want to provide a default implementation, so the method must not be overridden but can be use the virtual keyword.
public abstract class MyClass
{
public void SomeMethod()
{
// code
MustInherit();
// code
}
protected virtual void CanInherit()
{
// default implementation
}
}
I assume, you want to do have the following:
When you inherit from that abstract base class, you want to have those abstract methods already partly implemented in your class.
This is not possible out of the box. It could be achieved with some templating, but that has nothing to do with C# or VB.NET but with the IDE used.
The only thing you can do is to create that method as virtual (C# - I don't know how it is called in VB.NET) in the base class and call the base implementation in the derived class.
An Abstract class for you service :)
If you need that consumer of your abstract class ovverides some methods for sure then mark them as abstract too. If you need just to provide possibility of ovveriding you methods but this is not definitely necessary then mark them as virtual.
With the virtual keyword you are not forced to implement the inherited method, then it will use the default implementation of the base class. In that way, you kind of inherit all the code from the base method.
Otherwise, you can implement you own derived version of the method, and somewhere in it call the base class' version of method : base.MethodName(...);. That allow you to kind of inherit all the code from the base method once again, but this time with additional code before and after which is specific to your derived class.
Otherwise, you can make your base class' method such that it uses delegates in its code and call it here and there. Thus the fundamental functioning of the base class' method remain the same for all the derived classes, but each derived class provides its own delegates to adjust some detail key blocks of code in the base class' method.
Otherwise, if you want to see partially implemented methods with comments here and there like Add your code here, it's typically a matter of code generated by an external tool like Visual Studio or another IDE and has nothing to do with the language itself.
But as you see there are plenty of possibilities, depending of you you want precisely...