I am creating a class that basically need to perform some operations on files after reading them.
The class skeleton is like this
class filesystem
{ private string fileName;
private FileInfo fileInformation;
private Dictionary<string,string> headers;
}
What is the best way to expose these member variables as properties ? The FileInfo object needs a string(file path) in its constructor
1.If i do
public FileInfo File
{
get { return file; }
}
I want a setter property also for my FileInfo Object. How do i accomplish it?
2. public
Dictionary<string,
object> Headers
{
get { return headers; }
set
{
if (headers ==
null) headers = new
Dictionary<string,
string>();headers =
value;
}
}
Is it right way to code setter property for thsi kind of collection ? If not, what is the right way ? I did this way because I felt simply doing headers=value would create a shallow copy.
None
0 Points
20 Posts
Shallow copy/Deep copy for contained objects
Jan 12, 2009 05:22 AM|Van_003|LINK
I am creating a class that basically need to perform some operations on files after reading them.
The class skeleton is like this
class filesystem
{ private string fileName;
private FileInfo fileInformation;
private Dictionary<string,string> headers;
}
What is the best way to expose these member variables as properties ? The FileInfo object needs a string(file path) in its constructor
1.If i do
public FileInfo File{
get { return file; }}
I want a setter property also for my FileInfo Object. How do i accomplish it?
{
get { return headers; } set{
if (headers == null) headers = new Dictionary<string, string>();headers = value;}
}
Is it right way to code setter property for thsi kind of collection ? If not, what is the right way ? I did this way because I felt simply doing headers=value would create a shallow copy.
Please suggest.
Thanks in advance
Member
141 Points
30 Posts
Re: Shallow copy/Deep copy for contained objects
Jan 12, 2009 08:59 AM|SMHoff|LINK
Well there's a couple questions there.
Quote:
"What is the best way to expose these member variables as properties ? The FileInfo object needs a string(file path) in its constructor"
The best way really depends upon the situation. However, to simple set the variable you could do something like:
None
0 Points
20 Posts
Re: Shallow copy/Deep copy for contained objects
Jan 12, 2009 11:34 PM|Van_003|LINK
Ok, That looks like a good idea.
Just one confusion,
If one were to do myDictionary = value on a dictionary object in a setter property, without acquiring memory for it specifically using new(),
does it create a shallow copy , or does the framework itself manages to create a deep copy in this case ?
Participant
1540 Points
421 Posts
Re: Shallow copy/Deep copy for contained objects
Jan 13, 2009 04:13 AM|anonymouswrites|LINK
I am not sure if it is a good practise or not but What I do mostly is something like this
private Dictionary<int,string> myDic = new Dictionary<int,string>();
public Dictionary<int,string> MyDic
{
get {return myDic;}
set{myDic = value;}
}