mrichmon
asked on
Advanced Programmers Only: Initialize variables at declartion vs in constructor
This is more of a theoretical / best practice / most efficeint method type of question...
The following two pieces of code have the same effect:
public class MyClass
{
int test = 1;
}
public class MyClass
{
int test;
MyClass() {test = 1;}
}
Is it better/more effiecient/etc to assign defaut values when you declare a variable or to assign the values in the default constructor?
Is it just preference?
One advantage to initializing at the time of declaration is that if you have multiple values and multiple constructors the constructors only have to deal with the few values.
i.e.
public class MyClass
{
int test1 = 1;
int test2 = 2;
int test3 = 3;
int test4 = 4;
int test5 = 5;
int test6 = 6;
MyClass(int x)
{ test1 = x;} // test2 through test6 keep default values
MyClass(int x, int y)
{ test1 = x; test2 = y; } // test3 through test6 keep default values
}
versus
public class MyClass
{
int test1;
int test2;
int test3;
int test4;
int test5;
int test6;
MyClass(int x)
{
test1 = x;
test2 = 2;
test3 = 3;
test4 = 4;
test5 = 5;
test6 = 6;
}
MyClass(int x, int y)
{
test1 = x;
test2 = y;
test3 = 3;
test4 = 4;
test5 = 5;
test6 = 6;
}
}
However if you can chain the constructors like so then does that advantage get counteracted?
public class MyClass
{
int test1;
int test2;
int test3;
int test4;
int test5;
int test6;
MyClass()
{
test1 = 1;
test2 = 2;
test3 = 3;
test4 = 4;
test5 = 5;
test6 = 6;
}
MyClass(int x) : this()
{
test1 = x;
}
MyClass(int x, int y) : this()
{
test1 = x;
test2 = y;
}
}
Comments appreciated....
The following two pieces of code have the same effect:
public class MyClass
{
int test = 1;
}
public class MyClass
{
int test;
MyClass() {test = 1;}
}
Is it better/more effiecient/etc to assign defaut values when you declare a variable or to assign the values in the default constructor?
Is it just preference?
One advantage to initializing at the time of declaration is that if you have multiple values and multiple constructors the constructors only have to deal with the few values.
i.e.
public class MyClass
{
int test1 = 1;
int test2 = 2;
int test3 = 3;
int test4 = 4;
int test5 = 5;
int test6 = 6;
MyClass(int x)
{ test1 = x;} // test2 through test6 keep default values
MyClass(int x, int y)
{ test1 = x; test2 = y; } // test3 through test6 keep default values
}
versus
public class MyClass
{
int test1;
int test2;
int test3;
int test4;
int test5;
int test6;
MyClass(int x)
{
test1 = x;
test2 = 2;
test3 = 3;
test4 = 4;
test5 = 5;
test6 = 6;
}
MyClass(int x, int y)
{
test1 = x;
test2 = y;
test3 = 3;
test4 = 4;
test5 = 5;
test6 = 6;
}
}
However if you can chain the constructors like so then does that advantage get counteracted?
public class MyClass
{
int test1;
int test2;
int test3;
int test4;
int test5;
int test6;
MyClass()
{
test1 = 1;
test2 = 2;
test3 = 3;
test4 = 4;
test5 = 5;
test6 = 6;
}
MyClass(int x) : this()
{
test1 = x;
}
MyClass(int x, int y) : this()
{
test1 = x;
test2 = y;
}
}
Comments appreciated....
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I am not totally sure how to go about finding the MSIL code that you showed, although I am able to read and understand it ;o)
So your point makes sense.
But here's a twist - waht if they were no longer simple types but user classes themselves....
Thanks for your input.
So your point makes sense.
But here's a twist - waht if they were no longer simple types but user classes themselves....
Thanks for your input.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
good thoughts - thanks for the comments!
ASKER
I read "C# takes any initializers and executes them in the order declared before executing the constructor" But since they are called one right after the other I am not sure that the order difference even matters....
I would expect that for a small example the performance would be the same, but what if you have a lot of variables? Would it still not affect performance?
Which way do you prefer and why?
Thanks for the comments!