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#?