I want to overload default get set methods of c#. I could not achieve to write get(int index), and set(int index, int value) by not changing the original contenI want to overload default get set methods of c#. I could not achieve to write get(int index), and set(int index, int value) by not changing the original content. Below code just does not compile.
private int xx[2];
public int Xx
{
get (int index)
{
return xx[index == 1];
}
set (int index)
{
xx[index == 1] = value;
}
}
. Below code just does not compile.
private int xx[2];
public int Xx
{
get (int index)
{
return xx[index == 1];
}
set (int index)
{
xx[index == 1] = value;
}
}
What you refer to is called indexers . In c#, to create indexers, you use the keyword this
, followed by square brackets and then get (and an optional set):
public int this[int index]
{
get { return arr[i]; }
set { arr[i] = value; }
}
In your case it's not really obvious what your code should go, but I think you are looking for something like this:
private int xx[2];
public int this[index]
{
get { return xx[index]; }
set { xx[index] = value; }
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.