Z
Zytan
Is it possible, as in C? I don't think it is. Just checking.
Zytan
Zytan
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Is it possible, as in C? I don't think it is. Just checking.
Is it possible, as in C? I don't think it is. Just checking.
Why wouldn't it be possible? What exactly are you referring to?
If you provide a constructor that will initialize all members.
abc = ...; // declared and initialized in one line. Is this possible?
I wasn't looking to use a constructor. But, maybe I need one.
Does "int x = 4;" invoke a constructor for 'int'?
Zytan said:I wasn't looking to use a constructor. But, maybe I need one.
Does "int x = 4;" invoke a constructor for 'int'? Is that how
declarations and initializations are done on the same line?
instead. It depends on what kind of expression you want on the right
hand side of the assignment.
One way or another the RHS expression must be turned into an instance
of your struct.
No, but int in special since you can write literals for it in C# and
there are special IL opcodes for it.
initialize a struct, you generally need to call a constructor.
If it's
a mutable struct (generally a bad idea) you *could* just do:
MyStruct x;
x.SomeProperty = ...;
but it's generally better to have a constructor which fully constructs
the struct, and call that.
struct abc { ... };
abc y = ...; // declared and initialized in one line. Is this
possible?
Zytan said:Well, the primitive types are a special case - but yes, if you want to
initialize a struct, you generally need to call a constructor.
I guess I was just looking for something as simple as what you can do
with initializing an array:
int[] x = new int[] { 4, 5, 6, 7 };
Yes, I think I need a constructor. I did notice that C# complains if
you only initialize a portion of the fields in the struct (if you miss
one) and then attempt to use it. So, it is safe (perhaps not so in
earlier versions?). But, a c'tor seems more safe.
A mutable struct is a bad idea? What is a mutable struct? I know
mutable means it can change. But, unless all fields are private,
isn't the struct mutable? Or is this precisely what you mean, that
struct fields should all be private (almost like a class)?
Zytan said:struct abc { int x; double y; AnotherStruct s; };
abc y = { 100, 3.14, ??? };
It works up until the ???, since it's a struct. I don't know what to
put there.
with initializing an array:int[] x = new int[] { 4, 5, 6, 7 };
Well, you can do:
MyStruct[] x = new MyStruct[] { new MyStruct(1), new MyStruct(2) };
All fields *should* be private, for both structs and classes.
If you
need to access them from outside, use methods or properties.
A mutable
struct would be one which let you change the values, whether via
properties or methods (or, of course, non-private fields).
You don't do it like that - you have a constructor, and do:
abc y = new abc (100, 3.14, new AnotherStruct(...));
I guess I was just looking for something as simple as what you can do
with initializing an array:
int[] x = new int[] { 4, 5, 6, 7 };Well, you can do:MyStruct[] x = new MyStruct[] { new MyStruct(1), new MyStruct(2) };
Yes, sorry, I didn't mean that I wanted an array of struct, I mean
that I wanted to do something like this:
MySrtuct x = new MyStruct { field1, field2, field3, field4 };
Where fieldX are the fields in MyStruct, which could, themselves, be
structs.
All fields *should* be private, for both structs and classes.
Even for structs? But sometimes a struct is just something simple,
like holding x and y for the concept of a point.
Zytan said:MyStruct[] x = new MyStruct[] { new MyStruct(1), new MyStruct(2) };
Yes, sorry, I didn't mean that I wanted an array of struct, I mean
that I wanted to do something like this:
MySrtuct x = new MyStruct { field1, field2, field3, field4 };
Where fieldX are the fields in MyStruct, which could, themselves, be
structs.
Even for structs? But sometimes a struct is just something simple,
like holding x and y for the concept of a point.
Ok, I will try adding a constructor, and making everyting private. I
think in my case this will do just fine.
Even for structs? But sometimes a struct is just something simple,
like holding x and y for the concept of a point.
Yes, but be careful here: note the difference between making a point a
class versus making it a struct.
[snip]
The reason that you usually want immutable structs (that is, structs
with constructors that initialize all members, no public fields, and
no properties with setters or methods that change the struct's value)
is that struct is usually intended to create new value types, like
double, int, float, DateTime, etc.
Most ex-C programmers (like me) try
at least once to use "struct" to build a "light" class of just fields
and no methods. I tried to use it for StockItem or some such thing. It
was a disastrously bad idea. I learned my lesson.
Well, change the braces to brackets and define the appropriate
constructor, and you're there.
Right.
That doesn't mean it's not worth encapsulating them in properties.
Non-private fields are highly discouraged. You get much more control
and power with properties. You can add in validation etc later, or even
potentially change the underlying type/implementation without callers
caring.
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.