lamda not finding string

T

tshad

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
 
K

kndg

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.
 
A

Arne Vajhøj

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
 
T

tshad

kndg said:
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
 
P

Peter Duniho

tshad said:
[...]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top