473,387 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Generics and static members

Hi,

I've encountered a rather interesting language problem. Suppose I've
got following types:

//class providing usefull static methods
public class GoodBase<T>
{
public static void Met(){};
}

public class SomeEntity
: GoodBase<SomeEntity>
{
}

public interface IDictModel<T>
where T : GoodBase<T>
{
}

public class DictModel<T>
: IDictModel<T>
where T : GoodBase<T>
{
public void Test()
{
GoodBase<SomeEntity>.Met(); //OK
SomeEntity.Met(); //OK
T.Met(); //won't compile
}
}

The question is why can't I call a static method using generic
parameter?

I've specified that T is of type GoodBase<T>, so static methods from
GoodBase should be available by using generic parameter... Isn't
GoodBase<SomeEntity>.Met() actually the same as T.Met()? Why do I have
to repeat myself?

Regards,
Filip Zawada
Sep 19 '08 #1
3 1712
On Fri, 19 Sep 2008 09:27:25 -0700, Filip Zawada <fz*****@gmail.comwrote:
[...]
The question is why can't I call a static method using generic
parameter?

I've specified that T is of type GoodBase<T>, so static methods from
GoodBase should be available by using generic parameter... Isn't
GoodBase<SomeEntity>.Met() actually the same as T.Met()? Why do I have
to repeat myself?
You don't. But, you also can't use T directly as the type.

It doesn't make sense to call static members on the type parameter. This
is true regardless of whether or not the type parameter inherits a generic
type or a non-generic one. Consider:

class A
{
public static void MethodA() { }
}

class B<T: where T : A
{
public void MethodB()
{
T.MethodA();
}
}

One of two possible behaviors may be expected here.

First, it may be expected that if the class T reimplements MethodA(), you
want the implementation in the most-derived class to be called. That is,
if T is not A and has its own implementation of MethodA, you want _that_
implementation to be called rather than A.MethodA.

If this is the case, then it's not possible for the compiler to fulfill
your intent, because it needs to know when it compiles the _generic_ class
how to get at the method, and at that point the type parameter hasn't
actually been resolved yet.

I suppose in theory, the compiler could generate some complex code to use
reflection to determine at run-time the correct static method to call.
But that could lead to a serious, non-obvious performance problem at best,
and at worst could cause some subtle, hard-to-find bugs. It's wise for
the language design to prohibit the use of a type parameter in that
context.

Of course, there's nothing stopping you from writing such code
explicitly. But in that case, it would be obvious looking at the code
that's what you're doing.

Alternatively, maybe you always want to call A.MethodA. If that's the
intent of the code, then you can get it to work by doing exactly that:

class B<T: where T : A
{
public void MethodB()
{
A.MethodA();
}
}

Note that if T inherits a generic type, then you can use the same
technique, supplying the type parameter:

class A<T>
{
public static void MethodA() { }
}

class B<T: where T : A<T>
{
public void MethodB()
{
A<T>.MethodA();
}
}

In your own example, it would look like this:

public class DictModel<T>
: IDictModel<T>
where T : GoodBase<T>
{
public void Test()
{
GoodBase<SomeEntity>.Met(); //OK
SomeEntity.Met(); //OK
GoodBase<T>.Met(); //OK
}
}

That ensures that if the Met() method depends on the type parameter, that
you actually get the implementation you desire (as opposed to calling
GoodBase<SomeEntity>() in a class where you don't actually know that the
type parameter T is "SomeEntity", which would be wrong).

Hope that helps.

Pete
Sep 19 '08 #2
On Fri, 19 Sep 2008 09:27:25 -0700, Filip Zawada <fz*****@gmail.comwrote:
[...]
The question is why can't I call a static method using generic
parameter?
By the way, I found these entries on Eric Lippert's blog:

http://blogs.msdn.com/ericlippert/ar...-part-one.aspx
http://blogs.msdn.com/ericlippert/ar...-part-two.aspx
http://blogs.msdn.com/ericlippert/ar...art-three.aspx

I haven't had a chance to read through them yet, but given Lippert's place
in the .NET/C# world, I would expect that these articles do a _much_
better job of explaining the issue than my short post ever could.

Pete
Sep 19 '08 #3
Thank you for the answer, Pete. It was very helpful.
Now as I know the reasons, it's so obvious.

Filip
Sep 28 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
16
by: bigtexan | last post by:
I would like to do the following and cannot figure it out. public class A<T> { public delegate T GetValueDelegate(A<T> var); public GetValueDelegate GetValue = new GetValueDelegate(B.GetValue);...
1
by: Ole Nielsby | last post by:
I'm puzzled at how static fields intersect with generics, as the following snippet illustrates. namespace MonkeyParty { abstract class Fruit { } class Apple : Fruit { } class Pear : Fruit {...
11
by: herpers | last post by:
Hello, I probably don't see the obvious, but maybe you can help me out of this mess. The following is my problem: I created two classes NormDistribution and DiscDistribution. Both classes...
1
by: Vladimir Shiryaev | last post by:
Hello! Exception handling in generics seems to be a bit inconsistent to me. Imagine, I have "MyOwnException" class derived from "ApplicationException". I also have two classes...
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
8
by: Gary Brown | last post by:
Hi, When I do the following public class IAmAGeneric<T> { ... public T AMember (T a, T b) { return a + b; }; }
7
by: SpotNet | last post by:
Hello NewsGroup, Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have a question on the application of Generics. Knowingly, Generic classes are contained in the...
4
by: CSharper | last post by:
I have a sample as shown in the bottom on the mail. I am trying to see without changing anything in the way the As and Bs handled. I want to make it DRY using generics. I am really interested in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.