Click here to Skip to main content
15,890,512 members
Home / Discussions / C#
   

C#

 
GeneralRe: Subjective question: which one of these Lazy Init is best Pin
Super Lloyd19-Oct-17 21:46
Super Lloyd19-Oct-17 21:46 
GeneralRe: Subjective question: which one of these Lazy Init is best Pin
OriginalGriff19-Oct-17 21:52
mveOriginalGriff19-Oct-17 21:52 
GeneralRe: Subjective question: which one of these Lazy Init is best Pin
BillWoodruff20-Oct-17 20:21
professionalBillWoodruff20-Oct-17 20:21 
GeneralRe: Subjective question: which one of these Lazy Init is best Pin
Bernhard Hiller23-Oct-17 22:16
Bernhard Hiller23-Oct-17 22:16 
GeneralRe: Subjective question: which one of these Lazy Init is best Pin
Eddy Vluggen19-Oct-17 22:39
professionalEddy Vluggen19-Oct-17 22:39 
AnswerRe: Subjective question: which one of these Lazy Init is best Pin
Richard Deeming20-Oct-17 1:53
mveRichard Deeming20-Oct-17 1:53 
SuggestionRe: Subjective question: which one of these Lazy Init is best Pin
Eddy Vluggen20-Oct-17 2:04
professionalEddy Vluggen20-Oct-17 2:04 
GeneralRe: Subjective question: which one of these Lazy Init is best Pin
Richard Deeming20-Oct-17 2:12
mveRichard Deeming20-Oct-17 2:12 
Eddy Vluggen wrote:
The second third* won't, ever.

Yes it will. It's syntactical sugar; so long as the compiler supports it, the compiled code would run on .NET 1.0 if you wanted to.

IL for #1:
IL_0000:  ldarg.0     
IL_0001:  ldfld       Foo.field
IL_0006:  brtrue.s    IL_0014
IL_0008:  ldarg.0     
IL_0009:  ldarg.0     
IL_000A:  call        Foo.Create
IL_000F:  stfld       Foo.field
IL_0014:  ldarg.0     
IL_0015:  ldfld       Foo.field
IL_001A:  ret         
IL for #3:
IL_0000:  ldarg.0     
IL_0001:  ldfld       Foo.field
IL_0006:  dup         
IL_0007:  brtrue.s    IL_0019
IL_0009:  pop         
IL_000A:  ldarg.0     
IL_000B:  ldarg.0     
IL_000C:  call        Foo.Create
IL_0011:  dup         
IL_0012:  stloc.0     
IL_0013:  stfld       Foo.field
IL_0018:  ldloc.0     
IL_0019:  ret         

The only difference between the two is that #3 uses dup to avoid calling ldfld twice. It's effectively equivalent to:
C#
object temp = field;
if (temp == null)
{
    temp = Create();
    field = temp;
}

return temp;


(But yes, R# has an option to change the C# language level used by its suggestions, so you could prevent this suggestion if you really wanted to.)



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer



modified 20-Oct-17 8:26am.

GeneralRe: Subjective question: which one of these Lazy Init is best Pin
Eddy Vluggen20-Oct-17 4:31
professionalEddy Vluggen20-Oct-17 4:31 
GeneralRe: Subjective question: which one of these Lazy Init is best Pin
Richard Deeming20-Oct-17 4:43
mveRichard Deeming20-Oct-17 4:43 
GeneralRe: Subjective question: which one of these Lazy Init is best Pin
Eddy Vluggen20-Oct-17 6:01
professionalEddy Vluggen20-Oct-17 6:01 
AnswerRe: Subjective question: which one of these Lazy Init is best Pin
Super Lloyd20-Oct-17 20:31
Super Lloyd20-Oct-17 20:31 
GeneralRe: Subjective question: which one of these Lazy Init is best Pin
Super Lloyd20-Oct-17 20:32
Super Lloyd20-Oct-17 20:32 
AnswerRe: Subjective question: which one of these Lazy Init is best Pin
jschell20-Oct-17 9:22
jschell20-Oct-17 9:22 
GeneralRe: Subjective question: which one of these Lazy Init is best Pin
Eddy Vluggen20-Oct-17 14:18
professionalEddy Vluggen20-Oct-17 14:18 
GeneralRe: Subjective question: which one of these Lazy Init is best Pin
jschell23-Oct-17 6:02
jschell23-Oct-17 6:02 
GeneralRe: Subjective question: which one of these Lazy Init is best Pin
Eddy Vluggen23-Oct-17 6:25
professionalEddy Vluggen23-Oct-17 6:25 
GeneralRe: Subjective question: which one of these Lazy Init is best Pin
jschell25-Oct-17 9:07
jschell25-Oct-17 9:07 
AnswerRe: Subjective question: which one of these Lazy Init is best Pin
Super Lloyd20-Oct-17 20:40
Super Lloyd20-Oct-17 20:40 
GeneralRe: Subjective question: which one of these Lazy Init is best Pin
Bernhard Hiller23-Oct-17 22:28
Bernhard Hiller23-Oct-17 22:28 
GeneralRe: Subjective question: which one of these Lazy Init is best Pin
Super Lloyd24-Oct-17 1:04
Super Lloyd24-Oct-17 1:04 
GeneralRe: Subjective question: which one of these Lazy Init is best Pin
Richard Deeming24-Oct-17 1:53
mveRichard Deeming24-Oct-17 1:53 
AnswerRe: Subjective question: which one of these Lazy Init is best Pin
BillWoodruff20-Oct-17 18:10
professionalBillWoodruff20-Oct-17 18:10 
QuestionRe: Subjective question: which one of these Lazy Init is best Pin
Super Lloyd20-Oct-17 20:52
Super Lloyd20-Oct-17 20:52 
AnswerRe: Subjective question: which one of these Lazy Init is best Pin
BillWoodruff20-Oct-17 22:42
professionalBillWoodruff20-Oct-17 22:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.