简体   繁体   中英

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 "" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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