473,395 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

interface implementation

If s has type System.IO.Stream, why does ((IDisposable) s).Dispose()
work while s.Dispose() fails? The error is:

error CS0117: 'System.IO.Stream' does not contain a definition for
'Dispose'

This seems impossible since Stream implements IDisposable.

Aug 9 '06 #1
5 8654
It doesn't work because implementers of interfaces don't have to expose
their members publically. The Stream class does not expose the Dispose
method publically, which is why you need the cast.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<st*********@hotmail.comwrote in message
news:11*********************@75g2000cwc.googlegrou ps.com...
If s has type System.IO.Stream, why does ((IDisposable) s).Dispose()
work while s.Dispose() fails? The error is:

error CS0117: 'System.IO.Stream' does not contain a definition for
'Dispose'

This seems impossible since Stream implements IDisposable.

Aug 9 '06 #2
Nicholas Paldino [.NET/C# MVP] wrote:
It doesn't work because implementers of interfaces don't have to expose
their members publically. The Stream class does not expose the Dispose
method publically, which is why you need the cast.
Really?

class Foo : IDisposable {
protected void Dispose() {}
}

error CS0536: 'Foo' does not implement interface member
'System.IDisposable.Dispose()'. 'Foo.Dispose()' is either static, not
public, or has the wrong return type.

Aug 9 '06 #3
Yes, really:

class Foo : IDisposable
{
void IDispose.Dispose()
{
}
}
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<st*********@hotmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
Nicholas Paldino [.NET/C# MVP] wrote:
>It doesn't work because implementers of interfaces don't have to expose
their members publically. The Stream class does not expose the Dispose
method publically, which is why you need the cast.

Really?

class Foo : IDisposable {
protected void Dispose() {}
}

error CS0536: 'Foo' does not implement interface member
'System.IDisposable.Dispose()'. 'Foo.Dispose()' is either static, not
public, or has the wrong return type.

Aug 9 '06 #4
That should be:

class Foo : IDisposable
{
void IDisposable.Dispose()
{
}
}

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<st*********@hotmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
Nicholas Paldino [.NET/C# MVP] wrote:
>It doesn't work because implementers of interfaces don't have to expose
their members publically. The Stream class does not expose the Dispose
method publically, which is why you need the cast.

Really?

class Foo : IDisposable {
protected void Dispose() {}
}

error CS0536: 'Foo' does not implement interface member
'System.IDisposable.Dispose()'. 'Foo.Dispose()' is either static, not
public, or has the wrong return type.

Aug 9 '06 #5
Nicholas Paldino [.NET/C# MVP] wrote:
class Foo : IDisposable
{
void IDisposable.Dispose()
{
}
}
Wow, I didn't know about that syntax. Thanks!

Aug 9 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
3
by: Sai Kit Tong | last post by:
I posted for help on legacy code interface 2 days ago. Probably I didn't make it clear in my original mail. I got a couple of answers but none of them address my issues directly (See attached...
3
by: John Underwood | last post by:
Hi.. I was looking at interface, and I have a example in the docs i'll paste below.. I'm not grasping what you would gain by using a interface, does any one have a brief description of their...
21
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered...
15
by: jon | last post by:
How can I call a base interface method? class ThirdPartyClass :IDisposable { //I can not modify this class void IDisposable.Dispose() { Console.WriteLine( "ThirdPartyClass Dispose" ); } } ...
4
by: Ray Dukes | last post by:
What I am looking to do is map the implementation of interface properties and functions to an inherited method of the base class. Please see below. ...
6
by: Ricky W. Hunt | last post by:
It's dawning on my a lot of my problems with VB.NET is I'm still approaching it in the same way I've programmed since the late 70's. I've always been very structured, flow-charted everything, used...
8
by: Gregory | last post by:
I have a question about using STL containers in C++ class public interface. Lets say that I want to return some container from class method or accept class method parameter as some container. For...
15
by: Xah Lee | last post by:
On Java's Interface Xah Lee, 20050223 In Java the language, there's this a keyword “interface”. In a functional language, a function can be specified by its name and parameter specs....
52
by: Ben Voigt [C++ MVP] | last post by:
I get C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.