Solved

Which is the best implement?!?

Posted on 2006-11-08
5
218 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

947 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

21 Experts available now in Live!

Get 1:1 Help Now