'Why do we need to initialize struct to assign property value but not for variable

class Program
{
    public  static void Main(string[] args)
    {
       

        Car c1 ;

        // c1's data
        c1.Brand = "Bugatti";
        c1.Model = "Bugatti Veyron EB 16.4";
        c1.Color = "Gray";

       

        // c1's data
     

        // Displaying the values
        Console.WriteLine(//"Name of brand: " + c1.Brand +
                          "\nModel name: " + c1.Model +
                          "\nColor of car: " + c1.Color);


        Console.ReadLine();
    }

}

public struct Car
{

    // Declaring different data types
    public string Brand { get; set; }
    public string Model;
    public string Color;
}

c1.Brand ="Bugatti" // giving compilation error - use of unassigned local variable 'c1' //local variable might not be initialized before accessing but c.Model or c1.Color is not throwing error.



Solution 1:[1]

As per the documentation:

Because structure types have value semantics, we recommend you to define immutable structure types.

Option 1

If your Car type's properties don't need to change at runtime, they should not have any public setters. You can simplify population of a new Car by adding a constructor or by using one of the other options described in the docs.

static class Program
{
    static void Main(string[] args)
    {
        Car c1 = new Car(
            // c1's data
            brand: "Bugatti",
            model: "Bugatti Veyron EB 16.4",
            color: "Gray");

        // c1's data

        // Displaying the values
        Console.WriteLine(//"Name of brand: " + c1.Brand +
                          "\nModel name: " + c1.Model +
                          "\nColor of car: " + c1.Color);
        Console.ReadLine();
    }
}


public struct Car
{
    public Car(string brand, string model, string color)
    {
        Brand = brand ?? throw new ArgumentNullException(nameof(brand));
        Model = model ?? throw new ArgumentNullException(nameof(model));
        Color = color ?? throw new ArgumentNullException(nameof(color));
    }

    // Declaring different data types
    public string Brand { get;}
    public string Model { get; }
    public string Color { get; }
}

Option 2

Use a class instead of a struct.

    static void Main(string[] args)
    {
        Car c1 = new Car(
            // c1's data
            model: "Bugatti Veyron EB 16.4",
            color: "Gray")
        { Brand = "Bugatti" };

        // c1's data

        // Displaying the values
        Console.WriteLine(//"Name of brand: " + c1.Brand +
                          "\nModel name: " + c1.Model +
                          "\nColor of car: " + c1.Color);
        Console.ReadLine();
    }
}


public class Car
{
    public Car(string model, string color)
    {
        Model = model ?? throw new ArgumentNullException(nameof(model));
        Color = color ?? throw new ArgumentNullException(nameof(color));
    }

    // Declaring different data types
    public string Brand { get; set; }
    public string Model { get; }
    public string Color { get; }
}

Since one of the main benefits of using struct is to allocate fixed sizes of data on the stack (using stackalloc) and you are using string properties (which are not a fixed size), the most sensible option is to use a class for this scenario. Whether you actually need a constructor, use property setters, or logical defaults depends on whether your application will deal with the values if they are null and/or default.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 NightOwl888