次の例を想定します。データベース接続処理への一般的なアプローチ。
DbConnection.BeginTransaction()
(System.Data.Common から) インスタンスを返しDbTransaction
ます。これは明示的に述べられているので、なぜ明示的にキャストする必要があるのですか?
internal abstract class ADatabase<DbCommandType, DbConnectionType, DbTransactionType> : IDisposable
where DbCommandType : DbCommand, new()
where DbConnectionType : DbConnection, new()
where DbTransactionType : DbTransaction, new()
{
DbConnectionType _connection;
DbTransactionType _transaction;
void Foo()
{
_transaction = _connection.BeginTransaction(); // <-- Error..
}
}
私が得ているエラーは
エラー 1 型 'System.Data.Common.DbTransaction' を 'DbTransactionType' に暗黙的に変換できません。明示的な変換が存在します (キャストがありませんか?)
もちろんキャストできますが、問題はなぜですか?