473,382 Members | 1,749 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,382 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 8653
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?

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.