views:

193

answers:

11

I found myself having to remove the first line of a string quite often while working on a text parser in C#. I put together a simple function to do that for me, but coming from a PHP background, I have no idea where to put it since I can't define a function outside a class. What's a customary way of doing that in .NET? Do I create a static class to store my function?

+18  A: 

I generally make a Helper or Utility static class and then put corresponding helper functions in there.

Additionally, I try to keep the Helper and Utility classes grouped logically - putting the text parsing functions alongside the object conversion functions is nonsensical. The confusion is cleared up with a TextUtils class and a ConversionUtils class.

JustLoren
+1 beat me to it.
Stan R.
You'll probably also benefit from making such functions extension methods.
Matt Lacey
+2  A: 

Usually I create a Utilities class and define static helper methods.

Stan R.
+4  A: 

I don't think there's a standard for this. I tend to make a static class called BlahUtil. For your example, I'd make it a static method on StringUtil. This helps me group related methods into sensible units, making it easier to discover them and share them across teams.

You can also then choose which of these methods are exposed as extension methods (since c# 3.0):

public static class StringUtil
{
    public static string RemoveFirstLine(this string multiLineString)
    {
        // ...
    }
}
Drew Noakes
+17  A: 

Yes, static helper classes are usually the way to do this.

Also, in C# 3 you can declare the method like this:

public static string RemoveFirstLine(this string s) {
    ...
}

to make it an extension method. Then you can call it on any string as if the method was declared on the string type itself.

Joey
For what Butterfly is describing, extension methods are definitely the way to go.
Bomlin
Wow, extension methods are neat! That's what I ended up using. Thanks for mentioning them.
Butterfly
A: 

I'd create a static worker class for such functions. Maybe not the nicest way, but the one which keeps things simple... ;)

K

KB22
A: 

Use an extension method for a string. That's what they are for.

Samuel Carrijo
Damn added while I was writting my answer!!
David Kiff
+1  A: 

I've done the static "helper" classes but after some analysis; this type of helper function always ends up as a distinct class implementation. In your case you'd have a "basic text parser" class and a derived class that overrides the "parse" method.

Austin Salonen
A: 

You can use a class with static methods. Something like ParserUtils.RemoveFirstLine(). On .NET 3.5 and above you can sometimes use extension methods when your utility functions are related to a class you cannot modify, like the String class. Intellisense will show the extension method on any string object in the project.

Andre
+5  A: 

Be careful!

  • Generic utility functions which are cross cutting should live in a higher utility namespace. String parsing, File manipulation, etc.

  • Extension objects should live in their own namespace.

  • Utility functions that apply to a specify set of business objects or methods should live within the namespace of those objects. Often with a Helper suffix, ie BusinessObjectHelper. Naming is important here. Are you creating a container for miscellaneous methods, or would it make more sense to group them into specialized objects, ie a parser?

bryanbcook
+3  A: 

If you are using C# 3.0, you might want to consider using an extension method!

public static class StringExtensions
{
    public static string RemoveFirstLine(this string myString)
    {
         return myString.Remove("line..!");
    }
}

Then in code you can do this:

string myString = "Hello World etc";

string removedLineString = myString.RemoveFirstLine();
David Kiff
I generally hate extension methods. They break class and library encapsulation of functionality. Who am I to "fix" the .net library? Now someone runs across some method in my code myString.Whargarble(). Where do they find that method? I think myString = MyCompany.StringUtils.Whargarble(myString) is a more clear statement of what's going on.
chris
Thats a fair point, its up to you how to implement your code :). Im not sure an extension is regarded as a fix, its more of an extenstion to it! They find it by clicking on it and selecting F12, and identify its an extension method by the little icon in intellisense. Its unlikely they will come across it if you add them into an extensions namespace; you then explicity "opt-in" to using it. I have no issues with it personally. How would it "break encapsulation of functionaility"!? Its encapsulated within an extensions class!
David Kiff
A: 

Extensions are the way to go in those case. It literally add your function to the occurence. Only thing is that it's not possible to do static method in 2008.

David Brunelle