Hello .NET, D Here Calling
bearophile
bearophileHUGS at lycos.com
Tue Dec 23 03:38:46 PST 2008
Chad J Wrote:
> This is exactly where I'm coming from. I used to use C# properties a
> lot. They are super effective.
In C# you can use for example:
class TimePeriod {
private double seconds;
public double Hours {
get { return seconds / 3600; }
set { seconds = value * 3600; }
}
}
Or just:
public double TotalPurchases { get; set; }
Some people have proposed:
public int property Myval {
get;
set {
if (value > 10)
throw new Exception();
else
Myval = value;
}
}
Time ago I have written this for D1, I don't know if it can be useful:
import std.metastrings: Format;
template AttributeGetSet(Type, string name) {
const AttributeGetSet = Format!("
private %s %s__;
public %s %s() { return this.%s__; }
public void %s(int %s__local) { this.%s__ = %s__local; }
", Type.stringof, name, Type.stringof, name, name, name, name, name, name);
}
C# also has indexers:
>Indexers allow instances of a class or struct to be indexed just like arrays. Indexers resemble properties except that their accessors take parameters.<
http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx
Usage example:
class SampleCollection<T> {
private T[] arr = new T[100];
public T this[int i] {
get {
return arr[i];
}
set {
arr[i] = value;
}
}
}
But to me that looks a lot like the opIndex/opIndexAssign/opIndexLvalue of D.
Bye,
bearophile
More information about the Digitalmars-d
mailing list