473,407 Members | 2,315 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.

Interfaces and ref parameters

JR
Hi,

What are the rules regarding ref parameters and interface types?

The reason I ask is that I found myself writing code like this several times:

if (something != null)
{
something.Dispose();
something = null;
}

So I had the "clever" idea of writing a routine like this:

public static void MyDispose(IDisposable disposable)
{
if (disposable != null)
{
disposable.Dispose();
disposable = null;
}
}

I can pass variables of type IDisposable to this routine, but I cannot pass class types that implement IDisposable. E.g. I wanted to do something like:

Bitmap MyBitmap = ....
.....
MyDispose(MyBitmap);

It seems to me that the compiler should be able to figure out what I want (i.e. pass my bitmap to the method, just like it does if its not a ref parameter) then set MyBitmap to null. The compiler disagrees with this plan, and gives an error.

Is there any way to ahieve what I want?

JR

Nov 15 '05 #1
2 3460
JR <an*******@discussions.microsoft.com> wrote:
What are the rules regarding ref parameters and interface types?

The reason I ask is that I found myself writing code like this several times:

if (something != null)
{
something.Dispose();
something = null;
}

So I had the "clever" idea of writing a routine like this:

public static void MyDispose(IDisposable disposable)
{
if (disposable != null)
{
disposable.Dispose();
disposable = null;
As it stands, this line is pointless.
}
}

I can pass variables of type IDisposable to this routine, but I cannot pass class
types that implement IDisposable. E.g. I wanted to do something like:

Bitmap MyBitmap = ....
...
MyDispose(MyBitmap);
You certainly should be able to do that.
It seems to me that the compiler should be able to figure out what I
want (i.e. pass my bitmap to the method, just like it does if its not
a ref parameter) then set MyBitmap to null. The compiler disagrees
with this plan, and gives an error.
Hang on - you *don't* have a ref parameter at the moment...
Is there any way to ahieve what I want?


Assuming that you meant:

public static void MyDispose(ref IDisposable disposable)
{
if (disposable != null)
{
disposable.Dispose();
disposable = null;
}
}

there's a very good reason why you can't do what you want - the
compiler doesn't know that your method doesn't look like this:

public static void MyDispose(ref IDisposable disposable)
{
disposable = new MemoryStream();
}

Your MyBitmap variable would then contain a reference to a
MemoryStream, which is definitely not a good thing!

You *could* adopt a pattern of:

MyBitmap = MyDispose(MyBitmap);

and make MyDispose always return null. However, to be honest, I would
personally just stick with the "using" construct for almost all uses of
IDisposable. I rarely find I need anything else, and it makes it *so*
much easier to get right.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
JR


----- Jon Skeet [C# MVP] wrote: ----

public static void MyDispose(ref IDisposable disposable

if (disposable != null

disposable.Dispose()
disposable = null;

there's a very good reason why you can't do what you want - the
compiler doesn't know that your method doesn't look like this

public static void MyDispose(ref IDisposable disposable

disposable = new MemoryStream()
True! Thanks for the explaination

JR
Nov 15 '05 #3

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

Similar topics

21
by: Franco Gustavo | last post by:
Hi, Please help me to understand this, because I don't see what I'm missing. I was reading a lot of examples on Internet that explain that C# doesn't implement multiple inheritance it...
6
by: rakefet | last post by:
Hi. I'm really new to this world of .Net so your help would be most appreciated... We have API and COM interfaces developed in c. We would also like to supply a .net interface to our clients....
19
by: Duncan McNutt .[FTSE] | last post by:
Why does code derive from for example, IComparer ie, class SomeClass : IComparer { public SomeClass() { }
4
by: don | last post by:
I have two existing interfaces having methods with same names. Now I have to implement both intrfaces in one class. Is there any way I could implement methods with same names in both interfaces...
12
by: Achim Domma (Procoders) | last post by:
Hi, I've seen that there are implementations of Mock objects, which implement interfaces on-the-fly during execution. Could somebody point me to the direction on how to do that in .Net? I...
7
by: Ant | last post by:
Hi, I’m wondering what practical use is there for creating interfaces. I can fully appreciate creating an abstract class for the purpose of inheriting, but why an interface? Is it just to...
18
by: _dee | last post by:
Question about best use of interfaces: Say there's a 'Master' class that needs to implement a few interfaces: class Master : I1, I2, I3 { } The actual code already exists in smaller...
7
by: pietro.cerutti | last post by:
Hi guys, I have a daemon running on Debian and listening for multicast packets sent to 224.0.0.251:5353 (ZeroConf Multicast DNS queries). The server is plugged into a VLAN trunk with eth0 and...
2
by: Tobias Erbsland | last post by:
Hello I'm not really sure if this should be possible according to the standard, because didn't found anything in the FAQ and in the C++ book about that. ==== example === class A { public:
6
by: S_K | last post by:
Hi, I've been toying around with interfaces in C#. They are fun but can anybody give me some examples of where interfaces are used and what they are used for? Thanks so much. Steve
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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:
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.