Interface Behavior in VB.Net dfferent

The interface behaves differently in Vb.Net. The following is an example code snippet where IStudent has a SayHello method, which is implemented by the Student class. The access modifier for SayHello must be Public by default. Changing the access modifier to Private does not violate the existing code, and I can still access this private method using the code below

 Dim stdnt As IStudent = New Student stdnt.SayHello() 

An access modifier defines the scope of members in a class; more than private members are only available from a class that exists. But here the access modifier theory, encapsulation is violated.

  • Why is .net designed this way?
  • Is the concept of access modifier and encapsulation really broken?
  • How does the .net infrastructure handle this situation internally?

Thank you in advance

 Module Module1 Sub Main() Dim stdnt As IStudent = New Student stdnt.Name = "vimal" stdnt.SayHello() End Sub End Module Public Interface IStudent Property Name As String Sub SayHello() End Interface Public Class Student Implements IStudent Private Property Name As String Implements IStudent.Name Private Sub SayHello() Implements IStudent.SayHello Console.WriteLine("Say Hello!") End Sub End Class 
+6
source share
5 answers

An original poster presented this question to me via TheBugGuys@Coverity.com ; my answer is here:

https://communities.coverity.com/blogs/development-testing-blog/2013/10/09/oct-9-posting-interface-behaves-differently-in-visual-basic

Summarize:

Why was .NET designed this way?

This question is not clear.

Is encapsulation broken explicit implementation in C # and VB?

No. Method confidentiality limits the availability of the method name, not to those who can invoke the method. If the author of the class chooses a private method called by some other mechanism, besides searching by name, this is their choice. A third party cannot make a choice for them, except by using methods such as private reflection that violate encapsulation.

How is this feature implemented in .NET?

There is a special metadata table for explicit interface implementations. The CLR advises first when trying to figure out which class (or structure) method is associated with which interface.

+4
source

From MSDN :

You can use a private member to implement a member of an interface. When a private member implements an interface element, that member becomes accessible through the interface, although it is not available directly to object variables for the class.

In C #, this behavior is achieved by implementing the interface explicitly, for example:

 public interface IStudent { string Name { get; set; } void SayHello(); } public class Student : IStudent { string IStudent.Name { get; set; } void IStudent.SayHello() { Console.WriteLine("Say Hello!"); } } 

So, if you must omit IStudent. in front of method names, it will break. I see that the interface name is included in the VB syntax. I do not know if this makes any difference. But the members of the interface are not private, since there is no interface. They are kindly public ...

+4
source

There is no fundamental difference between C # and VB.NET, they just chose different ways to solve the ambiguity. Best to demonstrate a C # snippet:

 interface ICowboy { void Draw(); } interface IPainter { void Draw(); } class CowboyPainter : ICowboy, IPainter { void ICowboy.Draw() { useGun(); } void IPainter.Draw() { useBrush(); } // etc... } 

VB.NET simply chose a consistent interface implementation syntax, so the programmer does not need to weigh the differences between the implicit and explicit implementation syntax. Just always explicit in VB.NET.

Only the accessibility of an interface method matters. Always in public.

+3
source

When your stdnt variable is declared as IStudent, the methods and properties of the interface then become public, so the implementation of the derived class (Student) is performed. If, on the other hand, if stdnt was declared as Student, private members (Name and SayHello) will not be implemented, and an error will be generated.

I assume that interface element stubs (Name and SayHello) are Public by default, and access modifier definitions of an implementation implementation of a derived class are ignored.

IMHO.

+2
source

The exact equivalent in C # is as follows: a method available for objects of an interface type and a private method available to it:

  void IStudent.SayHello() { this.SayHello(); } private void SayHello() { Console.WriteLine("Say Hello!"); } 
+1
source

Source: https://habr.com/ru/post/954091/


All Articles