simple question about struct

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;
}
 
A

Anthony Tolle

[snip]
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 ?
[snip]

From official documentation at http://msdn.microsoft.com/en-us/library/ah19swz4(VS.71).aspx

"When you create a struct object using the new operator, it gets
created and the appropriate constructor is called. Unlike classes,
structs can be instantiated without using the new operator. If you do
not use new, the fields will remain unassigned and the object cannot
be used until all of the fields are initialized."

--
 
P

Peter Duniho

Tony said:
Hi!

Almost always should the access modifier be private as I have in my struct
Test below.

Yes. IMHO, public fields, in a struct or in a class, should always be
"readonly" (and thus must be initialized in the constructor or via an
initializer in the declaration) and should exist only in the simplest of
types.
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.
Correct.

When I use the second way
Test test2;
No initialization is being done for variable number.

Also correct. Which should be obvious. In the first example, you've
assigned a value to the variable and in the second you haven't. That's
exactly what "initialization" is.
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;

Not correct. You can always initialize the variable just as you did in
the first declaration, just later:

Test test1 = new Test();
Test test2;

test2 = new Test();
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 ?

You've missed something. Whether a field is private or not has nothing
to do with how to declare the variable (i.e. with or without an
initializer). You still have to initialize the variable before it's
used, but whether you do that in the declaration itself or later doesn't
matter.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top