Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / June 2007

Tip: Looking for answers? Try searching our database.

newbee need help on generic list

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Raymond Du - 29 Jun 2007 07:03 GMT
Hi,

For the following code snippets:

public static List<T> getListFromConfig<T>(List<T> objList, int uniqueID,
string
fileDirectory, string fileName, string configName)
           where T: new()
{
   //some codes to add items to objList
   return objList;
}

I have the following questions:
(1)  "getListFromConfig<T>", what is the purpose of <T>?  Does
getListFromConfig<List<T> > make sense at all?

(2) What does "where T: new()" do?

(3) The function take a List<T> as the first parameter: objList, then it
returns objList. Will the following codes do the same thing?

public static void getListFromConfig<T>(List<T> objList, int uniqueID,
string
fileDirectory, string fileName, string configName)
           where T: new()
{
  //some codes to add items to objList
  //return nothing here since objList will be sent back to the caller.
}

TIA
Alberto Poblacion - 29 Jun 2007 07:58 GMT
> public static List<T> getListFromConfig<T>(List<T> objList, int uniqueID,
> string
[quoted text clipped - 7 lines]
> I have the following questions:
> (1)  "getListFromConfig<T>", what is the purpose of <T>?

   The value that you pass in <T> is then used in every T contained in the
function.

> Does getListFromConfig<List<T> > make sense at all?

    Since the method creates a List<T>, if you pass a List<T> in place of
<T>, you would be using a List of Lists, assuming that another T is itself
defined in the block containing your code. Legal, but pretty ugly. I would
avoid doing that.

> (2) What does "where T: new()" do?

   The "where" introduces a "constraint". It is used to specify some
characteristics that the argument T must satisfy in order to be allowed to
your method. In this case, "new()" means that "T must have a default
constructor".

> (3) The function take a List<T> as the first parameter: objList, then it
> returns objList. Will the following codes do the same thing?
[quoted text clipped - 7 lines]
>   //return nothing here since objList will be sent back to the caller.
> }

  It is not exactly the same. In the first case, the method could return a
different list than it receives (doing objList=new List<T>()), while
preserving the original list. The last version can only add or remove values
from the existing list.
Jon Skeet [C# MVP] - 29 Jun 2007 09:02 GMT
> For the following code snippets:
>
[quoted text clipped - 10 lines]
> I have the following questions:
> (1)  "getListFromConfig<T>", what is the purpose of <T>?

That shows it's a generic method with one type parameter (T).

> Does getListFromConfig<List<T> > make sense at all?

No - the bit in the angle brackets is just a list of type parameters;
List<T> isn't a type parameter, it's a generic type itself.

> (2) What does "where T: new()" do?

It's a constraint which means that the method will accept a type
argument for T which has a parameterless constructor - so you could
use getListFromConfig<object> but you couldn't use
getListFromConfig<string> for example.

> (3) The function take a List<T> as the first parameter: objList, then it
> returns objList. Will the following codes do the same thing?

If it doesn't actually change the value of objList (as a variable),
yes.
However, in some cases it's useful to include a return value for the
purpose of chaining method calls together. For instance,
StringBuilder's Append methods don't really need to return anything,
but because they each return "this", you can use:

sb.Append("x").Append("y").ToString();

Jon

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.