4

In c# there is a great operator '??' that makes life much simplier. Instead of writing (foo != null)? foo : default I can write foo ?? default. Is there a simple way to apply this operator to class memeber e.g. in situation (foo != null)? foo.bar : default?

upd: OK I'll expalin a bit:

string a = (someVeryLongVariableName != null)? someVeryLongVariableName : ""; // Long way
string a = someVeryLongVariableName ?? ""; // Shorter with same result
string a = (someVeryLongVariableName != null)? someVeryLongVariableName.bar : ""; // What's the shorter way in this case?

upd2: Situation when I need this feature looks like this:

if (foo is MyClass) bar1 = (foo as MyClass).SpecificMember;
if (foo is OneMoreClass) bar2 = (foo as OneMoreClass).AnotherSpecificMember;
if (foo is MyFavoriteClass) bar3 = (foo as MyFavoriteClass).YetAnotherMember;

It'll be cool if I can shorten these to something like

bar1 = (foo as MyClass).SpecificMember ?? null;

Maybe it's not much shorter but it casts foo to MyClass only once and explicitly initializes bar with default value. And most important it looks nicer.

Poma
  • 8,174
  • 18
  • 82
  • 144
  • what's not simple writing (foo != null) ? foo.bar : default ?? – Mitch Wheat Dec 05 '10 at 02:33
  • What? I'm not sure you can get much simpler...am I missing something? – IAbstract Dec 05 '10 at 02:35
  • in the upd2 you are assigning 3 different variables (bar1, bar2 and bar3). I cannot see a way how shortening that to bar1 assignment alone will help you. Or did you intend to assign only bar1 and by mistake gave a wrong example? –  Dec 05 '10 at 04:51
  • possible duplicate of [Deep Null checking, is there a better way?](http://stackoverflow.com/questions/2080647/deep-null-checking-is-there-a-better-way) – Ben Voigt Dec 05 '10 at 06:23

3 Answers3

3

Only if you have some way of doing it like this

  (foo ?? defaultFoo).bar

But that would mean that you'd have to have an object of the same time as foo around with its bar set up the way you want it.

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
  • and what if `foo` is an expression like `(foo as string).Length`? – Poma Dec 05 '10 at 02:44
  • @Poma, what do you mean foo is an expression? In this case an expression is exactly what you are using foo as. However, your case would not work because .Length is an int which cannot be null. – tster Dec 05 '10 at 02:48
3

No, there's currently no such operator (access member or return default value) in C#. You can, of course, write a method to do this:

public static TMember MemberOrDefault<T, TMember>(T obj, 
                                                  Func<T, TMember> accessor, 
                                                  TMember @default) {
    return obj == null ? @default : accessor(obj);
}

string a = MemberOrDefault(someVeryLongVariableName, v => v.bar, "");

Note, however, that this will still return null if someVeryLongVariableName.bar is null (which might or might not be what you want).

Heinzi
  • 167,459
  • 57
  • 363
  • 519
3

There is no built-in C# operator that does what you describe. A new operator (most people suggest .? or ?.) has been requested, but it has not been added to C#.

As a workaround, many developers implement an IfNotNull extension method, e.g., in this StackOverflow question and answer.

Community
  • 1
  • 1
Bradley Grainger
  • 27,458
  • 4
  • 91
  • 108