471,305 Members | 1,311 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Generics, constraints and overloading

I'd like to provide two generic methods with the same name and
the same arguments, but with different constraints, such as:

void Explore<T>(T[] a, T[] b) where T : struct { }
void Explore<T>(T[] a, T[] b) where T : class { }

The value types should use the first method and the reference types
the second method. However, this won't compile, as the C# compiler
reports that there is already a method defined with the same name
and the same arguments.

Constraints seem not to be taken in account !?

Is there some other means of handling value and reference types
in different methods automatically ? I can't see one with generics
in C#. But I might have overlooked something.

Pierre

http://www.creativedocs.net/
Feb 17 '06 #1
10 2151
Hi,
Is the handling of those two cases so different that you need two methods?
You can use Type.IsClass to see if it's a class or not. you can have an if
statement and do what you need in any case.
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


"Pierre Arnaud" <op**@newsgroup.nospam> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
I'd like to provide two generic methods with the same name and
the same arguments, but with different constraints, such as:

void Explore<T>(T[] a, T[] b) where T : struct { }
void Explore<T>(T[] a, T[] b) where T : class { }

The value types should use the first method and the reference types
the second method. However, this won't compile, as the C# compiler
reports that there is already a method defined with the same name
and the same arguments.

Constraints seem not to be taken in account !?

Is there some other means of handling value and reference types
in different methods automatically ? I can't see one with generics
in C#. But I might have overlooked something.

Pierre

http://www.creativedocs.net/

Feb 17 '06 #2
Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi,
Is the handling of those two cases so different that you need two
methods? You can use Type.IsClass to see if it's a class or not. you
can have an if statement and do what you need in any case.


It's just an optimization issue:

- If it is a class type, then I have to do some checking to see if the
parameters are null; and I can compare for reference equality and need
to be prudent when calling object.Equals(object)

- If it is a struct type, I don't need to check for null and Equals can
be used without afterthought (or even System.IEquatable<T>.Equals)

You see the idea. If I want to do some null checking in my method, I
can't use a generic type for which T is not specified to be of class
type. And if I do, I can't pass value types to my method...

Pierre
Feb 18 '06 #3
Hi Pierre,

Ignacio has provided us with a good way of idetifying if a type is a class.
In your method, you can first use IsClass to check if T is a reference type
like the following:

if(typeof(T).IsClass)
.....//for reference types
else
.....//for value types

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Feb 20 '06 #4
Hi,
It's just an optimization issue:

- If it is a class type, then I have to do some checking to see if the
parameters are null; and I can compare for reference equality and need
to be prudent when calling object.Equals(object)

- If it is a struct type, I don't need to check for null and Equals can
be used without afterthought (or even System.IEquatable<T>.Equals)


Then Type.IsClass or Type.IsValueType is your answer

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Feb 20 '06 #5
Kevin Yu [MSFT] wrote:
Hi Pierre,

Ignacio has provided us with a good way of idetifying if a type is a
class. In your method, you can first use IsClass to check if T is a
reference type like the following:

if(typeof(T).IsClass)
....//for reference types
else
....//for value types
No, sorry, this won't work for me, as I want this test to be done at
compile time, not at run time. Here is a sample of what I want to be
able to do:

class Foo
{
public static bool Test<T>(T[] array) where T : struct
{
return (array[0] == array[1]);
}

public static bool Test<T>(T[] array) where T : class
{
return (array[0] == array[1])
&& (array[0] != null)
&& (array[1] != null);
}
}

I can't do the "== null" test if T is a value type, as it won't
compile. So how do I implement something which does this? I had
expected that the "where T : struct" and "where T : class"
constraints would be sufficient for the compiler to tell both
Test<T> methods apart; but it is not.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Feb 21 '06 #6
Hi,

"Pierre Arnaud" <op**@newsgroup.nospam> wrote in message
news:uH**************@tk2msftngp13.phx.gbl...

I can't do the "== null" test if T is a value type, as it won't
compile. So how do I implement something which does this?


As I said before, you use either IsClass or IsValueType to decide which is.
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Feb 21 '06 #7
Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi,

"Pierre Arnaud" <op**@newsgroup.nospam> wrote in message
news:uH**************@tk2msftngp13.phx.gbl...

I can't do the "== null" test if T is a value type, as it won't
compile. So how do I implement something which does this?


As I said before, you use either IsClass or IsValueType to decide
which is.


Sorry, no. I can't write :

class Foo
{
public static bool Test<T>(T[] array)
{
if (typeof (T).IsValueType)
{
return (array[0].Equals (array[1]));
}
if (typeof (T).IsClass)
{
return (array[0] == array[1]) // <--- here
&& (array[0] != null)
&& (array[1] != null);
}
throw new System.InvalidOperationException ();
}
}

as it won't compile. The C# compiler does not know "here"
that both array[0] and array[1] refer to reference types,
so it can't resolve the "==" ...
Feb 23 '06 #8
Hi,

Can you try to override the Object.Equal method and use return
(Object.Equal(array[0] == array[1]))?

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Feb 27 '06 #9
Kevin Yu [MSFT] wrote:
Hi,

Can you try to override the Object.Equal method and use return
(Object.Equal(array[0] == array[1]))?


Oh, yes. Object.Equal(array[0], array[1]) would do the trick.
Thanks for this (obvious -- but I hadn't seen it) solution.
Mar 4 '06 #10
You're welcome!

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Mar 6 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by andrew queisser | last post: by
10 posts views Thread by Ruediger Klaehn | last post: by
3 posts views Thread by Pierre Y. | last post: by
17 posts views Thread by atgraham | last post: by
9 posts views Thread by sloan | last post: by
6 posts views Thread by =?Utf-8?B?UXVhbiBOZ3V5ZW4=?= | last post: by
5 posts views Thread by teel | last post: by
17 posts views Thread by My interest | last post: by

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.