471,354 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

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.

Jan 24 '06 #1
6 1219
Maya,

See inline:
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.
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.
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.
I don't see where a static method over an instance method would slow
execution.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

Your opinions are greatly appreciated, Thank you!

Maya.

Jan 24 '06 #2
Maya <kf****@gmail.com> wrote:

<snip>

See my response in the .general newsgroup, and read
http://www.pobox.com/~skeet/csharp/faq/posting.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 24 '06 #3
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" <kf****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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.

Jan 24 '06 #4
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.

Jan 24 '06 #5
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.

Jan 24 '06 #6
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.Class1::message
IL_0005: call void [mscorlib]System.Console::WriteLine(string)
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::message
IL_0006: call void [mscorlib]System.Console::WriteLine(string)
IL_000b: ret
} // end of method Singleton::DoIt
Note the added argument on the stack to this.

Regards,
Jeff
I would expect static to be faster than creating an instance of an

object<<

*** Sent via Developersdex http://www.developersdex.com ***
Jan 24 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Axehelm | last post: by
5 posts views Thread by Marijn | last post: by
5 posts views Thread by Naren | last post: by
6 posts views Thread by Dumitru Sipos | last post: by
3 posts views Thread by Bob | last post: by
10 posts views Thread by A.M | last post: by
4 posts views Thread by ma740988 | last post: by
53 posts views Thread by fdmfdmfdm | last post: by
14 posts views Thread by Jess | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.