Check before calling a method on a potentially null object.

  • Thread starter Thread starter Glenn
  • Start date Start date
G

Glenn

Rather than...

if ( myInstance != null )
{
myInstance.Dispose();
}

maybe something like this...

myInstance?Dispose();

where ? = potentially null operator (question mark is the first symbol that
came to mind)

Just wondering if there's anything like this, going to be anything like
this, or am I being ridiculous.

BTW Please don't post comments about using the IDisposable pattern, this is
just an example.

Glenn
 
Glenn,

No, there is no operator that does this.
 
Glenn said:
Rather than...

if ( myInstance != null )
{
myInstance.Dispose();
}

maybe something like this...

myInstance?Dispose();

where ? = potentially null operator (question mark is the first symbol
that came to mind)

One way to avoid null checking is to not create objects that are null in the
first place. You can look into the Null Object pattern to see if it would
fit your situation.

PS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top