Struct vs Class

mp3909

Well-known member
Joined
Apr 22, 2018
Messages
61
Location
UK
Programming Experience
3-5
Why is it I get an error in the below code saying auto implemented property must be fully assigned before control is returned to the caller

C#:
    public struct Customer
    {
        private int id;
        private string name;

        public Customer(int a, string b)
        {
            this.id = a;
            this.name = b;
        }

        public int ColorIndex { get; set; }
        public string ColorName { get; set; }
    }

but when I change the keyword from struct to class, the error goes away?
 
structs are simpler, but have some compiler limitations.

It should probably be a class, see Choosing Between Class and Struct | Microsoft Docs

Using Structs - C# Programming Guide | Microsoft Docs
When writing a constructor with parameters for a struct, you must explicitly initialize all members; otherwise one or more members remain unassigned and the struct cannot be used, producing compiler error CS0171.

Auto-Implemented Properties - C# Programming Guide | Microsoft Docs
This is the reason the property above errors:
the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors

This prevents the C# 6 style initialization of the property, as described in article above:
It is also an error to initialize an instance field in a struct body
 
Last edited:
Change it to a class - or else you also have to assign values to both auto-properties in your constructor.
 
if I keep it to a struct, then is this what you mean:

C#:
    public struct Customer
    {
        private int id;
        private string name;

        public int ColorIndex { get; set; }
        public string ColorName { get; set; }

        public Customer(int a, string b)
        {
            this.id = a;
            this.name = b;
            this.ColorIndex = 0;
            this.ColorName = null;
        }

    }
 
You should change it to a class. Structures should generally be immutable, i.e. you should not be able to change its internal data. That's not a hard-and-fast rule but you should have a good reason for breaking it. Why do you want this type to be a structure? Using mutable structures can cause you issues later on, e.g. if you use a structure instead of a class, this code will not behave as you might expect:
C#:
var customers = new List<Customer>();

// Add Customers here.

customers(0).ColorName = "Red";
That code will NOT set the ColorName property of the first Customer object in the list if Customer is a structure but will if it is a class.
 
Hi jmcilhinney,

I took your last comment on board and wrote this code

C#:
using System;
using System.Collections.Generic;
namespace sample
{
    public struct Customer
    {
        public int ColorIndex{get; set;}
        public string ColorName{get; set;}
    }
    
    
    public class Program
    {
        public static void Main(string[] args)
        {
            var customers = new List<Customer>();
            
            Customer c1 = new Customer(){ColorIndex = 1, ColorName = "Blue"};
            Customer c2 = new Customer(){ColorIndex = 2, ColorName = "Orange"};
            Customer c3 = new Customer(){ColorIndex = 3, ColorName = "Yellow"};
            
            customers.Add(c1);
            customers.Add(c2);
            customers.Add(c3);
            
            foreach(Customer c in customers)
            {
                Console.WriteLine(c.ColorName);   
            }       
            
            customers[0].ColorName = "Red";
    
            Console.WriteLine(customers[0].ColorName);
        }
    }
}

and I now see your point -> as I try to change the ColorName for the first customer object in the list, it gives an error saying: Cannot modify the return value of 'System.Collections.Generic.List<sample.Customer>.this[int]' because it is not a variable.

Wow! Thanks for pointing this out jmcilhinney
 
Back
Top Bottom