From: tshad on
Given that a.Value is a string and dr is a DataRow

Just curious why this works:

var temp = Formats.Find(a => a.Value == (string)dr[0]);

And this doesn't

var temp = Formats.Find(a => a.Value == dr[0]);

The only difference is that I am not casting it.

If the 2nd one was a casting problem, I would expect an error - but I don't
get one. Temp is just = null.

Thanks,

Tom


From: kndg on
On 1/27/2010 10:00 AM, tshad wrote:
> Given that a.Value is a string and dr is a DataRow
>
> Just curious why this works:
>
> var temp = Formats.Find(a => a.Value == (string)dr[0]);
>
> And this doesn't
>
> var temp = Formats.Find(a => a.Value == dr[0]);
>
> The only difference is that I am not casting it.
>
> If the 2nd one was a casting problem, I would expect an error - but I don't
> get one. Temp is just = null.
>
> Thanks,
>
> Tom
>
>

I haven't look at the documentation, but my guess is
- dr[0] returning an object type, so you need a cast.
- assuming Formats is of type List<T>, then Formats.Find will return a
default(T) when the search failed. If 'a' is of reference type, will you
get a null.

Regards.
From: Arne Vajhøj on
On 26-01-2010 21:00, tshad wrote:
> Given that a.Value is a string and dr is a DataRow
>
> Just curious why this works:
>
> var temp = Formats.Find(a => a.Value == (string)dr[0]);
>
> And this doesn't
>
> var temp = Formats.Find(a => a.Value == dr[0]);
>
> The only difference is that I am not casting it.

The first does string==string, which checks if the strings
contains the same char sequence.

The second does object==object, which checks if it is the same
object.

Arne


From: tshad on

"kndg" <reply(a)this.newsgroup> wrote in message
news:%23Fu9uZvnKHA.4512(a)TK2MSFTNGP04.phx.gbl...
> On 1/27/2010 10:00 AM, tshad wrote:
>> Given that a.Value is a string and dr is a DataRow
>>
>> Just curious why this works:
>>
>> var temp = Formats.Find(a => a.Value == (string)dr[0]);
>>
>> And this doesn't
>>
>> var temp = Formats.Find(a => a.Value == dr[0]);
>>
>> The only difference is that I am not casting it.
>>
>> If the 2nd one was a casting problem, I would expect an error - but I
>> don't
>> get one. Temp is just = null.
>>
>> Thanks,
>>
>> Tom
>>
>>
>
> I haven't look at the documentation, but my guess is
> - dr[0] returning an object type, so you need a cast.
> - assuming Formats is of type List<T>, then Formats.Find will return a
> default(T) when the search failed. If 'a' is of reference type, will you
> get a null.

I understand the casting issue.

But normally, you get an error if you don't cast it. In this case, it went
through normally.

Thanks,

Tom
>
> Regards.


From: Peter Duniho on
tshad wrote:
> [...]
> But normally, you get an error if you don't cast it. In this case, it went
> through normally.

You also don't get an error if you do this:

void MethodA(object obj) { }

void MethodB()
{
string str = "Foo";

MethodA(str);
}

Same thing is going on in your code example. There's no need to cast if
there's an applicable method for the type(s) given.

More generally, upcasting (to the less-derived types) does not actually
require an explicit cast. The implicit cast always exists. You only
need an explicit cast when downcasting, to more-derived types.

Pete