472,784 Members | 996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,784 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 1257
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Axehelm | last post by:
Okay, I'm in a debate over whether or not static methods are a good idea in a general domain class. I'm personally not a fan of static methods but we seem to be using them to load an object. ...
5
by: Marijn | last post by:
I'd like to know how compilers usually handle static variables that are declared inside a function (as opposed to static class-members). Like in: int counter(){ static int c=0; ++c; return c;...
5
by: Naren | last post by:
Hello Grp, Correct me if I am wrong. static member functions can act only on static member varaibles.It can accessed by using the name of the class. Then why is there an access controller. ...
6
by: Dumitru Sipos | last post by:
Hello everybody! is there possible to have a function that is both static and virtual? Dumi.
3
by: Bob | last post by:
I have an abstract class Thing which has a static method Thing GetThing(). Class Something inherits from Thing. SomeThing supplies static info for GetThing to get stuff from the database to create...
10
by: A.M | last post by:
Hi, Can Global object have non-static methods? If answer is yes, then How can I access them in pages? I have following property in Global.asax.cs, but when I try to use it in pages, I receive...
8
by: nytimescnn | last post by:
I've read some discuession about lock() for thread-safe. I am wondering what will be the differce between below two code segment? Code 1: class A { private static Object padlock = new...
4
by: ma740988 | last post by:
Referencing source snippet below, the actual contruction of the foo objects is done in a class. In that regard, I chose methods, class1_construct and class2_construct for demonstration purposes....
53
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.