gpt4 book ai didi

c# - C# 泛型类型中的 "Current Type"占位符?

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

基本上,我想做的是:

public class MySpecialCollection<T>
where T : ISomething { ... }

public interface ISomething
{
public ISomething NextElement { get; }
public ISomething PreviousElement { get; }
}

public class XSomething : ISomething { ... }

MySpecialCollection<XSomething> coll;
XSomething element = coll.GetElementByShoeSize(39);
XSomething nextElement = element.NextElement; // <-- line of interest

... 无需将 nextElement 转换为 XSomething。有任何想法吗?我会想要某种......

public interface ISomething
{
public SameType NextElement { get; }
public SameType PreviousElement { get; }
}

提前致谢!

最佳答案

使界面通用:

public class MySpecialCollection<T> where T : ISomething<T> {
...
}

public interface ISomething<T> {
T NextElement { get; }
T PreviousElement { get; }
}

public class XSomething : ISomething<XSomething> {
...
}

关于c# - C# 泛型类型中的 "Current Type"占位符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3599662/

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