0

I'm pulling all of the advanced features together for this one, but haven't worked with generics or lambda expressions very much:

Here's example usage of the method I want to create:

MyClass mc = null;
int x = mc.TryGetOrDefault(z => z.This.That.TheOther); // z is a reference to mc
// the code has not failed at this point and the value of x is 0 (int's default)
// had mc and all of the properties expressed in the lambda expression been initialized
// x would be equal to mc.This.That.TheOther's value

Here's as far as I've gotten, but Visual Studio is complaining:

enter image description here

1 Answer 1

5

You haven't made your method generic in TResult. You want something like:

public static TResult TryGetOrDefault<TSource, TResult>
    (this TSource obj, Expression<Func<TSource, TResult>> expression)
Sign up to request clarification or add additional context in comments.

4 Comments

You've just made this into an entirely new question Ronnie. I think Jon has answered the first one perfectly.
I thought the scope of the question centered around my end result and since StackOverflow touts itself as being wiki like, I could update the question. Wait for URL to my next ?
@Ronnie: You can edit an existing question - but you've asked a new question, which isn't the same thing at all.
Got it. Thanks for the help. I asked another question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.