Hello Adam,
>> EqualityComparer<T>.Default.Equals(item, default(T))
>
> Isn't this the same as writing
> item == default(T)
No. Because your line of code will fail with "Error 1 Operator '==' cannot
be applied to operands of type 'Type1' and 'Type1'"
> And even if not I don't get it. This will not resolve the problem of
> structs. If T is struct type then it cannot be null and always has a
> value. So it always can be used in foreach.
Yes, you are right with that.
What do you think of this?
class Test<Type1, Type2> where Type1 : IEnumerable<Type2>
{
public void Fun(Type1 item)
{
IEnumerable<Type2> enumerable = item;
if (enumerable == null) throw new
ArgumentNullException("item");
// a check is here needed
foreach (Type2 e in enumerable)
{
/*...*/
}
}
}
If it's a value type, it will be boxed. No harm done. If Type1 is a class,
it will be checked for null.
Kind regards,
Henning Krause
Adam Badura - 23 Jul 2007 19:25 GMT
> No. Because your line of code will fail with "Error 1 Operator '==' cannot
> be applied to operands of type 'Type1' and 'Type1'"
Yes. You are right.
> What do you think of this?
>
[quoted text clipped - 16 lines]
> If it's a value type, it will be boxed. No harm done. If Type1 is a class,
> it will be checked for null.
That is what I needed. Prety simple. Shame I didn't thought on that
first before asking. :)
By the way. I am new to this group. What do does [MV.... in author mean?
Adam Badura
Rory Becker - 23 Jul 2007 19:33 GMT
> By the way. I am new to this group. What do does [MV.... in author
> mean?
MVP = "Most Valuable Professional" = Microsoft awarded title - Typically
given for community work in a given area.
--
Ror