Link to home
Start Free TrialLog in
Avatar of hamidovt
hamidovt

asked on

Using generics in C# ASP .Net 2.0

Hi,
I am using trying to use List of <T> generics in C# ASP.NET 2.0. In my code I have a a method that returns a list of objects with the following definition. Something like

public static List<Location> GetAll()
   List<Location> locations = new List<Location>();
   ...
   return locations;
}

This part of using generics is OK. My problem is that I would like to pass a List of <T> to another method. The code would look something like this

protected string Locations(List<Location> locations)
{
  string locationsString = string.Empty;
  foreach(Location lc in locations)
  {
    locationsString += lc.Name + " ";
  }
  return locationsString;
}

However I eceive  a compilation error saying that type or namespace name 'List' could not be found.
My questions are:
- is it possible to path a List<T> ?
- what is the right syntax to do that in C# ?

Thanks in advance!
ASKER CERTIFIED SOLUTION
Avatar of ripahoratiu
ripahoratiu
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hamidovt
hamidovt

ASKER

what do you mean by "you want to effectively pass a generic list? Something like: metdhod<T>(List<T> value) ?"  could you give a sample?
....
protected string Locations<T>(List<T> locations) where T : IWithName
{
  string locationsString = string.Empty;
  foreach(T lc in locations)
  {
    locationsString += lc.Name + " ";
  }
  return locationsString;
}

public interface IWithName
{


}

public interface IWithName
{
       string Name
                     {get; set;}

}

... and all the classes that are used as T are implementing IWithName . Is that what you want?
You are indeed right, actually I have missed the using System.Collections.Generic....