Re: one line struct initialization

Tech-Archive recommends: Fix windows errors by optimizing your registry



Zytan <zytanlithium@xxxxxxxxx> wrote:
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 };

Well, you can do:

MyStruct[] x = new MyStruct[] { new MyStruct(1), new MyStruct(2) };

etc

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.

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.

Yes, that's much better.

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)?

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).

--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.



Relevant Pages

  • Re: remove structs default constructor?
    ... I can only initialize those private members via constructor ... if the default constructor does not disappear once I ... > create my own struct constructor, the caller can create a struct still ... Imagine only having proper values. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Structs cannot contain explicit parameterless constructors
    ... What do you use this type of struct for? ... have a default parameterless constructor. ... the C# compiler disallows definition ... I'm slightly perplexed that I can't explicitly initialize a ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to initialize a member struct
    ... I can't give this struct a constructor. ... class constructor (call it "TemplatedClass") in another DLL library. ... This leaves me with trying to come up with a way to initialize ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Basic info needed on struct in C#
    ... constructor, so you dont need to initialize all the members to zero. ... A default constructor is always provided to initialize the struct ...
    (microsoft.public.cert.exam.mcad)
  • Re: C++ Compiler behavior regarding struct constructors
    ... > struct default constructor ... underlying class type shall have a user-declared default constructor. ... MyStruct, that default constructor zero-fills the memory. ... if you need initialization, ...
    (microsoft.public.dotnet.languages.vc)