To make "Assign if variable is not null, otherwise skip" be shorter #49427
-
at present
the current shortest is
but it still assign one time when variable is null.Test:
consider to make it be shorter and faster? |
Beta Was this translation helpful? Give feedback.
Answered by
PathogenDavid
Nov 17, 2020
Replies: 1 comment 1 reply
-
As @canton7 noted on your csharplang question, IL doesn't really matter. What matters is what the JIT emits. If you compare the JIT assembly for your two code snippets, the generated code is essentially the same: ; Using ??
C.M1(System.Object, System.Object)
L0000: test rdx, rdx
L0003: jne short L0008
L0005: mov rdx, r8
L0008: mov rax, rdx
L000b: ret
; Using if
C.M2(System.Object, System.Object)
L0000: test rdx, rdx
L0003: je short L0008
L0005: mov r8, rdx
L0008: mov rax, r8
L000b: ret |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
Flithor
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As @canton7 noted on your csharplang question, IL doesn't really matter. What matters is what the JIT emits. If you compare the JIT assembly for your two code snippets, the generated code is essentially the same: