In this short example I will show you how easy it is to actually create a List object and add some objects to it.
The List Object resides in the System.Collections.Generic;
That's why you must include that namespace before creating the List Object.
That it achieved by adding this line at the top of your .cs file.
using System.Collections.Generic;
with that said, let's jump to the code.
****************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Generic;
namespace List_Test
{
class Program
{
static void Main(string[] args)
{
// Creates the List Object and assign space to it.
List<string> list = new List<string>();
list.Add("one");//adds "one" to the list
list.Add("two");//adds "two" to the list
list.Add("three");//adds "three" to the list
/*ITERATES OVER THE LIST OF TYPE STRING, IN EACH ITERATION
THE ACTUAL VALUE IS ASSIGNED TO THE TEMP VARIABLE tempString*/
foreach (string tempString in list)
{
//shows the ToString() method return string in the Command Prompt.
Console.WriteLine(tempString.ToString());
}
//END MESSAGE
Console.WriteLine("END, press any key to quit.");
Console.ReadKey();//WAITS FOR USER INPUT TO CLOSE THE APPLICATION
}
}
}
********************************************************SCREENSHOTS:
Step 1 :
Add the System.Collections.Generic to the top.
Step 2:
Place The code in the Main method.
Step 3:
Run the application. (F5 if using Visual C# or Microsoft Visual Studio)
I hope it helped you out in some way.
No comments:
Post a Comment