Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Interface Inheritance (Basics)

Why does the following produce a compiler error:

public interface OwnSession : ISession { }

[...]
OwnSession s = SessionFactory.OpenSession(); // compiler error (in german unfortunately)
[...]

"SessionFactory" returns a "ISession" on "OpenSession()" (NHibernate)

like image 914
Anton Biller Avatar asked Dec 06 '22 03:12

Anton Biller


1 Answers

You should cast the result:

OwnSession s = (OwnSession) SessionFactory.OpenSession();

If OpenSession() returns an ISession type, it could be anything that implements ISession, so you have to tell the compiler you expected a OwnSession type (only if you are sure it will return that of course)

On the other hand, you could declare your variable as ISession, and continue working with that. Unless you want to use methods or properties from the OwnSession type which are not available in the ISession interface spec.

like image 141
Philippe Leybaert Avatar answered Dec 27 '22 20:12

Philippe Leybaert