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

Static method vs instance method

Hi,

I'm retouching some utility classes used everywhere across our app, and
there are certain methods used everywhere and pretty frequently. I'm
changing them from instance methods to static ones, so to use them you
don't need to create an instance of that utility class.

So my question is - is there any difference/danger of using a static
method vs instance method.

Example:

//--------- CODE ---------------
class CUtil
{
public string GetColumn(string s)
{
string[] ss = s.Split('.');
if (ss.Length > 1)
return ss[1];
else if (ss.Length > 0)
return ss[0];
else return String.Empty;
}
}
//------ VERSUS: -------------
class CUtil
{
public static string GetColumn(string s)
{
... same implementation ...
}
}
// -------- END OF CODE -------
Any possible memory/performance issues?

Thank you,
MuZZy
Dec 25 '05 #1
4 2083
MuZZy <tn*@newsgroups.nospam> wrote:
I'm retouching some utility classes used everywhere across our app, and
there are certain methods used everywhere and pretty frequently. I'm
changing them from instance methods to static ones, so to use them you
don't need to create an instance of that utility class.

So my question is - is there any difference/danger of using a static
method vs instance method.


If they're stateless, there's no danger. It does mean you can't make
them virtual and override them in a derived class, but that doesn't
sound like it's an issue here.

--
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
Dec 25 '05 #2
No problem in the case you described.

"MuZZy" <tn*@newsgroups.nospam> wrote in message
news:eP**************@TK2MSFTNGP12.phx.gbl...
Hi,

I'm retouching some utility classes used everywhere across our app, and
there are certain methods used everywhere and pretty frequently. I'm
changing them from instance methods to static ones, so to use them you
don't need to create an instance of that utility class.

So my question is - is there any difference/danger of using a static
method vs instance method.

Example:

//--------- CODE ---------------
class CUtil
{
public string GetColumn(string s)
{
string[] ss = s.Split('.');
if (ss.Length > 1)
return ss[1];
else if (ss.Length > 0)
return ss[0];
else return String.Empty;
}
}
//------ VERSUS: -------------
class CUtil
{
public static string GetColumn(string s)
{
... same implementation ...
}
}
// -------- END OF CODE -------
Any possible memory/performance issues?

Thank you,
MuZZy

Dec 25 '05 #3
Jon Skeet [C# MVP] wrote:
MuZZy <tn*@newsgroups.nospam> wrote:
I'm retouching some utility classes used everywhere across our app, and
there are certain methods used everywhere and pretty frequently. I'm
changing them from instance methods to static ones, so to use them you
don't need to create an instance of that utility class.

So my question is - is there any difference/danger of using a static
method vs instance method.


If they're stateless, there's no danger. It does mean you can't make
them virtual and override them in a derived class, but that doesn't
sound like it's an issue here.


Yeap, it's just a helper class of small functions like the one in the
sample, this class will not be inherited and will not have any
non-static members; i will actually hide the constructor as well so no
brick-head will try to screw with it:)

MuZZy
Dec 25 '05 #4
Hello!

Mind you that the C# 2.0 language (if this is your platform) allows classes
to be marked with the "static" modifoer. This forces the compiler to allow
only static members - which is probably what you're looking for.

This btw. also relieves you from creating a private constructor (or should
you need to initialize data on first request, you could provide a static
constructor).

public static class Utility
{
public static void DoSomething() { .. }
}

I think such situations as the one you're referring to are prime candidates
for "static" promotings.

--
With regards (and merry christmas)
Anders Borum / SphereWorks
Microsoft Certified Professional (.NET MCP)
Dec 25 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Chris Clement | last post by:
I have been handed a project that someone else started and most of it was developed in the VS.NET design mode. For whatever reasons, when I try to make changes to the controls in VS.NET design...
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
3
by: Dave | last post by:
Hi, Is there a general rule to use 'static' on a class member? It seems uneccessary to have to create an instance of an object just to use it's methods where declaring something as static makes...
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....
2
by: superseed | last post by:
Hi, I'm pretty new to C#, and I'm quite stuck on the following problem. I would like to add to my application a Windows.Form (singleton) on which I could display a message of one of the...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
5
by: Doru Roman | last post by:
Hi, Can somebody explain please the meaning and use of a STATIC method? Thanks, Doru
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...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
14
by: Jordan Marr | last post by:
I have the following class: class ProvisionCollection { ... private int m_VarianceCount; public int VarianceCount { get { return m_VarianceCount; }
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
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...
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
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.