c# - Calling static method on a generic class -
this question has answer here:
i have generic class program static method below:
class program { public static void main() { console.writeline("hi program"); console.readline(); } }
when try access static main method inside generic class program1 below:
class program1<t> : program t : program { public static void check() { t.main(); } }
i error :
't' 'type parameter', not valid in given context
however if use
public static void check() { program.main(); }
everything runs fine. can please explain mistake might committing?
when program1 : program
, telling program1 instances not of type program1, of type program, because inherits it.
but when program1<t>
, telling program1 can have independent type parameter in addition of it's own type, things independent type.
in case use program1 : program
, static method can following:
class program1 : program { public static void check() { program.main() // real thing avoid check method. // , use program1.main() in other places } }
in case of using program1<t>
, can't see explains usage, unless trying further thing didn't read in question. here, t not program, if set constraint did. t mere generic type. reasons use allow class work different types. if working 1 type, there's no reason use generic type, use program
.
Comments
Post a Comment