简体   繁体   English

c# 运营商'??' 并作为'

[英]c# operators '??' and 'as'

I saw this code but i don't know order of operators in second line.我看到了这段代码,但我不知道第二行中运算符的顺序。 What's result of second line?第二行的结果是什么?

Object obj = 1;

String s = obj as String?? "";

C# operator precedence table states that as (see "relational and type-testing" row) operator has higher precedence then null-coalescing operator ( ?? ), so your expression will result in empty string, cause obj as String will evaluate to null thus null-coalescing operator will return right-hand operand, ie "" . C# 运算符优先级表指出, as (参见“关系和类型测试”行)运算符的优先级高于空合并运算符( ?? ),因此您的表达式将导致空字符串,导致obj as String将评估为null因此空合并运算符将返回右手操作数,即""

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

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