Readonly Objects returned from methods?
Hi
let's assume i have the following function
void Person getPerson(int ID)
{
Person p= ....
return p;
}
Is it possible to get the Person as an readonly (in C++ const) object from this function without cloning the Person-object? In C++ i would return a const object and therefore only can read public members or call other const-methods of this object. How could i do this with C# ??
Thanks for reading!
Re: Readonly Objects returned from methods?
Quote:
Originally Posted by Killersheep2
Hi
let's assume i have the following function
void Person getPerson(int ID)
{
Person p= ....
return p;
}
Is it possible to get the Person as an readonly (in C++ const) object from this function without cloning the Person-object? In C++ i would return a const object and therefore only can read public members or call other const-methods of this object. How could i do this with C# ??
Thanks for reading!
You can possibly use Properties.
private Person p;
public Person P
{
get { return p; }
}
Maybe I am not understanding your questions correctly.
Re: Readonly Objects returned from methods?
C# doesn't have the concept of const. I guess its debatable whether it really exists in C++ either, but no, there's no such functionality provided in C#.
for accessors like that properties are the way C# does it (they're method like things that look like public variables. if no set {} block is defined inside the property, then it is readonly, but that doesn't stop someone from changing the value of the object's members).
Re: Readonly Objects returned from methods?
Quote:
Originally Posted by MadHatter
C# doesn't have the concept of const. I guess its debatable whether it really exists in C++ either, but no, there's no such functionality provided in C#.
for accessors like that properties are the way C# does it (they're method like things that look like public variables. if no set {} block is defined inside the property, then it is readonly, but that doesn't stop someone from changing the value of the object's members).
using const or readonly wouldn't work here?
Re: Readonly Objects returned from methods?
Quote:
Originally Posted by MadHatter
C# doesn't have the concept of const. I guess its debatable whether it really exists in C++ either, but no, there's no such functionality provided in C#.
for accessors like that properties are the way C# does it (they're method like things that look like public variables. if no set {} block is defined inside the property, then it is readonly, but that doesn't stop someone from changing the value of the object's members).
This is exactly the problem! I want to return an object, but dont want the receiver to get writeaccess to the members! If C# doesnt support this, i have to return a copy of my object. Sometimes copying is to slow, because the objects may be large!
Re: Readonly Objects returned from methods?
You can either
(1) Make Person immutable i.e. don't have any public 'set' methods. If you want to change a 'Person' then you create a copy. This is the trick that system.string uses.
(2) Have a read only interface, IPerson, which Person implements and which only has public get methods. Return this from the method. This isn't a complete solution : outside people could still cast to the concrete implementation but it's better than nothing.
(3) Have an interface class which has public get methods and return this e.g.
Code:
public class Person
{
int _numberOfLegs;
public int NumberOfLegs
{
get
{
return _numberOfLegs;
}
set
{
_numberOfLegs = value;
}
}
} ;
public class ReadonlyPerson
{
private Person _person;
public ReadonlyPerson(Person person)
{
_person = person;
}
public int NumberOfLegs
{
get
{
return _person.NumberOfLegs;
}
}
}
public class ContainingPerson
{
private Person _person;
public ReadonlyPerson Person
{
get
{
return new ReadonlyPerson(_person);
}
}
}
Create an instance of ReadonlyPerson to return to the outside world.
Darwen.
Re: Readonly Objects returned from methods?
Quote:
Originally Posted by Killersheep2
This is exactly the problem! I want to return an object, but dont want the receiver to get writeaccess to the members! If C# doesnt support this, i have to return a copy of my object. Sometimes copying is to slow, because the objects may be large!
Code:
public class Person
{
private Person p;
public Person P
{
get { return p; }
}
}
That should do it. It will allow read-only access to it.
Re: Readonly Objects returned from methods?
Quote:
Originally Posted by darwen
...
Thanks! I think about this immutable object. Sounds good to me. The wrapper-class sounds good too. Generic.List implements such a wrapper (.AsReadOnly() )
Re: Readonly Objects returned from methods?
Quote:
Originally Posted by eclipsed4utoo
the C# const keyword is only able to decorate string and value type members of a class. readonly allows a class variable to be accessed but its reference cannot be changed outside of the classes constructor.
in C++ you can decorate method parameters and return values as const, which isn't directly possible here.