Skip to main content
added link
Source Link
hannson
  • 4.5k
  • 8
  • 43
  • 46

My favorite trick is using the null coalesce operatornull coalesce operator and parentheses to automagically instantiate collections for me.

private IList<Foo> _foo;

public IList<Foo> ListOfFoo 
    { get { return _foo ?? (_foo = new List<Foo>()); } }

My favorite trick is using the null coalesce operator and parentheses to automagically instantiate collections for me.

private IList<Foo> _foo;

public IList<Foo> ListOfFoo 
    { get { return _foo ?? (_foo = new List<Foo>()); } }

My favorite trick is using the null coalesce operator and parentheses to automagically instantiate collections for me.

private IList<Foo> _foo;

public IList<Foo> ListOfFoo 
    { get { return _foo ?? (_foo = new List<Foo>()); } }
Minor edit: grammar/spelling/case/punctation/etc.
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

My favorite trick is using the null coalesce operator and parensparentheses to automagically instantiate collections for me.

private IList<Foo> _foo;

public IList<Foo> ListOfFoo 
    { get { return _foo ?? (_foo = new List<Foo>()); } }

My favorite trick is using the null coalesce operator and parens to automagically instantiate collections for me.

private IList<Foo> _foo;

public IList<Foo> ListOfFoo 
    { get { return _foo ?? (_foo = new List<Foo>()); } }

My favorite trick is using the null coalesce operator and parentheses to automagically instantiate collections for me.

private IList<Foo> _foo;

public IList<Foo> ListOfFoo 
    { get { return _foo ?? (_foo = new List<Foo>()); } }
Rollback to Revision 3
Source Link
user1228
user1228

My favorite trick is using the null coalesce operator and parens to automagically instantiate collections for me.

private IList<Foo> _foo;

public IList<Foo> ListOfFoo 
    { get { return _foo ?? (_foo = new List<Foo>()); } }

Please do not mistake this for the following

public IList<Foo> ListOfFoo 
    { get { return _foo ?? new List<Foo>(); } }

My entry does the following:

  1. Checks _foo for null
  2. If null, assigns a new List<Foo> to _foo
  3. Returns _foo

The second example does this:

  1. Checks _foo for null
  2. Returns a new List<Foo> if _foo is null

In other words, in my implementation

Assert.AreSame(instance.Foo, instance.Foo)

returns true. In the second implementation, it returns FALSE.

Doing it the second way will result in the following bug:

instance.Foo.Clear();
instance.Foo.Add(new Foo());
var kaboom = instance.Foo[0] // ArgumentOutOfRangeException

My favorite trick is using the null coalesce operator and parens to automagically instantiate collections for me.

private IList<Foo> _foo;

public IList<Foo> ListOfFoo 
    { get { return _foo ?? (_foo = new List<Foo>()); } }

Please do not mistake this for the following

public IList<Foo> ListOfFoo 
    { get { return _foo ?? new List<Foo>(); } }

My entry does the following:

  1. Checks _foo for null
  2. If null, assigns a new List<Foo> to _foo
  3. Returns _foo

The second example does this:

  1. Checks _foo for null
  2. Returns a new List<Foo> if _foo is null

In other words, in my implementation

Assert.AreSame(instance.Foo, instance.Foo)

returns true. In the second implementation, it returns FALSE.

Doing it the second way will result in the following bug:

instance.Foo.Clear();
instance.Foo.Add(new Foo());
var kaboom = instance.Foo[0] // ArgumentOutOfRangeException

My favorite trick is using the null coalesce operator and parens to automagically instantiate collections for me.

private IList<Foo> _foo;

public IList<Foo> ListOfFoo 
    { get { return _foo ?? (_foo = new List<Foo>()); } }
clearing up
Source Link
user1228
user1228
Loading
added 5 characters in body
Source Link
user1228
user1228
Loading
deleted 52 characters in body
Source Link
user1228
user1228
Loading
Post Made Community Wiki by CommunityBot
Source Link
user1228
user1228
Loading