简体   繁体   中英

Accessing directly a method in a class from its object

if a class has only one method which is not called may times but rarely, then instead of calling the method in a traditional way as below

RarelyCalledClass orarelyCalled = new RarelyCalledClass();
orarelyCalled.rarelyCalledMethod();

can i call this as below.

(new RarelyCalledClass()).rarelyCalledMethod();

Will this increase the performance as compiler has to do less operation.

It will be exactly the same performance and code. Just that you can't access the instance anymore in your code.. And the readability is also worse (in my and most peoples opinion).

Also something you always should keep in mind: Premature micro optimization is evil.

Profile your application. Is this an actual bottleneck? No? Then don't bother.

Will this increase the performance as compiler has to do less operation?

No. I don't think so.

I believe you can check their IL code with any decompiler, you will see the same things as well.

First ones IL code;

  .locals init ([0] class _1.RarelyCalledClass orarelyCalled)
  IL_0000:  nop
  IL_0001:  newobj     instance void _1.RarelyCalledClass::.ctor()
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  callvirt   instance void _1.RarelyCalledClass::rarelyCalledMethod()
  IL_000d:  nop
  IL_000e:  ret

Seconds one IL code;

  .maxstack  8
  IL_0000:  nop
  IL_0001:  newobj     instance void _1.RarelyCalledClass::.ctor()
  IL_0006:  call       instance void _1.RarelyCalledClass::rarelyCalledMethod()
  IL_000b:  nop
  IL_000c:  ret

Based on this structure;

static void Main(string[] args)
{
    //
}
}

class RarelyCalledClass
{
    public RarelyCalledClass()
    {

    }

public void rarelyCalledMethod()
{
    Console.WriteLine("Test");
}
}

Only difference looks like your first code uses stloc nad ldloc for stack issues, second one doesn't.

It should be the same after compilation unless you use the instance somewhere later. However ILSpy shows a difference:

First version (with assignment)

.method private hidebysig static 
    void Main (
        string[] args
    ) cil managed 
{
    // Method begins at RVA 0x2058
    // Code size 13 (0xd)
    .maxstack 1
    .entrypoint
    .locals init (
        [0] class ConsoleApplication1.TestClass obj
    )

    IL_0000: newobj instance void ConsoleApplication1.TestClass::.ctor()
    IL_0005: stloc.0
    IL_0006: ldloc.0
    IL_0007: callvirt instance void ConsoleApplication1.TestClass::TestMethod()
    IL_000c: ret
} // end of method Program::Main

Second version (without assignment)

.method private hidebysig static 
    void Main (
        string[] args
    ) cil managed 
{
    // Method begins at RVA 0x2058
    // Code size 11 (0xb)
    .maxstack 8
    .entrypoint

    IL_0000: newobj instance void ConsoleApplication1.TestClass::.ctor()
    IL_0005: call instance void ConsoleApplication1.TestClass::TestMethod()
    IL_000a: ret
} // end of method Program::Main

Both were build in Release mode.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM