newbee need help on generic list

  • Thread starter Thread starter Raymond Du
  • Start date Start date
R

Raymond Du

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
 
Raymond Du said:
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?

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?

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.
}

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.
 
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>?

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 said:
(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 said:
(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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top