Here will explain in detail:
I have
public class Season
{
#region Public Variables
private int _id;
private string _seasonName;
private int _startMonth;
#endregion
#region Public Properties
public int Id
{
get { return _id; }
set { _id = value; }
}
public int StartMonth
{
get { return _startMonth; }
set { _startMonth = value; }
}
public string SeasonName
{
get { return _seasonName; }
set { _seasonName = value; }
}
#endregion
#region Public Constructor
public Season()
{
this._initialize();
}
public Season(string pSeasonName, int _pStartMonth,int pID)
{
this._initialize(pSeasonName, _pStartMonth, pID);
}
#endregion
}
Next I have as show below
IList<Season> seaosns = new List<Season>();
seaosns.Add(new Season("Summer", 4, 1));
seaosns.Add(new Season("Winter", 6, 2));
seaosns.Add(new Season("MidSummer", 10, 1));
Here i need to check , if Ids are repeated in Ilist Items. This can be done by using For Loop, but need to know is there any other solution.
Thanks in Advance.
The most appropriate way to do that is to store the items (either instead, or in addition) in a
Dictionary<int,Season>, using the unique property (Id) as a key in the dictionary. Then you have anO(1)lookup both to test existence, and to fetch by key (Id). Note, however, that a dictionary is not ordered – meaning, it does not guarantee to respect either insertion order, or key order. The order is not defined – so if you need them in a particular order you might need to hold that separately.