Home > Development & Technical > Generics – Create List & ToArray C#

Generics – Create List & ToArray C#

Generics is new namespace within the .NET 2.0 Framework which i’m sure you all will start using at some point on a project or solving a problem.
 
Now some code examples for you (in C#):
To add the namespace to your class, simply add the using statement below:
using System.Collections.Generic;
 
To create a simple string generic list:
List<string> stringList = new List<string>();
 
Then to add a string to the list, simply do the following:
string helloMessage = "hello welcome to Generics";
stringList.Add(helloMessage)
OR
stringList.Add("hello welcome to Generics")
Then to add to an array of objects (in this example strings) then use the ‘ToArray’ method as follows:
string[] arrayOfStrings = stringList.ToArray();
 
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: