Similar to Varun's answer, I just wanted to do a full implementation.
public class CodeDetailsList
{
private static readonly CodeDetailsList _instance = new CodeDetailsList();
public static CodeDetailsList Instance
{
get { return _instance; }
}
public ReadOnlyCollection<CodeDetails> lstCodeDetailst { get; private set; }
private codeDetailsList()
{
var masterList = new List<CodeDetails>();
masterList.Add(new CodeDetails(1, 111, "xxxxx", "xxxxxxxxxxx"));
masterList.Add(new CodeDetails(2, 222, "yyyyy", "yyyyyyyyyyy"));
//... And so on ...
//mark the list as read only so no one can add/remove/replace items in the list
lstCodeDetailst= masterList.AsReadOnly();
}
}
public class CodeDetails
{
public CodeDetails(id, code, message, details)
{
Id = id;
Code = code;
Message = message;
Details = details;
}
//Mark the setters private so no one can change the values once set.
public int Id { get; private set; }
public int Code { get; private set; }
public string Message { get; private set; }
public string Details { get; private set; }
}
The constructor for CodeDetailsList
will be called once when you first try to access Instance
(If you had other static members in the class the constructor would run on the first time any static member was called).
Because lstCodeDetaillst
is a ReadOnlyCollection
callers will not be able to add, remove, or replace objects in the list. Also because now CodeDetails
has private setters all of the items in it are effectively "read only" too.