• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

Which is the best implement?!?

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
laubrass
Asked:
laubrass
2 Solutions
 
SinclairCommented:
At first glance, the two calls appear identical to me, because Features.instance is static.
0
 
anarki_jimbelCommented:
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
 
laubrassAuthor Commented:
ExportContoller is a copy and paste error while posting.
It should ha been
instance = new Features();
0
 
mjmarlowCommented:
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
 
troywillmotCommented:
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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now