DEV Community

Cover image for Using static class and extends methods in C#
Dany Paredes
Dany Paredes

Posted on • Updated on

Using static class and extends methods in C#

The static keyword is very useful in c# when we need to use class, method, or property without creating an object instance is very common using a static class as a box or container for properties helpers or methods.

For example, our boss said the employee object needs to be able to show the salary in the local currency, the simple solution is to add a method into the employee class to take the location and format the salary.

But why Employee class needs to be responsible to format the amount to local culture?

A good solution is to create a helper static class to manage the salary amount and return with the format, extracting unrelated responsibility from the Employe class and move it to the SalaryFormat static class as a helper. So let's go.

1- Our Employee class have the Salary and the LocalCurrencySalary

public class Employee
{
    public string Name { get; set; }
    public int Salary { get; set; }
    public string LocalCurrencySalary { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

2- The static class SalaryFormat exposes properties for supported cultures and the LocaleSalary method for returns the salary.

public static class SalaryFormat
{
    public const string DominicanRepublic = "es-DO";
    public const string Spain = "es-ES";
    public const string Usa = "es-US";    
    public static string Culture { get; set; }

    public static string LocaleSalary(int amount)
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo(SalaryFormat.Culture);
        return amount.ToString("C");
    }
}
Enter fullscreen mode Exit fullscreen mode

3- Using the static SalaryFormat is easy only needs to configure culture for SalaryFormat, and call the static method LocaleSalary passing the salary as parament.

static void Main(string  args)
{
    var customer =  new Employee { Name = "Dany" , Salary = 100};
    SalaryFormat.Culture = SalaryFormat.DominicanRepublic;

    customer.LocalCurrencySalary = SalaryFormat.LocaleSalary(customer.Salary);

    Console.WriteLine($"{customer.LocalCurrencySalary}");
    Console.ReadKey();
}
Enter fullscreen mode Exit fullscreen mode

Looks nice and clear, the Employee only store the data and don't need to handle the format process, because is the responsibility of SalaryFormat class and also can be used for other class when need to show a formated salary.

Why do I need to call a static class?

True, Why the Salary doesn't have a method to show it in local culture? The extends methods come to help us.

Extension methods

The extension methods allow add methods to custom or .net types without modifying the class himself, another good feature is the extends methods appear as part of the class in the Intellisense so the developers can see your helpers easy, to define an extension method we need add this keyword before the type in the static method signature.

Let's go to convert our code to use a extend methods, first, we will change a bit our code to make it more decoupled, the Salary will be a specific class and will be part of the Employee, the salary knows his location and amount.

1- The class Salary to store the Location and Salary amount.

public class Salary
{
    public string Location { get; set; }
    public int Amount { get; set; }

}
Enter fullscreen mode Exit fullscreen mode

2- Define a struct Location (no enums) provide the allowed locations.

public struct Location
{
    public static string Spain = "es-ES";
    public static string DominicanRepublic = "es-DO";
    public static string USA = "es-EN";
}
Enter fullscreen mode Exit fullscreen mode

3- The SalaryFormat class contains the extend method AsLocationCurrency has the "this" keyword before the Salary type to allow the AsLocationCurrency to be part Salary class without change the Salary class.

public static class SalaryFormat
{
    public static string AsLocationCurrency(this Salary salary)
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo(salary.Location);
        return salary.Amount.ToString("C");
    }
}
Enter fullscreen mode Exit fullscreen mode

4- To use the AsLocationCurrencyMethod, the developer can use it as part of the Employe and Salary property but the class is not modified is just extended.

static void Main(string args)
{

    var employee = new Employee
    {
        Name = "Elmer",
        Salary = new Salary {Amount = 1500, Location = Location.Spain}
    };

    Console.WriteLine($"{employee.Salary.AsLocationCurrency()}");
    Console.ReadKey();
}
Enter fullscreen mode Exit fullscreen mode

Our code is more readable and can be used in every place where the Salary class is used and the IntelliSense can show the available methods so for developers is easy to use.

Hopefully, that will give you a bit of a head-start how and when using Static and Extends methods in C#.

Photo by AltumCode on Unsplash

Top comments (0)