473,473 Members | 1,457 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

structs implementing interfaces, (un)boxing and performance

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
1 2449
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: codymanix | last post by:
Last night I had several thought about RAII and want to discuss a bit. Why doesn't CSharp support destructors in structs? Wouldn't that make RAII possible like in C++? When the struct goes out of...
4
by: Alistair Welchman | last post by:
I have a Hashtable of ints keyed on Guids, and I want to do the following: foreach ( DataRow row in dsWorkDays.Tables.Rows ) { Guid PersonId = (Guid)row; DateTime Day = (DateTime)row;
43
by: Mountain Bikn' Guy | last post by:
I have a situation where an app writes data of various types (primitives and objects) into a single dimensional array of objects. (This array eventually becomes a row in a data table, but that's...
10
by: Nathan Neitzke | last post by:
I am writing an app that might largely benefit from using a struct instead of a class. However, it needs to be rich enough where there are methods available. My question is - every time you...
13
by: gmccallum | last post by:
General Info: A struct is stored on the stack and a class on the heap. A struct is a value type while a class is a reference type. Question: What if a struct contains a string...
13
by: bjarne | last post by:
Willy Denoyette wrote; > ... it > was not the intention of StrousTrup to the achieve the level of efficiency > of C when he invented C++, ... Ahmmm. It was my aim to match the performance...
14
by: Jeff S. | last post by:
In a Windows Forms application I plan to have a collection of structs - each of which contains a bunch of properties describing a person (e.g., LastName, FirstName, EmployeeID, HomeAddress,...
161
by: Peter Olcott | last post by:
According to Troelsen in "C# and the .NET Platform" "Boxing can be formally defined as the process of explicitly converting a value type into a corresponding reference type." I think that my...
13
by: atlaste | last post by:
Hi, I'm currently developing an application that uses a lot of computational power, disk access and memory caching (to be more exact: an information retrieval platform). In these kind of...
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...
1
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...
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,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.