views:

36

answers:

4
class A
{
 public int a;
 public int c;
}

i will create 10 instances from A.Then i will create 15 instances from A again... go on. first 10 instance will have same value for a variable and next 15 instances will have again same value for a.But I don't mean that both group has same values for a .Problem is create same a value 10 times in first group and 15 times in second group on memory unnecessary.

What would be Best solution or solutions for reduce unnecessary datas in this situation?

A: 

If a and c are actually just integers, it won't be worth your time trying to optimize them out of the memory space; in most cases, anything you would use to do so would take up more space than the integers themselves.

However, if a and c are actually objects that take up a significant amount of memory, you could instead put object pointers (or holders, depending on the language) as members of A instead of the objects themselves. That way the only memory duplicated in the pointer.

VeeArr
object pointers mean referance type variable ? If so i gave similiar answer below.But i don't know how much space will take refance members.
Freshblood
References to an existing object are frequently little more than pointers, and take up a small amount of memory.
VeeArr
A: 

It is clear that static member will not work for this problem.Because instance groups will have different values.

this can be one of the solutions.What can other else ?

class Common
{
  public int a;
}
class A
{
  public Common a;
  public int c;
}
Freshblood
A: 

Would a HashSet<T> help?

Jim G.
I don't think so
Freshblood
A: 

Another solution is

class A
{
  public int? a;
  public int c;
}

but instead of create new instance of int? class we should to assign same instance.But casting int? to int will be ugly to use and cause performance issues.

Freshblood