473,386 Members | 1,705 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,386 software developers and data experts.

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 1003
TheMadHatter <Th**********@discussions.microsoft.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.com>
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**********@discussions.microsoft.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.com>
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
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...
8
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...
30
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...
3
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...
8
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...
11
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...
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...
22
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...
7
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...
5
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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
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
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
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...

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.