T
Tony Johansson
Hi!
Almost always should the access modifier be private as I have in my struct
Test below.
I can create a variable of this struct Test in two ways.
Test test1 = new Test();
Test test2;
When I use this way
Test test1 = new Test();
will all the instance variable be automaticaly initialized by their
constructor. In this case number is
initialized to 0 and the variable test1 is ready to be used.
When I use the second way
Test test2;
No initialization is being done for variable number.
You can't use property or methods to set the instance variable number the
one and only way to initialize this number in the struct
is to change the access modifier to public for this number so you can
initialize it in this way test2.number;
Now to my question because of the the instance variables is declared as
private is the use of struct when
you declare a variable like this
Test test;
of extremely little use as I believe or have I missed something ?
struct Test
{
private int number;
}
static void Main(string[] args)
{
Test test;
}
Almost always should the access modifier be private as I have in my struct
Test below.
I can create a variable of this struct Test in two ways.
Test test1 = new Test();
Test test2;
When I use this way
Test test1 = new Test();
will all the instance variable be automaticaly initialized by their
constructor. In this case number is
initialized to 0 and the variable test1 is ready to be used.
When I use the second way
Test test2;
No initialization is being done for variable number.
You can't use property or methods to set the instance variable number the
one and only way to initialize this number in the struct
is to change the access modifier to public for this number so you can
initialize it in this way test2.number;
Now to my question because of the the instance variables is declared as
private is the use of struct when
you declare a variable like this
Test test;
of extremely little use as I believe or have I missed something ?
struct Test
{
private int number;
}
static void Main(string[] args)
{
Test test;
}