Search code examples
c#vb.netcode-translation

Implement interface but having different property names


I though it would be easy, but I have a code like this one in VB.Net:

Sub Main
    
    Dim foo As IMyInterface(Of String) = New Cander()
    foo.Items.Add("Hello")
    Debug.WriteLine(foo.Items.First())
End Sub

Interface IMyInterface(Of Out T)
    ReadOnly Property Items As List(Of String)
End Interface

Public Class Cander
    Implements IMyInterface(Of String)

    Private _anyName As List(Of String)

    Public ReadOnly Property AnyName As List(Of String) Implements IMyInterface(Of String).Items
        Get
            If _anyName Is Nothing Then
                _anyName = New List(Of String)
            End If
            
            Return _anyName
        End Get
    End Property
    
End Class

So I can have a different name in the interface Items property and in the class AnyName property. So if I try to translate this code to C# it should be something like this:

public void Main()
{
    IMyInterface<string> foo = new Cander();
    foo.Items.Add("Hello");
    Debug.WriteLine(foo.Items.First());
}

// Define other methods and classes here
interface IMyInterface<out T>
{
    List<string> Items { get; }
}

public class Cander : IMyInterface<string>
{
    private List<string> _anyName;

    public List<string> AnyName //I don't know how to translate Implements IMyInterface(Of String).Items
    {
        get
        {
            if (_anyName == null)
                _anyName = new List<string>();

            return _anyName;
        }
    }
}

I don't know how translate Implements IMyInterface(Of String).Items code. Is a basic question but I searched documentations and another answers but I can't find any solution. Maybe it could be using Explicit Interface Implementation but I can't find a similar solution.

Is it possible in C#?


Solution

  • Yes, this is one of the differences between C# and VB.NET, and yes you have an use an explicit interface implementation:

    public class Cander : IMyInterface<string>
    {
        private List<string> _anyName;
    
        public List<string> AnyName
        {
            get
            {
                if (_anyName == null)
                    _anyName = new List<string>();
    
                return _anyName;
            }
        }
        List<string> IMyInterface<string>.Items => AnyName;
    }
    

    Note that this isn't exactly equivalent to the VB.NET version: the AnyName member doesn't implement IMyInterface<string>.Items in any way: what we're doing is defining a new property which does implement IMyInterface<string>.Items but which doesn't appear as a member of Cander, and its getter calls AnyName.

    If Items had both a getter and a setter, you'd have to write the slightly more roundabout:

    interface IMyInterface<out T>
    {
        List<string> Items { get; set; }
    }
    
    public class Cander : IMyInterface<string>
    {
        private List<string> _anyName;
    
        public List<string> AnyName //I don't know how to translate Implements IMyInterface(Of String).Items
        {
            get
            {
                if (_anyName == null)
                    _anyName = new List<string>();
    
                return _anyName;
            }
            set => _anyName = value;
        }
    
        List<string> IMyInterface<string>.Items
        {
            get => AnyName,
            set => AnyName = value,
        }
    }