473,499 Members | 1,618 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Interface casting required question

Supposed that a class "MyTestClass" implement interface "IMyInterface" (not
explicitly).

Now suppose that I make an instantiate of "MyTestClass" and then use this
instance to pass it to a function with an argument that requires
"IMyInterface" such as:

private void SomeFunction(IMyInterface xyz) {.}

My question is, will this function require a cast to "IMyInterface"? For
example, if I was to do this following, would this require a cast?

MyTestClass experiment = new MyTestClass();
SomeFunction(experiment); // Cast because argument is type "IMyInterface"???

Or does the compiler knows that the instance of the object implement the
interface and it ends up implementing the code the exact same way as if the
"SomeFunction" would have the argument of type "MyTestClass" instead of
"IMyInterface"?

Thank you.
Apr 10 '06 #1
1 1173
Rene <a@b.c> wrote:
Supposed that a class "MyTestClass" implement interface "IMyInterface" (not
explicitly).

Now suppose that I make an instantiate of "MyTestClass" and then use this
instance to pass it to a function with an argument that requires
"IMyInterface" such as:

private void SomeFunction(IMyInterface xyz) {.}

My question is, will this function require a cast to "IMyInterface"? For
example, if I was to do this following, would this require a cast?

MyTestClass experiment = new MyTestClass();
SomeFunction(experiment); // Cast because argument is type "IMyInterface"???
No.
Or does the compiler knows that the instance of the object implement the
interface and it ends up implementing the code the exact same way as if the
"SomeFunction" would have the argument of type "MyTestClass" instead of
"IMyInterface"?


There's an implicit conversion from a type which implements an
interface to that interface, so the compiler knows it can call that
method.

--
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
Apr 10 '06 #2

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

Similar topics

10
4438
by: The Directive | last post by:
Is there a way in C++ to create a "true" interface. I want to create an absract class that other classes can inherit from and be forced to implement the interface's abstract (pure virtual) methods....
231
22914
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
7
3635
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
0
906
by: AStrugglingDeveloper | last post by:
I'm probably missing something horribly obvious but this is the first time I've used the managed extensions for C++ and the documentation seems to indicate that what I am doing should work. I am...
10
11754
by: Bob | last post by:
This has been bugging me for a while now. GetType isn't availble for variables decalred as interface types, I have to DirectCast(somevariable, Object). In example: Sub SomeSub(ByVal...
2
1604
by: Kevin Frey | last post by:
Consider this simple example: interface IReader { bool Read( ); }; class MyReader : IReader { bool Read( ); // or should it be bool IReader.Read( ) ?
9
1473
by: Sean Chambers | last post by:
Not sure if this is the correct way to go about this, but it seems correct. I'm trying to eliminate dependencies through interfaces and want to minimize casting as much as possible, as a result I...
0
2491
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that...
5
1198
by: Russell Mangel | last post by:
Here is something much closer to what I was trying to do. Thanks to Arne, and Peter and all who helped. Sorry I didn't explain my question better. Russell Mangel Las Vegas, NV // Begin...
0
7132
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
7223
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
6899
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
5475
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
3103
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...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
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 ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.