471,306 Members | 1,305 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,306 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 1991
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Jay | last post: by
3 posts views Thread by Dave | last post: by
8 posts views Thread by Fernando Lopes | last post: by
2 posts views Thread by superseed | last post: by
5 posts views Thread by Doru Roman | last post: by
14 posts views Thread by Jordan Marr | last post: by
reply views Thread by rosydwin | last post: by

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.