Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实现接口,但具有不同的属性名称_C#_Vb.net_Code Translation - Fatal编程技术网

C# 实现接口,但具有不同的属性名称

C# 实现接口,但具有不同的属性名称,c#,vb.net,code-translation,C#,Vb.net,Code Translation,我认为这很容易,但我在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(O

我认为这很容易,但我在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
因此,我可以在接口
Items
属性和类
AnyName
属性中使用不同的名称。因此,如果我试图将这段代码翻译成C#,它应该是这样的:

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;
        }
    }
}
public void Main()
{
IMyInterface foo=new Cander();
foo.Items.Add(“Hello”);
Debug.WriteLine(foo.Items.First());
}
//在此处定义其他方法和类
接口接口
{
列出项目{get;}
}
公共类Cander:IMyInterface
{
私人名单(任何姓名);;
public List AnyName//我不知道如何翻译IMyInterface(字符串的)。Items
{
得到
{
如果(_anyName==null)
_anyName=新列表();
返回任意名称;
}
}
}
我不知道translate
如何实现IMyInterface(字符串的)。Items
code。这是一个基本问题,但我搜索了文档和其他答案,但找不到任何解决方案。也许它可以用 但我找不到类似的解决方案


在C#中可能吗?

是的,这是C#和VB.NET之间的区别之一,是的,您可以使用显式接口实现:

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;
}
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,
    }
}