Non-default values for a struct ?

  • Thread starter Joanna Carter [TeamB]
  • Start date
J

Joanna Carter [TeamB]

Hi

struct MyStruct
{
int value = 64; // does not compile

public MyStruct()
{
value = 64; //does not compile
}
}

What do I do to create a struct so that default(MyStruct) returns a MyStruct
with value already set to 64 ?

Joanna
 
P

Padu

"Joanna Carter [TeamB]"
Hi

struct MyStruct
{
int value = 64; // does not compile

public MyStruct()
{
value = 64; //does not compile
}
}

What do I do to create a struct so that default(MyStruct) returns a
MyStruct
with value already set to 64 ?

Joanna

Hi Joanna,

The .net framework disallows default (parameterless) constructors for
structs. I'm not sure why, but my guess is that because you can use structs
without "new", the framework cannot guarantee that the default constructor
will always be called.

Suppose the framework did allow your code to compile, then what would be the
value of "value" after the following code:

{
MyStruct struct;
Console.WriteLine("Value is {0}",struct.value);
}

The default value for int is 0, and that would be the output of the code
above, which could allow for some nasty bugs in your code. But that's only
my guess, please someone jump in if I'm wrong.


All that said, you are still allowed to write parameterized constructors,
and your code could look like:

struct MyStruct
{
int value;

public MyStruct(int initval)
{
value = initval;
}
}


Hope that helps.



By the way, it's amazing to see how many delphi dissidents I'm finding
around here. To speak the truth, all my friends who used to program in
delphi before are now converted to C#...

Cheers

Padu
 
M

Michael Nemtsev

Hello Padu,

The reason is that .net can't guarantee to call parameterless constructor.
Jon Skeet described this here http://www.yoda.arachsys.com/csharp/faq/#struct.constructors

P> The .net framework disallows default (parameterless) constructors for
P> structs. I'm not sure why, but my guess is that because you can use
P> structs without "new", the framework cannot guarantee that the
P> default constructor will always be called.

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
J

Joanna Carter [TeamB]

"Padu" <[email protected]> a écrit dans le message de (e-mail address removed)...

| By the way, it's amazing to see how many delphi dissidents I'm finding
| around here. To speak the truth, all my friends who used to program in
| delphi before are now converted to C#...

Hey, don't forget that Borland Developer Studio 2006 allows us "Delphiites"
to program in C# within the same IDE that we use for Delphi for Win32 or
..NET. So at least we don't have to completely defect to "the dark side" :))

Joanna
 

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