Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Where to put general methods?

Tags:

methods

c#

In my application I have a method to check a variable is a number or not. This method is called more than once in my code. The method is called from the codebehind pages. Like this method I have more methods which i cannot place within a single class (like Staff). Where should I put this kind of methods?

like image 245
Martijn Avatar asked Dec 21 '25 02:12

Martijn


2 Answers

In a utility class file.

like image 196
cjk Avatar answered Dec 22 '25 17:12

cjk


In C# 3.0, I would probably make this an extension method on the string class. I would group all of my string extensions into a single static class to improve readability.

 public static class StringExtensions
 {
      public static bool IsNumeric( this string source )
      {
          if (string.IsNullOrEmpty( source ))
          { 
               return false;
          }
          ...
      }

      public static bool IsMoney( this string source )
      {
          ...
      }

      ...
 }

Usage:

 if (amountLabel.Text.IsNumeric())
 {
    ...
 }
like image 20
tvanfosson Avatar answered Dec 22 '25 18:12

tvanfosson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!