Why static method cannot be abstract method? 
Author Message
 Why static method cannot be abstract method?

Dear All,
I want to create one skeleton class for my team to implement the coding. I
prepare one static method let's say
public static void SomeMethod(){

Quote:
}

I want to set this as abstract method, but compiler doesn't allow me to do
this. Why C# prevent us to create abstract+static method, I want to know the
drawback or something.
Thanks for your answer


Sat, 09 Apr 2005 11:21:00 GMT  
 Why static method cannot be abstract method?
The technical reason for statis methods not being astract is that there is
NO INHERITANCE FOLLOWED WITH STATIC METHODS.

Means: ClassA.StaticMethodA ()

will ALWAYS result in the method on class a called.

If class B overwrites the method, you need to call ClassB.StaticMehodA (),

Also, classes do not implement interfaces (their instances do). This means
that as you can not call a static method without specyfind the EXACT class
name, abstract static methods make no sense. Similar to overwriting.

--
Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)
--
Still writing SQL and dealing with DataSets?
  Why dont you use our EntityBroker - the most advanced o/r and business
object toolkit in town.


Quote:
> Hi There!

> Try using delegates.
> Then You can define them as a static members in your class.

> Mehtron


> > Dear All,
> > I want to create one skeleton class for my team to implement the coding.
I
> > prepare one static method let's say
> > public static void SomeMethod(){

> > }
> > I want to set this as abstract method, but compiler doesn't allow me to
do
> > this. Why C# prevent us to create abstract+static method, I want to know
the
> > drawback or something.
> > Thanks for your answer



Sat, 09 Apr 2005 18:00:52 GMT  
 Why static method cannot be abstract method?

Quote:
> I want to set this as abstract method, but compiler doesn't allow me to do
> this. Why C# prevent us to create abstract+static method, I want to know the
> drawback or something.

It simply makes no sense to have a static and abstract method. A static
method doesn't belong to an object, it belongs to a class. You can't
overwrite a static method, so why You would have it abstract? Where else
will You define the body of the method? If You don't want to have the
method in the base class, so dont't put it in there, only put it in the
derived classes.

--
Sleepless in Berlin
Wernfried



Sat, 09 Apr 2005 16:56:43 GMT  
 Why static method cannot be abstract method?
Hi There!

Try using delegates.
Then You can define them as a static members in your class.

Mehtron

Quote:

> Dear All,
> I want to create one skeleton class for my team to implement the coding. I
> prepare one static method let's say
> public static void SomeMethod(){

> }
> I want to set this as abstract method, but compiler doesn't allow me to do
> this. Why C# prevent us to create abstract+static method, I want to know the
> drawback or something.
> Thanks for your answer



Sat, 09 Apr 2005 17:48:28 GMT  
 Why static method cannot be abstract method?
Hello Niran,

Wernfried gave you very nice conceptual answer, and if you need
more technical detail, here goes:

For handling virtual (and abstract as well) methods the compiler
creates so-called "virtual method table" that allows tricks
with calling overriden methods using a reference only to an
interface or to a base class since every instance holds a pointer
to a table pertinent to its actual class.

Since static methods exist for the class itself and not for an
instance, they cannot be neither virtual nor abstract since there
is no virtual table available for the class itself (although single
virtual method table instance is shared between all instances of the
class).

--
Dmitriy Lapshin
X-Unity Unit Testing and Integration Environment
http://x-unity.miik.com.ua


Quote:
> Dear All,
> I want to create one skeleton class for my team to implement the coding. I
> prepare one static method let's say
> public static void SomeMethod(){

> }
> I want to set this as abstract method, but compiler doesn't allow me to do
> this. Why C# prevent us to create abstract+static method, I want to know
the
> drawback or something.
> Thanks for your answer



Sat, 09 Apr 2005 17:59:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Why static method?

2. using static methods -- why not use?

3. Possible bug when (not) implementing abstract methods

4. Guard specific type against abstract method implementation

5. Prevent overriding method in abstract base class ?

6. Can methods in abstract classes have implementation?

7. Tunnelling service methods into a single method

8. Cannot access base method 2

9. Cannot access base method

10. Can a method be declared inside another method?

11. Automation question - Cannot use AddClass method

12. CLS-Compliant Method Cannot Return an enum?

 

 
Powered by phpBB® Forum Software