吸气财产参数 [英] Getter property with arguments
问题描述
我想我以前在什么地方见过,但现在我不记得没有找到它,有没有一种方法,使带有参数的吸气财产?
I guess I've seen somewhere before, but now I can't remember neither find it, is there a way to make a getter property with arguments?
我的意思是,我可以转换成浮动的getSize(); 浮动大小
I mean, as I can convert "float getSize();" to "float Size"
float getSize() {
return this.size;
}
float Size {
get { return this.size; }
}
然后,可能我转换,例如漂浮的getSize(字符串单元) ; 浮动尺寸(字符串单位)或者类似的东西?
Then, could I convert for example, "float getSize(String unit);" to "float Size(String unit)" or something like that?
float getSize(String unit) {
return this.size;
}
float Size(String unit) {
get {
if (unit == Unit.Meters)
return this.size/100;
else
return this.size;
}
}
我觉得有使用功能都没有真正的问题,但可能会更好看是这样的:p
I think there's no really problem of using function at all, but may look better this way :P
推荐答案
要回答这个问题:不,这是不可能的,并且已经指出。出来,用一个参数一个getter看起来就像一个方法
To answer the question: No, it is not possible, and as already pointed out, a getter with a parameter would look just like a method.
你正在考虑的事情可能是一个索引默认属性,它看起来像这样:
The thing you are thinking about might be an indexed default property, which looks like this:
class Test
{
public string this[int index]
{
get { return index.ToString(); }
}
}
这允许你索引测试实例,像这样的:
This allows you to index into an instance of Test, like this:
Test t = new Test();
string value = t[1];
这篇关于吸气财产参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!