Function like C# properties?
- by alan2here
I was directed here from SO as a better stack exchange site for this question.
I've been thinking about the neatness and expression of C# properties over functions, although they only currently work where no parameters are used, and wondered. Is is possible, and if so why not, to have a stand alone function like C# property.
For example:
public class test
{
private byte n = 4;
public test()
{
func = 2;
byte n2 = func;
func;
}
private byte func
{
get
{
return n;
}
set
{
n = value;
}
func
{
n++;
}
}
}
edit:
Sorry for the vagueness first time round. I'm going to add some info and motivation.
The 'n++' here is just a simple example, a placeholder, it's not intended to be representative of the actual code that would be used.
I'm also looking at this from the point of view of looking at the property command as is, not in the context of using it for 'get_xyz' and 'set_xyz' member functions, which is certainly useful, but of instead comparing it more abstractly to functions and other programic elements.
A 'get' property can be used instead of a function that takes no parameters, and syntactically they are perhaps only aesthetically, but as I see it noticeably nicer.
However, properties also add the potential for an extra layer of polymorphism, one that relates to the 'func = 4;' getting, 'int n = func;' setting or 'func;' function like context in which they are used as well as the more common parameter based polymorphism. Potentially allowing for a lot of expression and contextual information reguarding how other would use your functions.
As in many places uses and definitions would remain the same, it shouldn't break existing code.
private byte func
{
get
{
}
get bool
{
}
set
{
}
func
{
}
func(bool)
{
}
func(byte, myType)
{
}
// etc...
}
So a read only function would look like this:
private byte func
{
get
{
}
}
A normal function like this:
private void func
{
func
{
}
}
A function with parameter polymorphism like this:
private byte func
{
func(bool)
{
}
func(byte, myType)
{
}
}
And a function that could return a value, or just compute, depending on the context it is used, that also has more conventional parameter polymorphism as well, like so:
private byte func
{
get
{
}
func(bool)
{
}
func(byte, myType)
{
}
}