C# static, is this correct?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Maya

    #1

    C# static, is this correct?

    Hello guys, in C#, is using "static" would be the most proper way to
    get around calling methods located in different classes? for instance,
    a method caller in class A wouldn't see a method in class B unless that
    method is declared as public static.

    This works fine (i guess!) for me, and i have been doing this for a
    long time, just came to my mind that there might be a better or more
    professional way to call methods in other classes without sharing the
    method for the whole namespace scope, and just came to my mind: what if
    this practice slows down execution? as i do have intensive calculations
    where a method has to return values in few milliseconds and the overall
    performance is vital for my application.

    Your opinions are greatly appreciated, Thank you!

    Maya.

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: C# static, is this correct?

    Maya,

    See inline:
    [color=blue]
    > Hello guys, in C#, is using "static" would be the most proper way to
    > get around calling methods located in different classes? for instance,
    > a method caller in class A wouldn't see a method in class B unless that
    > method is declared as public static.[/color]

    Well, a method in class A could call methods on class B that are not
    static if it has an instance of class B to call them on. Static allows you
    to call methods on the class, not in instances of the class.
    [color=blue]
    > This works fine (i guess!) for me, and i have been doing this for a
    > long time, just came to my mind that there might be a better or more
    > professional way to call methods in other classes without sharing the
    > method for the whole namespace scope, and just came to my mind: what if
    > this practice slows down execution? as i do have intensive calculations
    > where a method has to return values in few milliseconds and the overall
    > performance is vital for my application.[/color]

    I don't see where a static method over an instance method would slow
    execution.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m
    [color=blue]
    >
    > Your opinions are greatly appreciated, Thank you!
    >
    > Maya.
    >[/color]


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: C# static, is this correct?

      Maya <kfoury@gmail.c om> wrote:

      <snip>

      See my response in the .general newsgroup, and read
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.


      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • Yunus Emre ALPÖZEN [MVP]

        #4
        Re: C# static, is this correct?

        What about singleton ? Take a look at this..

        http://msdn.microsoft.com/library/de...tondespatt.asp

        --
        HTH

        Thanks,
        Yunus Emre ALPÖZEN
        BSc, MCSD.NET
        Microsoft .NET & Security MVP

        "Maya" <kfoury@gmail.c om> wrote in message
        news:1138122936 .455286.168340@ f14g2000cwb.goo glegroups.com.. .[color=blue]
        > Hello guys, in C#, is using "static" would be the most proper way to
        > get around calling methods located in different classes? for instance,
        > a method caller in class A wouldn't see a method in class B unless that
        > method is declared as public static.
        >
        > This works fine (i guess!) for me, and i have been doing this for a
        > long time, just came to my mind that there might be a better or more
        > professional way to call methods in other classes without sharing the
        > method for the whole namespace scope, and just came to my mind: what if
        > this practice slows down execution? as i do have intensive calculations
        > where a method has to return values in few milliseconds and the overall
        > performance is vital for my application.
        >
        > Your opinions are greatly appreciated, Thank you!
        >
        > Maya.
        >[/color]


        Comment

        • Dave

          #5
          Re: C# static, is this correct?

          I would expect static to be faster than creating an instance of an
          object. But i don't think by much. I would base my decision on
          whether or not you need to create an object or just have access to a
          method.

          Comment

          • Vladimir Matveev

            #6
            Re: C# static, is this correct?

            Singleton has some important differences with simple class with static
            methods
            1. Singleton class can implenet interfaces
            2. Singleton can be passed to other methods as an argument
            3. It is easier to control initialization and lifecycle of singleton
            object.

            Comment

            • Jeff Louie

              #7
              Re: C# static, is this correct?

              Dave... Although you can think of instances as having their own methods
              and
              fields in separate memory, in reality the compiler is able to optimize
              this so
              that there is only a single copy of methods. Each call to a method gets
              a
              separate stack frame. Here is the MSIL for a static call to DoIt and a
              singleton
              call to DoIt():

              Static : DoIt : void()

              ..method private hidebysig static void DoIt() cil managed
              {
              // Code size 11 (0xb)
              .maxstack 1
              IL_0000: ldsfld string TestILStatic.Cl ass1::message
              IL_0005: call void [mscorlib]System.Console: :WriteLine(stri ng)
              IL_000a: ret
              } // end of method Class1::DoIt
              Here is the Singleton DoIt() IL:

              Singleton : DoIt : void()

              ..method public hidebysig instance void DoIt() cil managed
              {
              // Code size 12 (0xc)
              ..maxstack 1
              IL_0000: ldarg.0
              IL_0001: ldfld string TestILSingleton .Singleton::mes sage
              IL_0006: call void [mscorlib]System.Console: :WriteLine(stri ng)
              IL_000b: ret
              } // end of method Singleton::DoIt
              Note the added argument on the stack to this.

              Regards,
              Jeff[color=blue][color=green]
              >>I would expect static to be faster than creating an instance of an[/color][/color]
              object<<

              *** Sent via Developersdex http://www.developersdex.com ***

              Comment

              Working...