J
Joe
I have a fundamental question about classes (and structs for that matter).
Suppose I have a class with a bunch of members and methods. And this class
will be instantiated many many times at once in memory. Now, performance
wise and memory consumption wise, which method would be best to design the
class? My underlying concern is that method #1 will consume more memory
since each class instantiated will be larger than method #2. Is this a
false assumption?
//Method 1: (keep methods inside class)
public class MyRectangle
{
double Width;
double Height;
public MyRectangle(double w, double h)
{
Width = w;
Height = h;
}
public double CalculateArea()
{
return (Width * Height);
}
}
//Method 2: (keep methods in another class)
public class MyRectangle
{
double Width;
double Height;
public MyRectangle(double w, double h)
{
Width = w;
Height = h;
}
}
Public class MyRectangleCalculations
{
public double CalculateArea(MyRectangle rect)
{
return (rect.Width * rect.Height);
}
}
Suppose I have a class with a bunch of members and methods. And this class
will be instantiated many many times at once in memory. Now, performance
wise and memory consumption wise, which method would be best to design the
class? My underlying concern is that method #1 will consume more memory
since each class instantiated will be larger than method #2. Is this a
false assumption?
//Method 1: (keep methods inside class)
public class MyRectangle
{
double Width;
double Height;
public MyRectangle(double w, double h)
{
Width = w;
Height = h;
}
public double CalculateArea()
{
return (Width * Height);
}
}
//Method 2: (keep methods in another class)
public class MyRectangle
{
double Width;
double Height;
public MyRectangle(double w, double h)
{
Width = w;
Height = h;
}
}
Public class MyRectangleCalculations
{
public double CalculateArea(MyRectangle rect)
{
return (rect.Width * rect.Height);
}
}