Id like to know what this piece of code is doing please. My comments are next to each expression as to what I think is happening.
// Resetting the generic type T and assigning to a local variable t
T t = default(T);
// Assigning the ExecuteReader method to a local variable named reader in order to get the result of the command.
var reader = command.ExecuteReader();
// Whilst the reader variable is reading the rows.
if (reader.Read())
// Pass the reader variable to the make method and assign it to t.
t = make(reader);
// return the value type t.
return t;
This is the Method signature declaration
public static T ReadList
VulpesPosted Mar 1, 2012, 2:33 PM
VulpesPosted Mar 2, 2012, 3:18 PM
int t = default(int);
which would set 't' to zero which is the default value for ints.
The only difference here is that the type is a type parameter, T:
T t = default(T);
If T happened to be int, then this line would be equivalent to the earlier one.
If T were string, then we would have:
string t = null; // because default(string) is null
Guest UserPosted Mar 2, 2012, 11:43 AM
With reference to your comment
// Sets the local variable t to the default value of the type parameter, T
T t = default(T);
I was under the impression that if a type is declared before a variable ie T t, its effectively "Getting" so that the t reference can be returned with a return statement ie return t;
With the opposite being the "Setting" like so...
t = default(T);
or even further down in the code
// Passing the reader reference to the make method and "Setting" to local variable t.
t = make(reader);
Thanks