Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Performance / August 2003

Tip: Looking for answers? Try searching our database.

Static vs. instance Methods

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Victor Jones - 22 Aug 2003 15:33 GMT
I was involved recently in desiging a middle tier class where we essentially
need not track the state of the object, hence  a class with static methods
was preferred. But there were some debate as to whether it made sense to
implement it as a singleton.

I wanted to know if there is any performance difference in CLR when calling
a static method vs. an call to instance method (where just a single instance
is used) - considering that there might be an initial expense in creating
the object on the heap.

Thanks
Victor
Bill Joyce [MSFT] - 25 Aug 2003 09:50 GMT
Go with the static methods.  Why create an object if you don't want or need
to store state?  And if you do allow a singleton object, it's only a matter
of time before someone does add data to the class, and then you may have
synchronization, deadlock, and/or scalability issues.

To answer your question, the static methods will be faster.  When you call a
non-static object method, the first thing that happens is that the "this"
pointer is pushed onto the stack.  There is no such step when calling static
methods.  Of course, the performance benefit is likely negligible in an
n-tier world - things like network hops make saving a few cycles irrelevant.
So this decision ultimately comes down to what is easier, safer, and cleaner
to implement and maintain.  Client code calling static methods is generally
easier to read than code calling non-static methods, and it's a better OOP
design.

-Bill

> I was involved recently in desiging a middle tier class where we essentially
> need not track the state of the object, hence  a class with static methods
[quoted text clipped - 8 lines]
> Thanks
> Victor
Anil - 25 Aug 2003 13:46 GMT
One thing to watch: Static methods are not supported for remoting. So, if
you want to seperate your layers into an n-tier deployment using remoting
you probably want to avoid static calls, as the calls will always be excuted
on the client end.

hope this helps,
Anil

> Go with the static methods.  Why create an object if you don't want or need
> to store state?  And if you do allow a singleton object, it's only a matter
[quoted text clipped - 28 lines]
> > Thanks
> > Victor
Victor Jones - 25 Aug 2003 22:42 GMT
Thanks for your input!

> One thing to watch: Static methods are not supported for remoting. So, if
> you want to seperate your layers into an n-tier deployment using remoting
[quoted text clipped - 45 lines]
> > > Thanks
> > > Victor
Ice - 27 Aug 2003 20:54 GMT
If state is introduced into a static method, its the same situation...

I've found it interesting that server-side solutions do not support static
methods purely, a singleton always has to be used to wrap the object that
should implement static methods.

ice
> Go with the static methods.  Why create an object if you don't want or need
> to store state?  And if you do allow a singleton object, it's only a matter
[quoted text clipped - 28 lines]
> > Thanks
> > Victor
Null - 29 Aug 2003 14:45 GMT
Uhm, what is a "singleton"?

Pardon my ignorance, but I'm curious to learn.

TIA.

Cheers!

> If state is introduced into a static method, its the same situation...
>
[quoted text clipped - 44 lines]
> > > Thanks
> > > Victor
Jon Skeet - 29 Aug 2003 14:52 GMT
> Uhm, what is a "singleton"?
>
> Pardon my ignorance, but I'm curious to learn.

It's basically a class which guarantees that only one instance will be
created of it, and provides a means of accessing that instance.

See http://www.pobox.com/~skeet/csharp/singleton.html for more
information about implementing a singleton in C#/.NET.

Signature

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

Null - 29 Aug 2003 15:02 GMT
Jon,

Thanks very much for the informative link - I appreciate it.

I've got so much to learn!

> > Uhm, what is a "singleton"?
> >
[quoted text clipped - 5 lines]
> See http://www.pobox.com/~skeet/csharp/singleton.html for more
> information about implementing a singleton in C#/.NET.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.