Solved

Which is the best implement?!?

Posted on 2006-11-08
5
215 Views
Last Modified: 2010-05-18
Hi Experts,

we use Singletons in some classes of our application.

here an example...
-----------------------------------------------------------
class name: Features
-----------------------------------------------------------
private string _Something;
public string Something
{
Get { return(_Something); }
Set { _Something = value; }
}

private static Features instance = null;

public static Features GetInstance()
            {
                  if(instance == null)
                  {
                        instance = new ExportContoller();
                  }

                  return instance;
            }

private Features()
{
}

(...)
-----------------------------------------------------------

and i'm asking myself which call method do i have to use... i mean which is the best one!?!?!

Method 1:
Features keyID = Features.getInstance();
keyID.Something = "AGAH876876";

or

Method 2:
Features.getInstance().Something = "AGAH876876";

Does they have different Lifetime?!?

thks!

laubrass
0
Comment
Question by:laubrass
5 Comments
 
LVL 2

Expert Comment

by:Sinclair
ID: 17902493
At first glance, the two calls appear identical to me, because Features.instance is static.
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 17902648
I believe both calls are identical and are compiled into same code.

The only iisue I can see from the above code is:

instance = new ExportContoller();

'instance' is of type 'Features'. Why's that?
0
 

Author Comment

by:laubrass
ID: 17902877
ExportContoller is a copy and paste error while posting.
It should ha been
instance = new Features();
0
 
LVL 7

Accepted Solution

by:
mjmarlow earned 350 total points
ID: 17903868
Method 2 seems most optimal.  I used ILDASM to inspect the RELEASE version of the code (see below) and comparing the emitted IL, Method 1 has a couple of additional instructions.  

# Method 1:
Features keyID = Features.GetInstance();
            keyID.Something = "AGAH876876";
.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       18 (0x12)
  .maxstack  2
  .locals init ([0] class AppSingleCall.Features keyID)
  IL_0000:  call       class AppSingleCall.Features AppSingleCall.Features::GetInstance()
  IL_0005:  stloc.0
  IL_0006:  ldloc.0
  IL_0007:  ldstr      "AGAH876876"
  IL_000c:  callvirt   instance void AppSingleCall.Features::set_Something(string)
  IL_0011:  ret
} // end of method Program::Main


Method 2:
Features.GetInstance().Something = "AGAH876876";
.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       16 (0x10)
  .maxstack  8
  IL_0000:  call       class AppSingleCall.Features AppSingleCall.Features::GetInstance()
  IL_0005:  ldstr      "AGAH876876"
  IL_000a:  callvirt   instance void AppSingleCall.Features::set_Something(string)
  IL_000f:  ret
} // end of method Program::Main

0
 
LVL 3

Assisted Solution

by:troywillmot
troywillmot earned 150 total points
ID: 17911436
Method 2 is probably best if you're only setting the one property (Something).
If you're making multiple method calls/property reads/sets then you'd probably get a marginal improvement from method 1 because you only call into GetInstance once.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now