473,411 Members | 2,030 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,411 software developers and data experts.

using static method in c# is good?

Nowadays we see the use of methods statics a lot in C#, but if we look for OO that generates a good controversy, because the method statcs aligns the calls and depending on I number him/it of calls can take Time Out then somebody has some concept different from mine regarding the use of methods or classes staticas. An example: If we have the object chair while I am seating nobody more he/she sits down if he goes an object statico, but if we create a new more stancia we will have a new chair so one more person can sit down!
Oct 30 '07 #1
6 2283
Shashi Sadasivan
1,435 Expert 1GB
Static methods and classes generally relate to the singleton pattern.
Its not about c# its about OOP's

so if it is goot for C++ its the same for C#, its how you implement it.

For your wheelchair, you could have instance so taht you can have one wheelchair for eevry user, however servicing might be done only by the manuacturer, or a certain type of wheelchair object has a single manufacturer, would you like to keep assigning the value of the manufacturer eevrytime you instantiate it? (which can lead to al lot of trouble) or would you like to keep it static, so that you can always find who the manufacturer is without having to assign it each time you create the instance?

I have used singletons for contacting paid 3rd party sites where i take information about certain conditions and my other processes use this information.
now every such transaction costs money?
IF that was finding temperature, would you like calling the service each time the process requires it, or would you like to call it once and store it somewhere!

i hope I made sense, otherwise search online for singleto patterns
Oct 30 '07 #2
r035198x
13,262 8TB
Use static variables/methods for fields/actions that are shared among all objects of the same class. If an object is allowed to have it's own value for a certain field or it's own implemenation for a certain action then that field/method should not be declared as static.
Most people misuse the modifier just to make their code compile and run.
Oct 30 '07 #3
Check out this link:
http://en.wikipedia.org/wiki/Singleton_pattern#C.23

There's a subtle difference between static methods and the singleton pattern. For example, you can pass a singleton as a parameter between interfaces/methods/etc., but how would you do that with a static method? As you keep working with the singleton pattern it will become obvious that there is a philosophical difference btw the static methods & the singleton pattern.

That's not to say that using static methods is bad or that singleton pattern is bad. Just that we need to take note of the (subtle) difference between the two.
Oct 30 '07 #4
Plater
7,872 Expert 4TB
I use statics to perform usefull program wide functions.
Such as making a log entry.
Expand|Select|Wrap|Line Numbers
  1. public static void WriteErrorLog(string Message)
  2. {...}
  3. public static void WriteUsageLow(string Message)
  4. {...}
  5.  
Oct 30 '07 #5
r035198x
13,262 8TB
I use statics to perform usefull program wide functions.
Such as making a log entry.
Expand|Select|Wrap|Line Numbers
  1. public static void WriteErrorLog(string Message)
  2. {...}
  3. public static void WriteUsageLow(string Message)
  4. {...}
  5.  
I hope all those those methods are in some Logger class rather than in a Person or Customer class.


The only link between statics and the singleton is that there is only one memory location reserved for a static field that is shared among all objects of the same class. With the singleton "pattern" only one instance of a class is allowed at a time during the lifetime of the program.

Aside from that the two are quite different.
Statics are allocated/resolved at compile time (even though they cannot be called on type parameter types). A singleton is not created at compile time but is an object created dynamically on the heap. The rule is that once one is placed there no other object of the same class can be created.
Oct 31 '07 #6
Plater
7,872 Expert 4TB
I hope all those those methods are in some Logger class rather than in a Person or Customer class.
Yes, I just realized after I started typing the example that it would be to big to include all that stuff. I have a static Logging class so it's Logging.WriteErrorLog()
Oct 31 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Michael Sparks | last post by:
Anyway... At Europython Guido discussed with everyone the outstanding issue with decorators and there was a clear majority in favour of having them, which was good. From where I was sitting it...
5
by: Chris | last post by:
Hi I have a scenario where I've created another AppDomain to dynamically load a DLL(s) into. In this newly loaded DLL I want to call a static method on a class. The problem arise is that I have...
17
by: Picho | last post by:
Hi all, I popped up this question a while ago, and I thought it was worth checking again now... (maybe something has changed or something will change). I read this book about component...
8
by: Fernando Lopes | last post by:
Hi there! Someone has some code sample about when is recommend use a statis method? I know this methos don't want to be initialized and all but I want to know when I need to use it. Tks....
5
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public...
2
by: Steve | last post by:
C# I am new to C#, and new to the whole OO concepts, and so I am sure this is a very stupid question, but I'll ask anyway. When should my methods be static? I have several "Business Objects" in...
6
by: gg9h0st | last post by:
i really wander what makes static method special? in fact i can access a non static method statically using '::' class aclass { function anonstatic() { echo 'non static'; } static...
25
by: David Sanders | last post by:
Hi, As part of a simulation program, I have several different model classes, ModelAA, ModelBB, etc., which are all derived from the class BasicModel by inheritance. model to use, for example...
25
by: =?iso-8859-1?B?QW5kcuk=?= | last post by:
Hello, I'm having a discussion with my colleagues (and boss) over the use of static methods in a class. My colleagues say that static methods should be avoided whenever is possible, because...
6
by: Anthony Smith | last post by:
I can call a class using "->", but it complains about the :: I see on the net where :: is used. Is there a good explanation on when to use one over the other or the differences? $help = new...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.