what is a helper class?

  • Thread starter Thread starter Mike P
  • Start date Start date
M

Mike P

I have heard about helper classes, but can somebody please explain to me
what they are and what they are used?
 
Mike P said:
I have heard about helper classes, but can somebody please explain to me
what they are and what they are used?

They're usually classes just with static methods acting on another
class. Something like:

public static class StringHelper
{
public static string Reverse(string input)
{
(Code goes here)
}
}
 
Jon,

What would be the reason for creating a class just with static methods
acting on another class?


Thanks,

Mike
 
Mike P said:
What would be the reason for creating a class just with static methods
acting on another class?

It collects them together. Often you can't add methods to a class
itself, because you don't have the code - I can't add the Reverse
method to string, for example.

If you're going to frequently do the same things with an instance of a
class, it's worth putting that somewhere rather than repeating the
code.

Another example is in my miscellaneous utility library
(http://pobox.com/~skeet/csharp/miscutil) - I've got a helper class for
working with streams. It has a set of methods to copy the contents of
one stream to another, and another set to read the complete contents of
a stream as a byte array.
 
Jon Skeet said:
It collects them together. Often you can't add methods to a class
itself, because you don't have the code - I can't add the Reverse
method to string, for example.

You can with C# V3.0 by adding "this" to the methods argument, ie:

public static class StringHelper
{
public static string Reverse(this string input)
{
(Code goes here)
}
}

....

using StringHelper;

string myString;
....

string reveresedString = myString.Reversed();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top