#296 – You Must Assign a Value to All Fields Before Using a struct
April 9, 2011 Leave a comment
Before you can reference a struct in your code, all of its fields must first be initialized. If all fields have not been initialized, the compiler will issue an error.
In the example below, we assign only two of the three public fields of a Point3D struct. The compiler complains with the error:
Use of unassigned local variable ‘pt’
static void Main() { Point3D pt; pt.x = 5.0; pt.y = 5.0; DoSomethingWithPoint(pt); } static void DoSomethingWithPoint(Point3D pt) { Console.WriteLine("{0},{1},{2}", pt.x, pt.y, pt.z); }