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

C#

 
AnswerRe: How to start a form without being activated? Pin
BillWoodruff21-Oct-17 16:54
professionalBillWoodruff21-Oct-17 16:54 
GeneralRe: How to start a form without being activated? Pin
alin122-Oct-17 2:03
alin122-Oct-17 2:03 
GeneralRe: How to start a form without being activated? Pin
Dave Kreskowiak22-Oct-17 2:18
mveDave Kreskowiak22-Oct-17 2:18 
GeneralRe: How to start a form without being activated? Pin
alin122-Oct-17 2:54
alin122-Oct-17 2:54 
GeneralRe: How to start a form without being activated? Pin
Dave Kreskowiak22-Oct-17 8:54
mveDave Kreskowiak22-Oct-17 8:54 
GeneralRe: How to start a form without being activated? Pin
alin127-Oct-17 16:41
alin127-Oct-17 16:41 
GeneralRe: How to start a form without being activated? Pin
Dave Kreskowiak28-Oct-17 4:00
mveDave Kreskowiak28-Oct-17 4:00 
QuestionSubjective question: which one of these Lazy Init is best Pin
Super Lloyd19-Oct-17 13:33
Super Lloyd19-Oct-17 13:33 
I have a problem writing lazy init in a (humanly lazy) fashion.

1st, I am not wondering about and using Lazy<T> for that discussion. This is not the topic at hand...

The topic here is which of the following 2 hand crafted lazy initialisation is the most... "satisfying" (a completely subjective topic, I should warn you)

method 1
C#
public object Property 
{
  get 
  {
    if (field == null)
      field = Create();
    return field;
  }
}
object field;
object Create() { return new object(); }

method 2
C#
public object Property 
{
  get 
  {
    field = field ?? Create();
    return field;
  }
}
object field;
object Create() { return new object(); }

the difference is that in method1 I use an if statement, which is more cumbersome to write, but in method2 I use the (C# 6) null coalesce operator, this is more succinct, but it looks like I might do pointless assignment.

[EDIT] a coworker came up with my favourite answer
C#
public object Property 
{
  get 
  {
    return field ?? (field = Create());
  }
}
object field;
object Create() { return new object(); }
A new .NET Serializer
All in one Menu-Ribbon Bar
Taking over the world since 1371!


modified 19-Oct-17 20:44pm.

AnswerRe: Subjective question: which one of these Lazy Init is best Pin
PIEBALDconsult19-Oct-17 14:26
mvePIEBALDconsult19-Oct-17 14:26 
AnswerRe: Subjective question: which one of these Lazy Init is best Pin
Bernhard Hiller19-Oct-17 21:37
Bernhard Hiller19-Oct-17 21:37 
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 
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 

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.