Pergunta

//
// Summary:
//     Returns the specified System.DateTime object; no actual conversion is performed.
//
// Parameters:
//   value:
//     A date and time value.
//
// Returns:
//     value is returned unchanged.
public static DateTime ToDateTime(DateTime value);

Why does System.Convert has ToDateTime that accepts DateTime ?

The method documentation states the value remain unchanged.

Foi útil?

Solução

Convention, predictability and because the IConvertable defines the method ToDateTime.

I believe that under the covers, System.Convert simply runs through all the combinations of the IConvertable classes.

Outras dicas

The actual code is indeed:

public static DateTime ToDateTime(DateTime value)
{
  return value;
}

This confirms the value will be unmodified. There are a lot of convert methods. I think it is there just because it should accept all primitive objects in the signatures.

I.e. when using reflection, if would be unexpected if a convert from a DateTime to DateTime is not supported.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top