Skip to main content

How to Resolve Error "CS0170: Use of possibly unassigned field 'field'" in C#

The Compiler Error CS0170 is a Definite Assignment error specific to Structs (Value Types). The message reads: "Use of possibly unassigned field 'StructField'".

Unlike Classes (Reference Types), which default to null when declared, a Struct declared without the new keyword exists on the stack immediately, but its internal fields contain undefined (garbage) data. C# does not automatically initialize these fields to zero/false/null unless you explicitly call the constructor.

This error occurs when you try to read a field from a struct variable that was declared without new and hasn't had all of its fields manually initialized yet.

Understanding Struct Initialization

When you use a Struct in C#, you have two ways to initialize it:

  1. With new: var s = new MyStruct();
    • Calls the default constructor.
    • Automatically sets all fields to their default values (0, false, null).
    • The object is considered fully assigned.
  2. Without new: MyStruct s;
    • Allocates space on the stack.
    • Does NOT clear the memory. The fields are unassigned.
    • The compiler treats the struct as "unusable" until you manually assign a value to every single field.

CS0170 happens in the second case when you try to read a field before finishing the manual assignment.

Scenario 1: Declaration Without new

This is the most common cause. You declare a struct variable but forget to initialize it or call new before trying to access its data.

Example of error:

public struct Point
{
public int X;
public int Y;
}

public class Program
{
static void Main()
{
// Declared without 'new'. Fields X and Y are undefined.
Point p;

// ⛔️ Error CS0170: Use of possibly unassigned field 'X'
// The compiler refuses to read 'X' because it has no value yet.
System.Console.WriteLine(p.X);
}
}

Scenario 2: Partial Initialization

Even if you assign some fields, the struct as a whole remains in an "unassigned" state until all fields are set. You cannot use the struct (e.g., pass it to a method) or read the unassigned fields.

Example of error:

public struct User
{
public int Id;
public string Name;
}

public class Program
{
static void Main()
{
User u;
u.Id = 100; // We assigned Id...

// ⛔️ Error CS0170: Use of possibly unassigned field 'Name'
// We forgot to assign 'Name', so accessing it is illegal.
System.Console.WriteLine(u.Name);

// This would ALSO fail, because 'u' is not fully initialized:
// ProcessUser(u);
}
}

The easiest and safest fix is to always use the new operator when declaring structs. This invokes the default constructor, which guarantees that all fields are zeroed out (integers become 0, booleans become false, references become null).

public struct Point
{
public int X;
public int Y;
}

public class Program
{
static void Main()
{
// ✅ Correct: 'new' initializes X to 0 and Y to 0 automatically.
Point p = new Point();

System.Console.WriteLine(p.X); // Prints: 0
}
}

Increase readability by using Object Initializers

You can combine new with initializers for readability.

// ✅ Correct: Fully initialized and values set
Point p = new Point { X = 10, Y = 20 };

Solution 2: Manually Assign All Fields

If you have a specific reason to avoid new (rare high-performance scenarios to skip the init command), you must strictly assign every public field before using the variable.

public struct Point
{
public int X;
public int Y;
}

public class Program
{
static void Main()
{
Point p;

// We must initialize ALL fields manually
p.X = 10;
p.Y = 20;

// ✅ Correct: The compiler tracks that both X and Y are assigned.
// The struct 'p' is now considered fully initialized.
System.Console.WriteLine(p.X);
}
}
note

Auto-Implemented Properties: If your struct uses Auto-Properties (public int X { get; set; }), you must use new (Solution 1). You cannot assign to backing fields of auto-properties manually in this context because those fields are hidden/private.

Conclusion

CS0170 enforces memory safety for value types.

  1. Identify the Struct: This error only happens with struct, not class.
  2. Check Declaration: Did you use Point p; instead of Point p = new Point();?
  3. The Fix:
    • Best Practice: Use new to ensure defaults are set.
    • Manual Fix: If omitting new, ensure every single field is assigned a value before you try to read any of them.