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