C# C语言中带参数的属性#
在VB.Net中,属性可以像在函数中一样接收参数,方法是在属性定义的开头提到所有参数: 我试着用C#来做这件事,但我得到了一个错误:C# C语言中带参数的属性#,c#,properties,C#,Properties,在VB.Net中,属性可以像在函数中一样接收参数,方法是在属性定义的开头提到所有参数: 我试着用C#来做这件事,但我得到了一个错误: public class Geek { private string[] m_address = new string[2]; public string[int x] address // error here { get { if (index >= 0 &&am
public class Geek
{
private string[] m_address = new string[2];
public string[int x] address // error here
{
get
{
if (index >= 0 && index < index.length)
{
return m_address;
}
}
set {
if (index >= 0 && index < index.length)
{
m_address = value;
}
}
}
}
公共类极客
{
私有字符串[]m_地址=新字符串[2];
公共字符串[int x]地址//此处出错
{
得到
{
如果(索引>=0&&index=0&&index
是的,在C#中没有这样的东西。您可以在C#中定义的唯一参数化属性类型是indexer
典型的C#属性只有一个默认参数值
。属性是一组访问器,因此您可以定义这些方法,而不是使用属性:
public class Geek
{
private string[] m_address = new string[2];
public void setAddress(int index)
{
if (index >= 0 && index < index.length)
{
m_address = value;
}
}
public void getAddress(int index)
{
if (index >= 0 && index < index.length)
{
return m_address;
}
}
}
然后,让我们将主类更改为使用Address:
public class Geek
{
public Address Address { get; } = new Address();
}
现在您可以对Address属性使用indexer,如下所示:
var hassn = new Geek();
hassn.Address[0] = "National Street";
public class Geek
{
private string[] m_address = new string[2];
public string this[int index]
{
get {
if (index >= 0 && index < index.length)
{
return m_address;
}
else
{
return null;
}
}
set {
if (index >= 0 && index < index.length)
{
m_address = value;
}
}
}
}
阅读你的问题,我认为你不需要“额外的参数”。您需要的(看起来)是实现索引器。您可以在此处阅读: 因此,您的示例代码如下所示:
var hassn = new Geek();
hassn.Address[0] = "National Street";
public class Geek
{
private string[] m_address = new string[2];
public string this[int index]
{
get {
if (index >= 0 && index < index.length)
{
return m_address;
}
else
{
return null;
}
}
set {
if (index >= 0 && index < index.length)
{
m_address = value;
}
}
}
}
公共类极客
{
私有字符串[]m_地址=新字符串[2];
公共字符串此[int索引]
{
获取{
如果(索引>=0&&index=0&&index
如注释C所示,这是一个更受限制的版本
要定义索引器,请使用此:
public string this[int x] {
get => underlyingCollection[x];
set {
if (valueValue(value) {
underlyingCollection[x] = value;
} else {
throw new ArgumentException(…);
}
}
}
注意,您可以重载index参数(例如,支持
string
和int
索引)。
public class Geek
{
private string[] m_address = new string[2];
public void setAddress(int index, string valueM)
{
if (index >= 0 && index < m_address.Length)
{
m_address[index] = valueM;
}
}
public string getAddress(int index)
{
if (index >= 0 && index < m_address.Length)
{
return m_address[index];
}
else
{
return null;
}
}
}
公共类极客
{
私有字符串[]m_地址=新字符串[2];
public void setAddress(int索引,字符串值m)
{
如果(索引>=0&&index=0&&index
我想知道这是否可以!!或者不是,没关系。属性只不过是两个方法(get&set)的集合,尽管我的答案和它的用法与您的VB.NET代码更为相似,但您对类型的公共成员使用的camel case不是惯用的C#:使用pascal case;get/set方法对也不是:使用属性。@MohamedHassan我已经用indexer更新了我的答案
public class Geek
{
private string[] m_address = new string[2];
public void setAddress(int index, string valueM)
{
if (index >= 0 && index < m_address.Length)
{
m_address[index] = valueM;
}
}
public string getAddress(int index)
{
if (index >= 0 && index < m_address.Length)
{
return m_address[index];
}
else
{
return null;
}
}
}