1
var d = get(x).Title;
var e = get(y).Title;

The getting of Title gives an exception if get(y) returns null. However what I need is for e to be null if get(y) is null and there to be no exception generated. I can do this with two steps of get(y) but I want to only do the get(y) one time.

Is there some easy one line way that I can with just one get(y) and with one line of code set e to null if get(y) is null or the actual value if get(y) does not return a null?

Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427
  • what's more important? only calling get(y) once or not assigning an intermediate variable? – Adam Dec 21 '11 at 16:50

4 Answers4

2
var z = get(y);
var e = z != null ? z.Title : null;

You can also use extension methods like those:

    public static TResult IfNotNull<T, TResult>(this T obj, Func<T, TResult> selector)
        where T : class
    {
        return obj.IfNotNull(selector, default(TResult));
    }

    public static TResult IfNotNull<T, TResult>(this T obj, Func<T, TResult> selector, TResult valueIfNull)
        where T : class
    {
        if (obj == null)
            return valueIfNull;
        return selector(obj);
    }

...

    var e = get(y).IfNotNull(_ => _.Title);
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
1
var d = get(x) != null ? get(x).Title : null;

Since you state that only one call of get(x) get(y) is allowed you could write this as follows

  var X = get(x);
  var d = X != null ? X.Title : null;

That is the shortest way to write the code but I think a more clear way to write all of this would be to handle this with some form of an extension method of a function call since this is something that you are having to do to multiple variables

Example Extension Methods

public static TypeOfTitle GetTitle(this TypeOfget x)
{
   return x != null ? x.Title : null;
}

Then call this as follows

var d = get(x).GetTitle();
Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
msarchet
  • 15,104
  • 2
  • 43
  • 66
1

You can use an inline conditional statement (or, ternary operator), like so:

var e = get(y) != null ? get(y).Title : null;

However, you might want to take the 'primitive' route (I know, I know, one-liners are all the rage), if for nothing else but to minimise the calls to get (which you do state as a concern), which would simply be:

string e = null;
var somethingOrOther = get(y);
if (somethingOrOther != null) {
    e = somethingOrOther;
}

To my knowledge, I'm afraid it's one or the other - someone else might know something I don't, or Jon Skeet might concoct some evil for you, but this is where I stand (anything else would seem only to be variations of the same approaches).

Furthermore, since now being aware of a possible duplicate, but for completeness, I'll quote Eric Lippert's on "null if object is null, or object.member if object is not null":

There's no short form for that; implementing one is a fairly frequently requested feature.

Community
  • 1
  • 1
Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
0

You'll have to use a terinary operator:

var e = (get(y) == null ? null : get(y).Title).

If get(y) is an expensive operation, you'll want to cache it in another variable.

Bryan Boettcher
  • 4,412
  • 1
  • 28
  • 49
  • Still hoping for a solution with no other variable. The ?? below looked good but another poster said it would not work :-( – Samantha J T Star Dec 21 '11 at 16:47
  • 1
    @SamanthaJ: You can't do it without another variable unless you call get(y) twice. Even the null coalescing operator (written improperly below) requires two calls to get(y). – Bryan Boettcher Dec 21 '11 at 16:48
  • @SamanthaJ, there have been calls for a `?.` operator, which would theoretically allow you to say `get(x)?.Title` without a null check, but there have been no announcements about whether to include that in a future version of C#, and it is obviously not there now. – Anthony Pegram Dec 21 '11 at 16:48
  • 1
    @SamanthaJ: unless this is code golf, use another variable. – Bryan Boettcher Dec 21 '11 at 16:49