I know I sound stupid, but why is last created twice in the following code? I’m confused as to why last needs to equal lastName to be used. Why not just use “last” alone? It looks like 2 strings with different names doing the exact same thing.

public class Person { private string last; private string first;
public Person(string lastName, string firstName) { last = lastName; first = firstName; }
// Remaining implementation of Person class. }
Just to reformat your code slightly:
I’m not sure what you mean by ‘created twice’. The first two statements say that the Person class stores two bits of data that no-one but this class is allowed to see (private fields).
The constructor function takes two arguments (new Person("Lister", "Dave")) and takes each of these values and stores them in the private fields you declared.
The private string last; declares what the field is, and sets a default value (null). The assignment operator in the constructor actually sets it to something useful.
Bonus point for the Red Dwarf reference 🙂
I think OP is confused because of class definition vs constructor definition. But yeah, OP basically you created a class. The constructor is to instantiate the class into an object.
So like: public class Person is just a blueprint for a Person object.
The constructor is what actually is used to create the object. Hope this along with the comment above helps.
From what i’ve seen the string parameter in constructors are = to a separate name used in the main method. Can;t you create fields for the string made in the constructor without making a separate one with a different name and passing it into it?
You’re confusing a few things:
Let’s just start with a class without a constructor:
This class defines what a Person must have. You can create an instance of the class with the new keyword:
Notice the empty brackets? That’s because you’re calling the constructor with no arguments that implicitly exists for every class that has no explicitly defined constructors.
Now of course you can do the following:
or with newer syntax you can even do:
But it does NOT guarantee that all instances of class Person actually have a Lastname or Firstname.
But uncertain state is a cause for bugs, so you want to be able to control that the name must be filled. That’s where constructors come in.
We’ve already had in implicit constructor, so let’s do the exact same thing with an explicit constrcutor:
Notice that the constructor has no return value unlike normal methods. That’s because it returns (in combination with the new keyword) an instance of the class it is part of. A return type would be pointless.
So, it’s not much better right now. But we can expand on it. We can restrict the access to Lastname and Firstname via properties and only allow setting the values via a constructor.
Now we cannot call the empty constructor anymore, because we don’t have one.
We can also implement checks in the constructor, or compute arbitrary things:
I’m also a beginner, but do you mean not having a constructor at all?
You can actually not have no constructor. If you do not declare on yourself, there will always be a “hidden” constructor with no Parameters.
If OP would’t have declared a Constructor, there would be a “hidden” (Hidden because you did not write one and don’t see one) one.
And it would look like this :

I think that the author did not want to use “this” keyword to distinguish a parameter name from a private field name. You can change it to:
C# devs
null reference exceptions

source