C# and structs... something odd may even a bug with C#

  • Thread starter Thread starter James Lapalme
  • Start date Start date
J

James Lapalme

Dear All,

This code should not compile and execute but is does ... why???

Here is the code ... I'm using Visual Studio .Net 2002

public struct Point{
public int x;
public int y;
}

public class App{
static Point p;
public static void Main(){
Point a = new Point();
a.x = 1;
a.y = 1;
a.Equals(p);
}
}

If I change the code to :

public class App{
public static void Main(){
Point p;
Point a = new Point();
a.x = 1;
a.y = 1;
a.Equals(p);
}
}

It does not compile any more because I'm using p without having initialized
it's members... but in the first code I did not initialized them either.

Thanks,

James Lapalme
 
Thats because member variables (static and nonstatic) are automatically
assigned to its default value, whereas local variables are not and have to
be assigned manually.
 
Back
Top