gpt4 book ai didi

c# - C#中重载[]运算符并给出多个返回值

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

<分区>

Possible Duplicate:
How do I overload the square-bracket operator in C#?

对于一个类,是否可以在 C# 中重载运算符 []?

如果是这样,声明 [] 重载函数的正确语法是什么?

运算符 [] 也可以根据给定的参数返回不同的数据类型吗?如果不是,您认为我的其他解决方案是什么?我应该改用 Object 吗?

public class MyClass {

private Dictionary<string,Element> eAttribs;
private Dictionary<string,string> defAttribs;

// Also can the operator [] returns different data types based off the parameter given?
// If not, what do you think is my other solution?
public Element operator[](string attribKey) {
if (eAttribs.containsKey(attribKey)
return eAttribs[attribKey];
else return null;
}

// COMPILE Error below: Unexpected symbol '['
public string operator[](string attribKey) {
if (defAttribs.containsKey(attribKey)
return defAttribs[attribKey];
else return null;
}
}

30 4 0
文章推荐: css - 与网络安全字体相比,使用自定义字体技术有什么缺点?
文章推荐: jQuery 动态 CSS 加载奇怪的行为
文章推荐: python - 使用 PyDev 配置 Jython、处理和 OpenGL
文章推荐: javascript - 动态
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com