Creating a Utilities Class?
You will be better off using a static
class with static
methods. Then you won't need to instantiate your utilities class to use it. It will look something like this:
public static Utilites
{
public static int sum(int number1, int number2)
{
test = number1+number2;
return test;
}
}
Then you can use it like this:
int result = Utilites.sum(1, 3);
If you are working with .NET 3.0 or above, you should look into extension methods. They allow you to write a static
function that will act against a particular type, like Int32
, while seeming to be a method on that object. So then you could have: int result = 1.Add(2);
.
Try this out; it might just show you another way. ;)
C# Tutorial - Extension Methods
You should make it a static
class, like this:
public static class Utilities {
public static int Sum(int number1, int number2) {
return number1 + number2;
}
}
int three = Utilities.Sum(1, 2);
The class should (usually) not have any fields or properties. (Unless you want to share a single instance of some object across your code, in which case you can make a static
read-only property.