Solved

Which is the best implement?!?

Posted on 2006-11-08
5
227 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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