Ich habe einige Codes initialisieren, eine Struktur in C# zu initialisieren:Wie eine Struktur in C#
namespace Practice
{
public struct Point
{
public int _x;
public int _y;
public int X
{
get { return _x; }
set { _x = value; }
}
public int Y
{
get { return _y; }
set { _y = value; }
}
public Point(int x, int y)
{
_x = x;
_y = y;
}
}
class Practice
{
public static void Main()
{
Point p1;
p1.X = 1;
p1.Y = 2;
}
}
}
Der obige Code gibt einen Compiler-Fehler:
error CS0165: Use of unassigned local variable 'p1'
Warum diese Fehler geworfen wird?
Dies ist eigentlich einer meiner Lieblings-Peeves in C#. Jede lokale Variable sollte auf "default" IMO initialisiert werden. – leppie