473,387 Members | 1,517 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, 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 2252
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: andrew queisser | last post by:
I've read some material on the upcoming Generics for C#. I've seen two types of syntax used for constraints: - direct specification of the interface in the angle brackets - where clauses I...
10
by: Ruediger Klaehn | last post by:
Sorry about the harsh language, but I have to vent my anger to somebody who actually understands what I am talking about. Complaining to my girlfriend is not going to produce any meaningful results...
3
by: Pierre Y. | last post by:
Hi, I've just read that article : http://msdn.microsoft.com/library/en-us/dnvs05/html/csharp_generics.asp I'm asking something. Why are generics constraints not been implemented using...
17
by: atgraham | last post by:
Here is the "lead C# architect" attempting to impugn C++ templates (bottom of the page). http://www.artima.com/intv/generics2.html (bottom of the page) (Full article begins at...
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...
9
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
6
by: =?Utf-8?B?UXVhbiBOZ3V5ZW4=?= | last post by:
I am trying to create a generics class with multiple constrains, as follows: public class KeyHandler<Twhere T : TextBoxBase, ComboBox When I try that, the compiler would complain: The class...
5
by: teel | last post by:
Hi there, I'm trying to apply "less than" and "more than" operators on the Generics (class template-like) type objects. Below is the code of my class representing a parameter that can be any type,...
17
by: My interest | last post by:
How can I use * operator in C# generics? e.g. class foo<T_> { T_ v1, v2: public T_ squar() { return v1 * v2; /// wrong! }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.