Hi Ashok,
in C# if you use Explicit Interface implementation, then you cannot mark
*any* accessibility on the method. As I said you cannot call the method
from even inside your won class without casting to the interface. SO that
is in essence private, or even more so, yet it is exposed via the interface
because an interface is a public contract.
To quote from the ECMA specification for the CLR:
Partition I , Section 8.9.4
"However, since accessibility attributes are relative to the implementing
type rather than the interface itself, all members of an interface shall
have public accessibility, and no security permissions can be attached to
members or to the interface itself."
Here's an example in C#:
interface IFoo
{
void Bar();
}
class Baz : IFoo
{
void test()
{
//can't call IFoo.Bar, need to cast to IFoo
(this as IFoo).Bar();
}
void IFoo.Bar() //Can be called from anywhere the class is cast to IFoo
{
Console.WriteLine("IFoo.Bar");
}
}
The IFoo.Bar method can be called anywhere you have a reference to the
object. This is exactly the same as in VB.
In VB however you can have multiple use of the same method, and that's when
the accessibility comes into play.
In either VB or C#, if you implement an interface you are agreeing to the
contract that the methods will be accessible from anywhere a reference to
the containing object is.
Post by AshokGBill,
Thanks on responding,
C# even if a class implements it explicitly, it will not get compile if it
is declared it as private. C# enforces the implementing class to declare
it as Public - VB doesn't!.
I think VB should also do this to eliminate the confusion.
Regards,
Ashok
Post by Bill McCarthyHi Ashok,
An interface is a contract. IF you implement an interface you guarantee
to expose the methods that interface defines.. that is the contract. C#
also has private interface implementation via explicit interface
implementation, in which case you can't even call that method from inside
the class, you must first cast the "this" (Me in Vb) reference to the
interface. That is, in the strictest form of the contract, the members
are always accessible but you may need to cast to the interface first.
http://msmvps.com/blogs/bill/archive/2005/02/27/37105.aspx
Bill.
Post by AshokGGöran Andersson,
Thanks for the reply. Even if you rename the method in the
implementation it get's called.
My point here is allowing to call a private method through the
Interface. If you delcare with class name then you can't call.
So, Private members should not be allowed to call from outside the class
and similarly it should not allow Interface implemented member to
declared as Private
Regards,
Ashok
Post by Göran AnderssonPost by AshokGHi,
Consider this code in VB.NET project ClassLibrary1...
Public Interface ITest
Function Name() As String
Function Age() As Integer
End Interface
Public Class Test
Implements ITest
Private Function Age() As Integer Implements ITest.Age
Return 10
End Function
Public Function Name() As String Implements ITest.Name
Return "Test"
End Function
End Class
And a new C# (VB.NET too) Console Project and referefer above ClassLibrary1
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
ClassLibrary1.ITest t = new ClassLibrary1.Test();
Console.WriteLine(t.Age());
}
}
Note that Age is private in VB is getting accessing from C#/VB.NET
through interface... -Huh
C# doen't allow this as it won't compile!
Regards
Ashok
You are confusing the Age method with the Age method. :)
There are actually two methods Age in the class. The method Age in the
Test class is private. The method Age in the ITest interface is public.
They just happen to be the same method and happen to have the same name.
When the method has a different name than the method that it
Private Function Answer() As Integer Implements IQuestion.Reply
Return 42
End Function
The method Answer is private, but the method Reply is public.
In C# it's a bit stricter, and thus less confusing. The methods that
implement the interface has to have the same names as in the interface,
and they have to be public.
--
Göran Andersson
_____
http://www.guffa.com