is there some guideline when to use static methods

T

Tony Johansson

Hello!

Here is the class Category and both methods is this class are instance
methods.
I just wonder I could change and use static methods and fields instead so
is there any best practice when it's adviceable
to use static method.?

class Category
{
private NameValueCollection nvc = new NameValueCollection();

public Category()
{
Init();
}

/// <summary>
/// This method initialize the <key,value> pair collection where key
is category and value is animalType
/// </summary>
private void Init()
{
nvc.Add(CategoryType.Mammal.ToString(), AnimalType.Cat.ToString());
nvc.Add(CategoryType.Mammal.ToString(), AnimalType.Dog.ToString());
nvc.Add(CategoryType.Mammal.ToString(),
AnimalType.Horse.ToString());

//nvc.Add(CategoryType.Bird.ToString(), "Duck");
//nvc.Add(CategoryType.Bird.ToString(), "Kolibri");

//nvc.Add(CategoryType.Insect.ToString(), "Butterfly");
//nvc.Add(CategoryType.Insect.ToString(), "Bee");

//nvc.Add(CategoryType.Marine.ToString(), "Pike");
//nvc.Add(CategoryType.Marine.ToString(), "GoldFish");

//nvc.Add(CategoryType.Reptile.ToString(), "Frog");
//nvc.Add(CategoryType.Reptile.ToString(), "Snake");
}

/// <summary>
/// This method return the values for the corresponding passed key
/// </summary>
/// <param name="key"></param>
/// <returns> An Ilist as string array</string></returns>
public IList<string> GetMyValues(string key)
{
List<string> returnValueList = new List<string>();
foreach (string s in nvc.GetValues(key))
{
returnValueList.Add(s);
}

return returnValueList.AsReadOnly();
}
}

//Tony
 
A

Arne Vajhøj

Here is the class Category and both methods is this class are instance
methods.
I just wonder I could change and use static methods and fields instead so
is there any best practice when it's adviceable
to use static method.?

You should use non-static when you either need more instances
now or could foresee more instances sometimes in the future.

You should use static when you are absolutely sure that you
you do not need instances and will not need instances in
the future.

It is hard to predict the future. So as a consequence you
should always prefer non-static. You should probably use
99.5% non static and only 0.5% static.

You will eventually experience that:
- if you make something static then at some point in time you
will have to spend time converting it to non static
- if you make something non static then you will never convert
it to static

The above is for all/mostly static both fields and methods
(procedural programming style code).

There are some cases where static can and should be used.

1) Fields that has to be shared among instances. They will
almost always be const (they are always implicit static - you
do not specify it) or readonly. It could be a field with
an instance of the System.Random class.

2) Methods in a class that returns an instance of the class.

3) Extension method classes. This is not really static in OO
design way, but the C# syntax requires them to be static.
class Category
{
private NameValueCollection nvc = new NameValueCollection();

public Category()
{
Init();
}

///<summary>
/// This method initialize the<key,value> pair collection where key
is category and value is animalType
///</summary>
private void Init()
{
nvc.Add(CategoryType.Mammal.ToString(), AnimalType.Cat.ToString());
nvc.Add(CategoryType.Mammal.ToString(), AnimalType.Dog.ToString());
nvc.Add(CategoryType.Mammal.ToString(),
AnimalType.Horse.ToString());

//nvc.Add(CategoryType.Bird.ToString(), "Duck");
//nvc.Add(CategoryType.Bird.ToString(), "Kolibri");

//nvc.Add(CategoryType.Insect.ToString(), "Butterfly");
//nvc.Add(CategoryType.Insect.ToString(), "Bee");

//nvc.Add(CategoryType.Marine.ToString(), "Pike");
//nvc.Add(CategoryType.Marine.ToString(), "GoldFish");

//nvc.Add(CategoryType.Reptile.ToString(), "Frog");
//nvc.Add(CategoryType.Reptile.ToString(), "Snake");
}

///<summary>
/// This method return the values for the corresponding passed key
///</summary>
///<param name="key"></param>
///<returns> An Ilist as string array</string></returns>
public IList<string> GetMyValues(string key)
{
List<string> returnValueList = new List<string>();
foreach (string s in nvc.GetValues(key))
{
returnValueList.Add(s);
}

return returnValueList.AsReadOnly();
}
}

This should obviously not be converted to all static.

It is very realistic to imagine more than one Category.

Arne
 
J

J van Langen

?I only use static methods/properties if the information or functionality is
not instance specific.
A static method cannot access non static members.
Non static can access static members.

"Tony Johansson" schreef in bericht

Hello!

Here is the class Category and both methods is this class are instance
methods.
I just wonder I could change and use static methods and fields instead so
is there any best practice when it's adviceable
to use static method.?

class Category
{
private NameValueCollection nvc = new NameValueCollection();

public Category()
{
Init();
}

/// <summary>
/// This method initialize the <key,value> pair collection where key
is category and value is animalType
/// </summary>
private void Init()
{
nvc.Add(CategoryType.Mammal.ToString(), AnimalType.Cat.ToString());
nvc.Add(CategoryType.Mammal.ToString(), AnimalType.Dog.ToString());
nvc.Add(CategoryType.Mammal.ToString(),
AnimalType.Horse.ToString());

//nvc.Add(CategoryType.Bird.ToString(), "Duck");
//nvc.Add(CategoryType.Bird.ToString(), "Kolibri");

//nvc.Add(CategoryType.Insect.ToString(), "Butterfly");
//nvc.Add(CategoryType.Insect.ToString(), "Bee");

//nvc.Add(CategoryType.Marine.ToString(), "Pike");
//nvc.Add(CategoryType.Marine.ToString(), "GoldFish");

//nvc.Add(CategoryType.Reptile.ToString(), "Frog");
//nvc.Add(CategoryType.Reptile.ToString(), "Snake");
}

/// <summary>
/// This method return the values for the corresponding passed key
/// </summary>
/// <param name="key"></param>
/// <returns> An Ilist as string array</string></returns>
public IList<string> GetMyValues(string key)
{
List<string> returnValueList = new List<string>();
foreach (string s in nvc.GetValues(key))
{
returnValueList.Add(s);
}

return returnValueList.AsReadOnly();
}
}

//Tony
 
Top