473,581 Members | 2,819 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Confused about interfaces.....

Yello,

Quick Q:
If I have a few objects and cast them(?) into an array of a piticular
interface,
would I have boxed the objects? Thus I wouldnt continuously re-box
that type, inturn saving processing time?

eg:
private bird bird1; //Has an IAnimal interface, (no, it does not inherit
animal obj)
private cat cat1; //""
private dog dog1; //""

private IAnimal[] myPets; //(<--will that even work?, I know that obj will
work,
// but I dont want to un-box, and
re-box continuously.)

void main()
{

bird1 = new bird();
cat1 = new cat();
dog1 = new dog();

myPets = new IAnimal[3];
myPets[0] = bird1;
myPets[1] = cat1;
myPets[2] = dog1;

for(int x =0; x < 3; x++)
myPets[x].dispose(true);
}

//(Perhaps disposing my pets might not have been the best choice.
// No, realy, I do like my pets, and dont plan to dispose of them.)
Jan 19 '06 #1
3 1015
TheMadHatter <Th**********@d iscussions.micr osoft.com> wrote:
Quick Q:
If I have a few objects and cast them(?) into an array of a piticular
interface,
would I have boxed the objects? Thus I wouldnt continuously re-box
that type, inturn saving processing time?


Boxing only occurs if you're dealing with value types. You haven't
shown the declarations of cat, bird or dog, so it's a bit hard to say
what's happening. However, you're right that if they *are* value types,
you'll only get the boxing once, when you put them in the array, and
they won't be unboxed unless you cast them back to their original
types.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 19 '06 #2
Hum... what an intresting answer...

Say, if I deserialize an obj from a (eg) filestream, then cast,
are you saying that there isnt any real casting happening?,
thus there isnt any cost in preformance in doing the cast?

As for my previous example, I envisioned a heap obj, not
a value type.

Thanks for the previous answer.
"Jon Skeet [C# MVP]" wrote:
TheMadHatter <Th**********@d iscussions.micr osoft.com> wrote:
Quick Q:
If I have a few objects and cast them(?) into an array of a piticular
interface,
would I have boxed the objects? Thus I wouldnt continuously re-box
that type, inturn saving processing time?


Boxing only occurs if you're dealing with value types. You haven't
shown the declarations of cat, bird or dog, so it's a bit hard to say
what's happening. However, you're right that if they *are* value types,
you'll only get the boxing once, when you put them in the array, and
they won't be unboxed unless you cast them back to their original
types.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jan 19 '06 #3
TheMadHatter wrote:
Hum... what an intresting answer...

Say, if I deserialize an obj from a (eg) filestream, then cast,
are you saying that there isnt any real casting happening?,
thus there isnt any cost in preformance in doing the cast?

As for my previous example, I envisioned a heap obj, not
a value type.


If the type involved is a reference type, and if there isn't any
user-defined conversion occurring, then the cast is really nothing more
than a check. Now, I would include that as "real casting" but that's a
matter of communication rather than behaviour.

There *is* a performance cost in doing a cast - the runtime has to
check that the type is an appropriate one. However, that cost is pretty
small.

Jon

Jan 19 '06 #4

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

Similar topics

1
2194
by: baylor | last post by:
In C#, an interface cannot mark any method as static. i'm told the ILASM supports it but i've never tested that Two questions. First, why? OK, i've heard the reason about interfaces being allocated on the stack but that's not a real reason. Which is OK because this isn't a real question, it's just a complaint dressed up to look like a reason...
8
1349
by: Chris | last post by:
Hi, I was just asigned a project and was doing some research and am now confused with Enterprise Service, Remoting and Web Services. Now I am not sure which will be benificial to my project. I need to create and expose a component to process credit card payment and log the details to a database. The component will be used by an asp.net app...
30
2493
by: Frank Rizzo | last post by:
We are having one of those religious debates at work: Interfaces vs Classes. My take is that Classes give you more flexibility. You can enforce a contract on the descendant classes by marking methods abstract (which is all that an interface does). In addition, the classes allow you to be flexible by adding functionality that child classes...
3
1105
by: Tim Wallace | last post by:
OK, I'm a bit confused about what .NET 2.0 includes. I was adding a reference to my project, and lo and behold, I saw Microsoft.Vsa in the list of .NET components available. I added it. Then, I checked it out in the Object Browser. The version is the same as for System. What gives? Has Microsoft.Vsa been dropped or what. I hope not, but...
8
1770
by: John | last post by:
What is the purpose / benefit of using an interface statement? It doesn't seem like anything more than a different way to make a class... (except you can't define any procedures in an interface like you can do in a class). I'm obviously missing something... John
11
2483
by: herpers | last post by:
Hello, I probably don't see the obvious, but maybe you can help me out of this mess. The following is my problem: I created two classes NormDistribution and DiscDistribution. Both classes provide an implemation of the operator +. Now I want to write another generic class Plan<DType>, which can
18
1970
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 classes that each
22
2086
by: RSH | last post by:
Hi, I have been reading on interfaces working on samples I've run across on the web. For the life of me I cannot seem to grasp them. It appears to me that interfaces are simply blueprints to a class, that when implemented, they require the implementing calss to make sure that each of the properties, functions etc. are handled by the...
7
3080
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see item 7 for why this is virtual ...
5
3005
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I am actually a VB.Net guy, but I have worked somewhat with C++ and C#. I just want to ask about the relationship between Abstract Classes and Interfaces. My first question is if C# even has Iinterfaces. I took some Java programming classes and Interfaces are a main staple of Java. And in VB.Net I use interfaces for setting...
0
7857
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8137
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8299
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8163
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5667
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5355
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3799
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2297
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1397
muto222
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.