Proper class design

  • Thread starter Thread starter Peter
  • Start date Start date
P

Peter

Hi,

I have a question about good class design ....

In my class I have some properties related to large amount
of data.
What is the best strategy, to include a reference to those
data (and making the object dependent on the client) or
copy the data into the object (independent object) ?

For example ....
class MyClass
{
int [] data; // reference to an int array .. no memory
alloc

(...)

// property Data
public Data
{
get
{
return data;
}
set
{
data = value;
}
}
}

and in the main method:

int [] large_data = new int[1000000];
MyClass myClass = new MyClass();
myClass.Data = large_data;

In this case the member "data" (via Property Data) refers to
"large_data" memory on the heap, like a C pointer.
At the other hand, if the "large_data" memory gets out of
scope, the object will no longer work, because of missing
data.

e.g.

MyClass myClass = new MyClass();
{
int [] large_data = new int[1000000];
myClass.Data = large_data;
}
// here, large_data is out of scope !!
int [] dd;
dd = myClass.Data; // data are missing ???

Some help would be nice.

Thanks,
Peter
 
Hi,

Did you tried your example?

The memory will not be reclamed until no references are left.

MyClass myClass = new MyClass();
{
int [] large_data = new int[1000000];
myClass.Data = large_data;
}

as myClass is referencing the data it will not be reclamed.

which is the best way to create it , it does depend of how/who create the
data.

Cheers,
 
Hi,

I must say I did not try the given example, but it's clear,
..NET is really very smart.

Thanks,
Peter

Sahil Malik said:
In the first example, large_data does not get GC'ed unless myClass itself
goes out of scope as myClass is holding a reference to large_data.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik


Peter said:
Hi,

I have a question about good class design ....

In my class I have some properties related to large amount
of data.
What is the best strategy, to include a reference to those
data (and making the object dependent on the client) or
copy the data into the object (independent object) ?

For example ....
class MyClass
{
int [] data; // reference to an int array .. no memory
alloc

(...)

// property Data
public Data
{
get
{
return data;
}
set
{
data = value;
}
}
}

and in the main method:

int [] large_data = new int[1000000];
MyClass myClass = new MyClass();
myClass.Data = large_data;

In this case the member "data" (via Property Data) refers to
"large_data" memory on the heap, like a C pointer.
At the other hand, if the "large_data" memory gets out of
scope, the object will no longer work, because of missing
data.

e.g.

MyClass myClass = new MyClass();
{
int [] large_data = new int[1000000];
myClass.Data = large_data;
}
// here, large_data is out of scope !!
int [] dd;
dd = myClass.Data; // data are missing ???

Some help would be nice.

Thanks,
Peter
 
Actually think of it this way, say if you were doing COM, and if you don't
call RELEASE, the instance remains loaded. The only difference here is that
rather than delegating the responsibility to the object itself for cleanup,
the responsibility belongs to GC instead. So the CLR needs to keep a count
of all the various pointer references to an object - and thats about it. Not
that big a deal :)

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik



Peter said:
Hi,

I must say I did not try the given example, but it's clear,
.NET is really very smart.

Thanks,
Peter

Sahil Malik said:
In the first example, large_data does not get GC'ed unless myClass itself
goes out of scope as myClass is holding a reference to large_data.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik


Peter said:
Hi,

I have a question about good class design ....

In my class I have some properties related to large amount
of data.
What is the best strategy, to include a reference to those
data (and making the object dependent on the client) or
copy the data into the object (independent object) ?

For example ....
class MyClass
{
int [] data; // reference to an int array .. no memory
alloc

(...)

// property Data
public Data
{
get
{
return data;
}
set
{
data = value;
}
}
}

and in the main method:

int [] large_data = new int[1000000];
MyClass myClass = new MyClass();
myClass.Data = large_data;

In this case the member "data" (via Property Data) refers to
"large_data" memory on the heap, like a C pointer.
At the other hand, if the "large_data" memory gets out of
scope, the object will no longer work, because of missing
data.

e.g.

MyClass myClass = new MyClass();
{
int [] large_data = new int[1000000];
myClass.Data = large_data;
}
// here, large_data is out of scope !!
int [] dd;
dd = myClass.Data; // data are missing ???

Some help would be nice.

Thanks,
Peter
 
Back
Top