Re: confusion on interface IDataReader
Peter Osucha <[email protected]>
| Newsgroups | gmane.comp.windows.devel.dotnet.cx |
|---|---|
| Message-ID | <[email protected]> |
Shawn, is it correct to way the method returns an interface? I would think the 'correct' way to say what the method returns is that it returns an object that implements the IDataReader interface... (And yes, this was confusing since I didn't pay attention to what the SqlCommand object was returning - an object that implemented IDataReader)... > -----Original Message----- > From: Discussion relating to the specifics of the C# and > Managed C++ languages [mailto:[email protected]] > On Behalf Of Shawn Wildermuth > Sent: Tuesday, June 06, 2006 4:53 PM > To: [email protected] > Subject: Re: [DOTNET-CX] confusion on interface IDataReader > > As an adjuct (hopefully not to confuse the issue), you can > certainly define a method (or property) in .NET that returns > an interface: > > IDataReader GetDataReader() > { > // ... > } > > You can't return an instance of an IDataReader, but have to > return a reference to an object that supports IDataReader: > > IDataReader GetDataReader() > { > // ... > SqlDataReader rdr = cmd.ExecuteReader(); > > // An implicit cast from SqlDataReader to IDataReader is done here > return rdr; > > // You could also (to make the code a little more readable: > // return (IDataReader) rdr; > // - or - > // return rdr as IDataReader; > > } > > That's perfectly valid, but I understand your confusion... > > Thanks, > > Shawn Wildermuth > http://adoguy.com > C# MVP, MCSD.NET, Author and Speaker =================================== This list is hosted by DevelopMentor® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com