Solved

Which is the best implement?!?

Posted on 2006-11-08
5
230 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 30

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

705 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