You might like to check out the discussion at:
http://www.thescripts.com/forum/thread224006.html
One contribution (from Andre) says:
If you go through the documentation and specs.. you'll see that it says
'structs that are not initilized cannot be accessed or used'. This
doesn't imply that the runtime would do any sort of tracking, in fact,
the compiler will plainly give you a message that there's an error and
won't compile (till you've initialized struct data before using it).
So.. it only checks for validity upon struct data being accessed. Have a
look at the following sample program:
public struct Point
{
public int x, y;
public Point(int p1, int p2)
{
x = p1;
y = p2;
}
}
class MyClass {
static void Main(string args[]) {
Point p;
Console.WriteLine("X is: {0}", p.x);
// Compiler will perform a check hede and will abort..
p.x;
Console.WriteLine("X is: {0}", p.x);
/*
if you comment out the preceding statement, the program will work and
compile. Notice I didn't initialize y and it still worked. This shows
that the compiler doesn't check for 'every' field that's not initialized
but only when a field is being accessed */
}
}
Peter
"Jay" <nospamwrote in message
news:uF**************@TK2MSFTNGP06.phx.gbl...
In C++ I can initialise a struct as follows:
struct fileInfo {char filename[STRLEN]; int gotFile; int ch; int ln;};
fileInfo configFile = {"", 0, 1, 1};
Is there a similar way to initialise a struct in C#? The compiler flags an
error at the opening
curly brace of the second line of the following:
struct fileInfo {string filename; int gotFile; int ch; int ln;}
fileInfo configFile = {"", 0, 1, 1};
I add a constructor that takes four parameters, but that seems quite
wordy. Is there a better way?