0

Is there any harm or benefit for having instance methods call static methods to do their work?

If your confused by what I mean, take a look at the example code below. StripFormatting is an instance method and static method. If another developer creates an instance of PhoneUtil then all that is needed is for the StripFormatting method to be called on the instance of the object. If a developer decides not to create an instance of PhoneUtil they can call the static method except this method has a parameter for the PhoneNumber.

public string StripFormatting()
{
        return PhoneUtil.StripFormatting(this.PhoneNumber);
}

public static string StripFormatting(string psPhoneNumber)
{
        string tsPhoneNumber = psPhoneNumber;
        Regex toNotDigit = new Regex("\\D+");
        tsPhoneNumber = toNotDigit.Replace(tsPhoneNumber, "");
        return tsPhoneNumber;
}
jbeverid
  • 291
  • 7
  • 23

1 Answers1

0

Member function or Static methods are chosen based on your software design. There are pros and cons in relation to that design you have chosen.

In general, consider using static methods when that method has no any effect on some type internal state and does not need to store some data inside, in shorts: it's execution only.

In all oher cases conside using instance methods.

Repeat this is a basic idea,as all depends on your design.

For example: looking on the code provided I see that method you wrote can easily be made static.

Tigran
  • 61,654
  • 8
  • 86
  • 123