473,322 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 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 1273
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.