473,407 Members | 2,320 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,407 software developers and data experts.

Using interface and dispose

Hello!

I know that the block of code below is the same as using the using clause.
I must have this kind of text because the question is about the statement
((IDisposable)reader).Dispose();

Now to my question:
If I use this statement ((IDisposable)reader).Dispose();
or reader.Dispose();
must mean exactly the same thing.
It's the method Dispose in the TextReader class that is called here because
it's not virtual.
{
TextReader reader = new StreamReader("some filename";
try
{
string line;
while((line = reader.ReadLine()) != null)
{
Console.WrileLine(line);
}
}
finally
{
if (reader != null)
{
((IDisposable)redaer).Dispose();
}
}
}

//Tony
Jun 27 '08 #1
5 1937
On Jun 17, 12:14 pm, "Tony" <johansson.anders...@telia.comwrote:
So which Dispose in which class is called.
I far as I understand it must be in the TextReader class.
In this particular case, yes.

However, suppose you had:

public class OddTextReader : TextReader, IDisposable
{
// Abstract methods etc filled in

void IDisposable.Dispose()
{
// Some stuff here
}
}

then:

using (TextReader reader = new OddTextReader())
{
...
}

would call the OddTextReader's explicit interface implementation of
Dispose.

Jon
Jun 27 '08 #2
Hello!

I tested you example and it was the Dispose of OddTextReader that was
called.
I would understand it if this Dispose had been declared as virtual in
TextReader.
Here is the same as your example. Here I get the same strange result that I
don't understand.
When test is called as an interface it's test in MyDerived that is called in
spite of
having this method Test as public void Test(...) not using virtual
If I replace ITest with MyBase then Test in the MyBase is called which is
correct and understandable.

So can you explain why Test in MyDerived class is called. It should be Test
in MyBase according to my knowledge.

ITest test = new MyDerived();
test.Test();

interface ITest
{ void Test() }

public class MyBase
{ public void Test() {} }

public class MyDerived : MyBase, ITest
{ public void Test() {} }

//Tony

"Jon Skeet [C# MVP]" <sk***@pobox.comskrev i meddelandet
news:82**********************************@2g2000hs n.googlegroups.com...
On Jun 17, 12:14 pm, "Tony" <johansson.anders...@telia.comwrote:
So which Dispose in which class is called.
I far as I understand it must be in the TextReader class.

In this particular case, yes.

However, suppose you had:

public class OddTextReader : TextReader, IDisposable
{
// Abstract methods etc filled in

void IDisposable.Dispose()
{
// Some stuff here
}
}

then:

using (TextReader reader = new OddTextReader())
{
...
}

would call the OddTextReader's explicit interface implementation of
Dispose.

Jon

Jun 27 '08 #3
On Jun 17, 2:38 pm, "Tony" <johansson.anders...@telia.comwrote:

<snip>
So can you explain why Test in MyDerived class is called. It should be Test
in MyBase according to my knowledge.
Well, you're reimplementing ITest (although note that you're doing it
in a different way to my example - you're not using explicit interface
implementation).

Sections 13.4.5 and 13.4.6 of the C# 3 spec go into the details.

Jon
Jun 27 '08 #4
Hello!

Your example work the same even if I implement this Dispose implicit meaning
that it's the Dispose in OddTextReader that is called.
The reason for this accoring to my knowledge is that
the call to Dispose is beibng made as a being a Disposable.

public class OddTextReader : TextReader, IDisposable
{
// Abstract methods etc filled in

void IDisposable.Dispose()
{
// Some stuff here
}
}

then:

using (TextReader reader = new OddTextReader())
{
((IDisposable)reader).Dispose();
...
}

would call the OddTextReader's explicit interface implementation of
Dispose.

//Tony

"Jon Skeet [C# MVP]" <sk***@pobox.comskrev i meddelandet
news:17**********************************@d1g2000h sg.googlegroups.com...
On Jun 17, 2:38 pm, "Tony" <johansson.anders...@telia.comwrote:

<snip>
So can you explain why Test in MyDerived class is called. It should be
Test
in MyBase according to my knowledge.

Well, you're reimplementing ITest (although note that you're doing it
in a different way to my example - you're not using explicit interface
implementation).

Sections 13.4.5 and 13.4.6 of the C# 3 spec go into the details.

Jon

Jun 27 '08 #5
On Jun 18, 7:22 am, "Tony" <johansson.anders...@telia.comwrote:
Your example work the same even if I implement this Dispose implicit meaning
that it's the Dispose in OddTextReader that is called.
Yes - the results were the same (in this case), but I was just
pointing out the difference in case you hadn't spotted it.
The reason for this accoring to my knowledge is that
the call to Dispose is beibng made as a being a Disposable.
Yes.

Jon
Jun 27 '08 #6

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

Similar topics

7
by: Willem van Rumpt | last post by:
Hi all, coming from an unmanaged programming background, I took my time to sort out the IDisposable and finalizer patterns. Just when I thought I had it all conceptually neatly arranged, the...
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" ); } } ...
0
by: Mark Goldin | last post by:
I am trying to put together a small project where I will learn how to embed win controls in apsx pages. So far I got some code. When I run an aspx page nothing is shown on the page. Here is a class...
2
by: Robert Bravery | last post by:
Hi all, Being new to C# and .net I often don't know how to use things. I have created an app that imports excel data, it works well, with methods to open excel, extract the data and close excel....
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: 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?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.