471,337 Members | 862 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Interfaces in C#

Hi
I am very confused the way C# deals with interfaces in contrast to Java!
One example: There exists the ICloneable Interface which is for example
implemented in the SQLParameter class.
But why does the only method of this interface not appear in the
SQLParameter class? That does not make any sense to me!?!
Regards
Stephan
Jan 24 '06 #1
13 1471
You cannot see this method because it is private.
--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

"Stephan Zaubzer" <st*************@schendl.at> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi
I am very confused the way C# deals with interfaces in contrast to Java!
One example: There exists the ICloneable Interface which is for example
implemented in the SQLParameter class.
But why does the only method of this interface not appear in the
SQLParameter class? That does not make any sense to me!?!
Regards
Stephan

Jan 24 '06 #2
It implements the Clone() method from ICloneable but it's only
available when the class is cast to ICloneable, e.g.

System.Data.SqlClient.SqlParameter p = new
System.Data.SqlClient.SqlParameter();
ICloneable cloneable = (ICloneable) p;

You can enforce this in C# but prefixing the interface name, such as

public object ICloneable.Clone()
{
return new Object();
}

SqlParameter does return a SqlParamter from its Clone() method, I'm not
sure why Microsoft have chosen to do this though.

Jan 24 '06 #3
It's not private, you can get at it using the above.

Jan 24 '06 #4
If only there was editing in newsgroups! yes it is private, but once
again you can access it by casting it.

Jan 24 '06 #5
Search for the term "explicit interface implementation"... that's what
you're looking at here.
--
http://www.kynosarges.de
Jan 24 '06 #6
I do not see the sense in Implementing an interface, when implementing
all of it's members private...
Strange...
If I call this method by first casting the object to ICloneable, will
the copy be a deep or shallow copy?
Regards
Stephan
Chris S. wrote:
If only there was editing in newsgroups! yes it is private, but once
again you can access it by casting it.

Jan 24 '06 #7
Stephan Zaubzer wrote:
I do not see the sense in Implementing an interface, when implementing
all of it's members private...
It's not really making them private. The access isn't really private or
public - it's just context-dependent.
Strange...
If I call this method by first casting the object to ICloneable, will
the copy be a deep or shallow copy?


It won't be a copy at all. It'll be a different "view" on the same
object.

Jon

Jan 24 '06 #8
Jon Skeet [C# MVP] wrote:
Stephan Zaubzer wrote:
Strange...
If I call this method by first casting the object to ICloneable, will
the copy be a deep or shallow copy?


It won't be a copy at all. It'll be a different "view" on the same
object.


Sorry, just to clarify - the ICloneable you call the method on won't be
a copy. What the Clone method chooses to do is its own business, of
course.

Jon

Jan 24 '06 #9
Hi,

Take a look at a thread from yesterday with subject "protected interface
methods"

The methods of an interface ARE public, they cannot be something else

The reason is that SqlParameter implement it explicitely in the code it
looks like:
public object ICloneable.Clone () { .....}

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Vadym Stetsyak" <va*****@ukr.net> wrote in message
news:eD**************@TK2MSFTNGP14.phx.gbl...
You cannot see this method because it is private.
--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

"Stephan Zaubzer" <st*************@schendl.at> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi
I am very confused the way C# deals with interfaces in contrast to Java!
One example: There exists the ICloneable Interface which is for example
implemented in the SQLParameter class.
But why does the only method of this interface not appear in the
SQLParameter class? That does not make any sense to me!?!
Regards
Stephan


Jan 24 '06 #10
Hi,

It's not private , all the members of an interface MUST be public.

It's just that the "default" interface of the class does not allow access to
it, you have to cast it to ICloneable for access it.

Why doing this you may ask?

Well there are several reasons, the most obvious one, what if two interfaces
implement the same method with the same signature? how do you implement both
methods?
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Chris S." <sl********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
If only there was editing in newsgroups! yes it is private, but once
again you can access it by casting it.

Jan 24 '06 #11
Hi,

"Stephan Zaubzer" <st*************@schendl.at> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I do not see the sense in Implementing an interface, when implementing all
of it's members private...
Strange...
Not at all, it makes a lot of sense doing it that way, see my other post
about it.
If I call this method by first casting the object to ICloneable, will the
copy be a deep or shallow copy?


It does depend of the Clone() implementation, the interface assure nothing.
Unfortunately the MSDN doc does not provide any info neither in this
particular case.

OP: remember that Clone() return object , so you must cast it to
SqlParameter

Question:
Does 2.0 provide a ICloneable<T> ?

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 24 '06 #12
I doubt that it is declared as public... it is private.
However, you can access this method when you will perform casting...
The reason is that SqlParameter implement it explicitely in the code it
looks like:
public object ICloneable.Clone () { .....}
Yes, SqlParameter implement IClonable explicitly, but with private
object ICloneable.Clone () { .....}

Just for illustration.

interface IFace
{
void method();
}

class TestIface : IFace
{
//void method() {} //error CS0536: 'TestApp.TestIface' does not
//implement interface member
'TestApp.IFace.method()'....
void IFace.method() {} //explicit
}

public class Test
{
public void test()
{
TestIface test = new TestIface();
test.method(); //error CS0117: 'TestApp.TestIface' does not
contain a definition for 'method'
IFace iface = (IFace)test;
iface.method(); //success
}
}

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:u0**************@TK2MSFTNGP10.phx.gbl... Hi,

Take a look at a thread from yesterday with subject "protected interface
methods"

The methods of an interface ARE public, they cannot be something else

The reason is that SqlParameter implement it explicitely in the code it
looks like:
public object ICloneable.Clone () { .....}

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Jan 24 '06 #13
Hi,
"Vadym Stetsyak" <va*****@ukr.net> wrote in message
news:OK***************@TK2MSFTNGP09.phx.gbl...
I doubt that it is declared as public... it is private.
However, you can access this method when you will perform casting...


The fact that you do not have to put public in front of it, does not make
it private. Prove of this is if you try to mark it as private explicitely
you will get an error, the same thing will happens with protected and
private. As you are EXPLICITELY saying that it does implement a method of an
interface you CANNOT put an access modifier in front of it, as it's public ,
and cannot be something else !
An interesting twist is that you can combine an interface implementation
with explicit and implicit implementation:

public class Class1: IAaa
{
public void method1() { } //note you have to mark it as public, as this
is implicitely declared !
void IAaa.method2() { } // no need to mark access, IT'S PUBLIC
}

static void Main()
{
Class1 c1 =new Class1();
IAaa a = c1;
a.method1();
a.method2();
c1.method1();
c1.method2(); //error, method2 is implemente explicitely, can not be
access from Class1's interface
}

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 24 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by baylor | last post: by
30 posts views Thread by Frank Rizzo | last post: by
8 posts views Thread by John | last post: by
9 posts views Thread by Sean Kirkpatrick | last post: by
22 posts views Thread by RSH | last post: by
18 posts views Thread by Tony | last post: by
10 posts views Thread by hyperboreean | last post: by
23 posts views Thread by A.Gallus | last post: by
reply views Thread by rosydwin | last post: by

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.