Person Object Question

Your first code example - the constructor - you're right, you don't need to do it that way. You could make the object and then set the name through the setter later.

However if you're designing the Person class for someone else to use, you might not want the user to be making Person objects with null values for Name, because that could cause run-time errors. All Persons must have some name. So if you write the constructor like that you will not allow someone to write... var Mike = new Person(); ...because there will be no constructor that takes zero arguments. It's a way of helping to ensure that your Person class is used correctly.

Also, sometimes it is helpful to create a variety of constructors to help translate from different data types. A quick and dirty example: public class Coordinate { public double X { get; set; } public double Y { get; set; } public double Z { get; set; }

    public Coordinate(string CoordinateDefinitionString, char Delimiter)
    {
        var splitString = CoordinateDefinitionString.Split(Delimiter);
        X = Double.Parse(splitString[0]);
        Y = Double.Parse(splitString[1]);
        Z = Double.Parse(splitString[2]);
    }
}
var myCoordinate = new Coordinate("1.234,3.120,5.789", ',');

Now with regard to your pastebin example - think about who or what should be responsible for answer what a Person's name is. If you want to know someone's name you ask them and they tell you - it doesn't go through some external source. The idea is that things relevant to a Person should be encapsulated by it. Example:

    public class Person
    {
        public string FirstName { get; set; }
        public string MiddleName { get; set; }
        public string LastName { get; set; }

        public string FullLegalName
        {
            get
            {
                if (MiddleName == null)
                {
                    return FirstName + " " + LastName;
                }
                else
                {
                    return FirstName + " " + MiddleName + " " + LastName;
                }
            }
        }

        public Person(string First, string Last)
        {
            FirstName = First;
            MiddleName = null;
            LastName = Last;
        }

        public Person(string First, string Middle, string Last)
        {
            FirstName = First;
            MiddleName = Middle;
            LastName = Last;
        }
    }

This way the Person knows whether or not they have a middle name and if that's relevant to answer when someone asks.

/r/csharp Thread