By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,567 Members | 1,609 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,567 IT Pros & Developers. It's quick & easy.

structs implementing interfaces, (un)boxing and performance

P: n/a
Hi,

I have a struct that implements an interface:

interface IMyInterface
{
void DoIt();
}
public struct MyStruct : IMyInterface
{
void IMyInterface.DoIt(){}
}

and somehwre else I have a method, that takes the interface :

public void UseIt(IMyInterface myi){}

As my implementation of the interface does not need any data, I decided
to make it a struct, as this should be more efficient.

But what is happening now when I pass a struct into the function that
takes the interface:

MyStruct s = new MyStruct();
UseIt(s);

Will the struct be "boxed" before it is passed or is there another
mechanism in place?

Thanks in advance,
Andy
--
To mail me directly, remove the NO*SPAM parts in
NO***********@gmx.netNO*SPAM
Nov 15 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Andreas Mueller wrote:
Hi,

I have a struct that implements an interface:

interface IMyInterface
{
void DoIt();
}
public struct MyStruct : IMyInterface
{
void IMyInterface.DoIt(){}
}

and somehwre else I have a method, that takes the interface :

public void UseIt(IMyInterface myi){}

As my implementation of the interface does not need any data, I
decided to make it a struct, as this should be more efficient.

But what is happening now when I pass a struct into the function that
takes the interface:

MyStruct s = new MyStruct();
UseIt(s);

Will the struct be "boxed" before it is passed or is there another
mechanism in place?


Yes the struct will definitely be boxed, there's no way around that. When
you create types that are often accessed through interfaces, you're probably
better off with a class. However, this hugely depends on how exactly you'll
be using the type...

HTH,

Andreas

Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.