The Bolla Blog

Sponsors

The Lounge

Wicked Cool Jobs

Syndication

News

  • Settling into a new job/life in the Pittsburgh area.
Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
C# 4.0 Feature Request: "??=" the Lazy load operator...

A cool C# 2.0 feature is the "??" coalesce operator, which lets you do this:

public ISomeService Service
{
get { return service = service ?? LazyLoadSomeService(); }
}

For the noobs, this code is equivalent to this:

public ISomeService Service
{
get { return service = service == null ? LazyLoadSomeService() : service; }
}

Which is the same as:

public ISomeService Service
{
get
{
if (service == null)
return service = LazyLoadSomeService();
else
return service;
}
}

Now it would be great if we could trim this up just a little bit more...

public ISomeService Service
{
get { return service ??= LazyLoadSomeService(); }
}

This is similar to +=, *=, and family. This seems like the logical progression to me. I guess this would be called "coalescing assignment operator" or something like that. And we could take this one step even further... C# 3.0 has Automatic properties that alleviates the need for an explicitly declared backing field. What happens when my suggested "coalescing assignment operator" and automatic properties do the nasty...

public ISomeService Service
{
get?? { LazyLoadSomeService(); }
}

The syntax is debatable but I think the concept is nice and useful.


Posted 10-26-2007 7:06 PM by Jim Bolla

[Advertisement]

Comments

Damien Guard wrote re: C# 4.0 Feature Request: "??=" the Lazy load operator...
on 10-28-2007 5:52 AM

Except there should be some locking round there to ensure concurrent usage, e.g:

public ISomeService Service

{

   get

   {

       if (service == null) {

          lock(this)

          if (service == null)

             service = LazyLoadSomeService();

       }

       return service;

   }

}

Michal Grzegorzewski wrote re: C# 4.0 Feature Request: "??=" the Lazy load operator...
on 10-28-2007 7:40 AM

Jim, this is nice proposal :)

so, maybe other wishes? I'd like to have regexp syntax inside c#, just like in other languages, ie ruby. So, what do you think about:

"cat" =~ "cat|dog"

which could be equivalent to

Regex.IsMatch("cat", "cat|dog"")

C# 4.0 Feature Request: "??=" the Lazy load operator… - The Bolla Blog « Noocyte’s Weblog wrote C# 4.0 Feature Request: "??=" the Lazy load operator… - The Bolla Blog « Noocyte’s Weblog
on 10-29-2007 9:02 AM

Pingback from  C# 4.0 Feature Request: "??=" the Lazy load operator… - The Bolla Blog « Noocyte’s Weblog

Eugene wrote re: C# 4.0 Feature Request: "??=" the Lazy load operator...
on 11-28-2007 4:46 AM

I think the modifyer "const" for class members, class functions, etc, function parameters and generally revisioned "const" approach.

Also modifyer like readonly but bore strong than readonly, cose we could modify all members of readonly fields.

Casey Barton wrote re: C# 4.0 Feature Request: "??=" the Lazy load operator...
on 12-05-2007 2:09 PM

I just thought of exactly this, and Googled for "coalescing assignment operator", wondering if anyone else had. Count me in. :)

For automatic properties , how about a new keyword instead:

public ISomeService Service

{

   init { LazyLoadSomeService(); }

   get;

   set;

}

Pop Catalin wrote re: C# 4.0 Feature Request: "??=" the Lazy load operator...
on 03-11-2008 10:12 PM

This is how it should be done

public ISomeService Service

{

  get { return  service ?? (service =LazyLoadSomeService()); }

}

else you assign the backing field to itself every time you call get.

AK wrote re: C# 4.0 Feature Request: "??=" the Lazy load operator...
on 04-11-2008 11:40 PM

Dude we are already so close with C# , dont add FLUFF.

Add STUFF

return _MYSERVICECLASS ?? (_MYSERVICECLASS = new MyService());

   public class MyService

   {

       public int Count()

       {

           return 10;

       }

   }

   public partial class Form1 : Form

   {

       MyService _MYSERVICECLASS;

       public Form1()

       {

           InitializeComponent();

       }

       private int GetCount()

       {

           return MYSERVICECLASS.Count();

       }

       private MyService MYSERVICECLASS

       {

           get

           {

               return _MYSERVICECLASS ?? (_MYSERVICECLASS = new MyService());

           }

       }

       private void Form1_Load(object sender, EventArgs e)

       {

           MessageBox.Show(string.Format ("{0} count ,  service is null {1}",GetCount(),_MYSERVICECLASS==null  ));

       }

   }

Alex Krakovetskiy's blog wrote Що ми чекаємо від C# 4.0?
on 10-24-2008 4:47 PM

Спробую підсумувати все, що мені вдалось знайти на тему "що ми чекаємо від C# 4.0". Отже, Feature

boj wrote re: C# 4.0 Feature Request: "??=" the Lazy load operator...
on 11-10-2008 3:38 PM

nice:)

on 11-18-2008 6:07 AM

Спробую підсумувати все, що мені вдалось знайти на тему "що ми чекаємо від C# 4.0". Отже, Feature

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)

CodeBetter.Com