Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List Quey

Status
Not open for further replies.

Matsul

IS-IT--Management
May 6, 2002
140
0
0
BE
I have a method

public List<string> getMList()
{

List<string> listM=null;

listContracts.ForEach(delegate(Cobj contract)
{
listM.Add(contract.M);

});

return (listM);
}


The problem is I need to get just the first item in the list. Say,

string mm=getMList[0]; // does not work

What is the best way to get the first object in a list ?

thanks.

 
Why would you want to return a list, if you only need one item?
Code:
string mm = listContracts.Count > 0
  ? listContracts[0].M 
  : string.Empty;
Anyways, if you want to make the whole list of M-properties first, don't forget to initialize listM (you cannot add items to a null-reference).
Code:
List<string> listM = new List<string>();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top