简体   繁体   English

有可能使用?? 班级成员的操作员?

[英]is it possible to use ?? operator on class member?

In c# there is a great operator '??' 在c#中有一个很棒的运算符'??' that makes life much simplier. 这让生活更加简单。 Instead of writing (foo != null)? foo : default 而不是写(foo != null)? foo : default (foo != null)? foo : default I can write foo ?? default (foo != null)? foo : default我可以写foo ?? default foo ?? default . foo ?? default Is there a simple way to apply this operator to class memeber eg in situation (foo != null)? foo.bar : default 是否有一种简单的方法将此运算符应用于类memeber,例如在情境中(foo != null)? foo.bar : default (foo != null)? foo.bar : default ? (foo != null)? foo.bar : default

upd: OK I'll expalin a bit: upd:好的,我会稍微expalin:

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: upd2:我需要此功能时的情况如下所示:

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. 也许它不会短得多,但只会将foo转换为MyClass一次,并使用默认值显式初始化bar。 And most important it looks nicer. 最重要的是它看起来更好。

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. 但这意味着你必须拥有与foo相同的对象,其条形设置方式与你想要的一样。

No, there's currently no such operator (access member or return default value) in C#. 不,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). 但请注意,如果someVeryLongVariableName.bar为null(这可能是您想要的,也可能不是您想要的),它仍会返回null。

There is no built-in C# operator that does what you describe. 没有内置的C#运算符可以执行您描述的操作。 A new operator (most people suggest .? or ?. ) has been requested , but it has not been added to C#. 已经请求了一个新的运算符(大多数人建议.??. ),但它还没有被添加到C#中。

As a workaround, many developers implement an IfNotNull extension method, eg, in this StackOverflow question and answer . 作为一种解决方法,许多开发人员实现了IfNotNull扩展方法,例如,在此StackOverflow问题答案中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM