Skip to main content

Struct cannot contain parameterless constructor

In This Article

CodeRush Classic shows the Struct cannot contain parameterless constructor code issue if a struct constructor does not have parameters.

#Fix

Add the required parameters to the struct constructor.

#Purpose

Highlights the struct constructor declarations, which would cause the Structs cannot contain explicit parameterless constructors compilation error.

#Example

public struct UserInfo
{
    public UserInfo()
    {
        Name = string.Empty;
        FamilyName = string.Empty;
        BirthDate = DateTime.MinValue;
        Phone = string.Empty;
    }
    public string Name { get; set; }
    public string FamilyName { get; set; }
    public DateTime BirthDate { get; set; }
    public string Phone { get; set; }
}

Fix:

public struct UserInfo
{
    public UserInfo(string name, 
        string familyName, 
        DateTime birthDate, 
        string phone)
    {
        Name = name;
        FamilyName = familyName;
        BirthDate = birthDate;
        Phone = phone;
    }
    public string Name { get; set; }
    public string FamilyName { get; set; }
    public DateTime BirthDate { get; set; }
    public string Phone { get; set; }
}