By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,418 Members | 1,430 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,418 IT Pros & Developers. It's quick & easy.

Forcing Generic Types....

P: n/a
Hey Everyone

I know that I can do something like this in C#
MyClass<WHATEVER>
{
private WHATEVER var_name_of_type_whatever;
public ChangeVar(WHATEVER change_to_value)
{
var_name_of_type_whatever = change_to_value;
}
}

But is there a way to do this?

MyClass<WHATEVER:RealInterfaceClass>
{
private WHATEVER
var_name_of_type_whatever_that_is_forced_to_be_of_ basetype_RealInterfaceClass;
public CallMethodThatExistsOnRealInterfaceClass()
{

var_name_of_type_whatever_that_is_forced_to_be_of_ basetype_RealInterfaceClass.MethodIKnowExists();
}
}

so that in my templated code I can treat that variable as a
RealInterfaceClass and call its methods?

I'm just curious if something like this is possible, instead of just
having the variable be of type RealInterfaceClass and having to upcast
it to the derived class when I'm using the object.

Thanks
Mustafa
Jul 14 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
MMAS wrote:
Hey Everyone

I know that I can do something like this in C#
MyClass<WHATEVER>
{
private WHATEVER var_name_of_type_whatever;
public ChangeVar(WHATEVER change_to_value)
{
var_name_of_type_whatever = change_to_value;
}
}

But is there a way to do this?

MyClass<WHATEVER:RealInterfaceClass>
{
private WHATEVER
var_name_of_type_whatever_that_is_forced_to_be_of_ basetype_RealInterfaceClass;
public CallMethodThatExistsOnRealInterfaceClass()
{

var_name_of_type_whatever_that_is_forced_to_be_of_ basetype_RealInterfaceClass.MethodIKnowExists();
}
}
Yes, it's called constrained genericity. The C# syntax is like this:

class MyClass<Twhere T : Type{
...
}

where Type can be either a base class, an interface or another type
parameter. It also supports constraints to indicate that a type must be a
reference (where T : class) or a value type (where T : struct), though these
are less common, and a constraint that a type must have a default
constructor (where T : new()).

--
J.
Jul 14 '08 #2

P: n/a
On Jul 14, 2:39*pm, Jeroen Mostert <jmost...@xs4all.nlwrote:
MMAS wrote:
Hey Everyone
I know that I can do something like this in C#
MyClass<WHATEVER>
{
* private WHATEVER var_name_of_type_whatever;
* public ChangeVar(WHATEVER change_to_value)
* {
* *var_name_of_type_whatever = change_to_value;
* }
}
But is there a way to do this?
MyClass<WHATEVER:RealInterfaceClass>
{
*private WHATEVER
var_name_of_type_whatever_that_is_forced_to_be_of_ basetype_RealInterfaceClass;
*public CallMethodThatExistsOnRealInterfaceClass()
* {
var_name_of_type_whatever_that_is_forced_to_be_of_ basetype_RealInterfaceClass.MethodIKnowExists();
* }
}

Yes, it's called constrained genericity. The C# syntax is like this:

* *class MyClass<Twhere T : Type{
* * *...
* *}

where Type can be either a base class, an interface or another type
parameter. It also supports constraints to indicate that a type must be a
reference (where T : class) or a value type (where T : struct), though these
are less common, and a constraint that a type must have a default
constructor (where T : new()).

--
J.
Great, thanks so much.
Jul 14 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.