dimanche 28 juin 2015

should c# static getter be thread safe

I am wondering if this class is thread safe

can I use Currencies as getter?

Should I lock GetLiveExchangeRates() method?

public class CurrencyManager
{
    public static List<CurrencyModel> Currencies { get; private set; }
    private static readonly object LockObj = new object();

    public CurrencyManager()
    {
        Currencies = new List<CurrencyModel>();
    }

    public static void GetLiveExchangeRates()
    {
        lock (LockObj)
        {
            Currencies = GetSomeFooLiveDataFromInternet();
        }
    }
}

EDIT

How would you refactor it?

Aucun commentaire:

Enregistrer un commentaire