OT: Inheriting static methods works?

OT: Inheriting static methods works?

Old forum URL: forums.lhotka.net/forums/t/554.aspx


ajj3085 posted on Thursday, July 06, 2006

Hi all,

Just noticed something.  I have a class with a static method, like so:

public abstract class MyBase {

      public static void MyMethod<T>( ArrayList list );

}

I noticed that sub classes could 'use' MyMethod.. for example:

public class MySubClass : MyBase {  }

MySubClass.MyMethod<MySubClass>( new ArrayList() );

And it compiles just fine.  Now I thought static methods didn't get inherited, so I would think that the above line wouldn't compile... but it does (indeed, everything executes as it should).

Was I wrong about inheritance and subclasses?  Or is this something new?

Also offtopic, how are people color coding their code?  Manually?  I'm using the RichTextEditor (for developers) and even though I manually colored the code above, it didn't show up.  Huh? [:^)]

Andy

skagen00 replied on Thursday, July 06, 2006

From what I've found it seems that one can include static members and methods in generics, at which point each type of the generic will have their own "static member". However, inheriting (which is different from usinggenerics) from a non generic, if I'm right, shouldn't give you the statics - they belong to the ancestor class. The thing with generics is that they're really not a class themselves, just a template for other classes.

If you do FxCop, though, it frowns on using statics within generics - I'm not entirely sure why, I haven't read an associated article.

 

Copyright (c) Marimer LLC