gpt4 book ai didi

C#属性继承问题

转载 作者:太空宇宙 更新时间:2023-11-03 14:30:17 25 4
gpt4 key购买 nike

我如何在 C# 中从接口(interface)继承属性并在类中为该属性指定其他名称?

例如:

public interface IFoo
{
int Num {get;set;}
}

public class IFooCls : IFoo
{
int Ifoo.Num{get;set}
}

在这种情况下,接口(interface)中的属性名是什么,在类中也是一样的。我想要的是在类上提供其他属性名称,但在这种情况下仍指向界面中的“Num”。在 VB 中,我们可以这样做:

Public ReadOnly Property UserId() As String Implements System.Security.Principal.IIdentity.Name
Get
Return _userId
End Get
End Property

最佳答案

这是一种模拟您在 C# 中尝试执行的操作的方法

public interface Foo {
string Name {get;set;}
}

pubilc class Bar : Foo {

#region Foo implementation
public string Name {get{return UserName;} set{UserName = value;}}
#endregion //Foo implementation

public string UserName {get; set;}
}

关于C#属性继承问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2816363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com