ich möchte ägnlich, wie es in c++ möglich war, eine property so implementieren, das der zurückgegebene wert schreibgeschützt ist. ziel ist es eine Objektreferenz nur zu Lesezwecken zurückzugeben.
ist das in c# möglich?
beispiel (c++)
Entsprechender C# Code
ist das in c# möglich?
beispiel (c++)
Code:
class MyClass
{
private:
MyProperty* property;
public:
MyClass()
{
property = new MyProperty();
}
// Über die zurückgegebene Referenz kann die property nicht verändert werden
const MyProperty& GetProperty()
{
return this->property;
}
};
Entsprechender C# Code
Code:
public class MyClass
{
public MyProperty property;
public MyClass()
{
this.property = new MyProperty();
}
// Die property kann über den Rückgabewert geändert werden
public MyProperty Property
{
get
{
return this.property;
}
}
}