Where should I put different functions in a .NET project?

I had to delete the first line of the line quite often while working on a text parser in C #. I put together a simple function to do this for me, but based on the PHP background, I have no idea where to put it, since I cannot define a function outside the class. What is the usual way to do this in .NET? Create a static class to hold my function?

+6
function string c # text
source share
11 answers

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

In addition, in C # 3, you can declare a method as follows:

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

to make it an extension method. You can then call it on any line, as if the method was declared for the type of the line itself.

+18
source share

Usually I create a static Helper or Utility class and then put the corresponding helper functions there.

In addition, I try to ensure that the Helper and Utility classes are grouped logically - adding text parsing functions along with object conversion functions is pointless. The confusion is cleared by the TextUtils class and the ConversionUtils class.

+20
source share

Be careful!

  • Common utility functions that are cross-cutting should live in a wider namespace. Parsing strings, manipulating files, etc.

  • Extension objects must live in their own namespace.

  • Utilities that apply to a specific set of business objects or methods must live in the namespace of these objects. Often with an assistant suffix, i.e. BusinessObjectHelper. Naming is important here . Do you create a container for different methods or will it make more sense to group them into specialized objects, i.e. a parser?

+6
source share

I do not think there is a standard for this. I try to make a BlahUtil static class. For your example, I would make a static method on StringUtil . This helps me group related methods into reasonable units, making it easier to spot them and share them between teams.

Then you can also choose which of these methods will be shown as extension methods (starting with C # 3.0):

 public static class StringUtil { public static string RemoveFirstLine(this string multiLineString) { // ... } } 
+4
source share

If you are using C # 3.0, you may need to use the extension method!

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

Then in the code you can do this:

 string myString = "Hello World etc"; string removedLineString = myString.RemoveFirstLine(); 
+3
source share

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

+2
source share

I made static "helper" classes, but after some analysis; this type of helper function always ends as a separate class implementation. In your case, you will have a class "main text parser" and a derived class that overrides the "parse" method.

+1
source share

I would create a static working class for such functions. Maybe this is not the most pleasant way, but the one that keeps things simple ...;)

TO

0
source share

Use the extension method for the string. What are they for?

0
source share

You can use a class with static methods. Something like ParserUtils.RemoveFirstLine (). In .NET 3.5 and later, you can sometimes use extension methods when your utility functions are associated with a class that you cannot change, such as the String class. Intellisense will show an extension method for any string object in the project.

0
source share

Extensions are the way to go in this case. This literally adds your function to the occurrence. The only thing that cannot be done with the static method in 2008.

0
source share

All Articles