Why type parameter cannot used to call static method

A

Andrus

I tried to use type parameter to call static method but got compile error
'T' is a 'type parameter', which is not valid in the given context.

Why this is not allowed ?
Should I really use a lot longer ModelGenericBase<T>.test1() ?

Andrus.



class VirtualGrid<T>
where T : ModelGenericBase<T> {

void test() {
//'T' is a 'type parameter', which is not valid in the given context
T.test1();
}
}

class ModelGenericBase<T> {
public static void test1() {
}
}
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Andrus said:
I tried to use type parameter to call static method but got compile error
'T' is a 'type parameter', which is not valid in the given context.

Why this is not allowed ?
Should I really use a lot longer ModelGenericBase<T>.test1() ?

Andrus.



class VirtualGrid<T>
where T : ModelGenericBase<T> {

void test() {
//'T' is a 'type parameter', which is not valid in the given context
T.test1();
}
}

class ModelGenericBase<T> {
public static void test1() {
}
}

You can't use the type parameter to call a static method, as there is no
way of specifying in the class header that the type has to have that method.
 
A

Andrus

Göran Andersson said:
You can't use the type parameter to call a static method, as there is no
way of specifying in the class header that the type has to have that
method.

I specified in class header that this type has this method:

where T : ModelGenericBase<T>

Is this bug ?

Andrus.
 
C

Christof Nordiek

Hi Andrus,

Andrus said:
I tried to use type parameter to call static method but got compile error
'T' is a 'type parameter', which is not valid in the given context.

Why this is not allowed ?
Should I really use a lot longer ModelGenericBase<T>.test1() ?

Yes, indeed. Why don't you want to do that?
Only readability?
C# doesn't support polymorphism of static members, so there would not be a
semantical difference.

For readability you could use a using alias directive.

Christof
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Andrus said:
I specified in class header that this type has this method:

where T : ModelGenericBase<T>

Is this bug ?

Andrus.

You specified that the type has to be of the type ModelGenericBase<T> or
inherit the type ModelGenericBase<T>. As static methods are specific to
the type and are not inherited, the type doesn't have to have the static
method if it only inherits ModelGenericBase<T>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top