FAQ: Basic Classes and Objects - Making Classes

This community-built FAQ covers the “Making Classes” exercise from the lesson “Basic Classes and Objects”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C#

FAQs on the exercise Making Classes

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hello,

Can someone explain the difference between an instance and an object of a class with real-world examples?

Hello, @madaskalas.

When we create an instance of a Class, we are creating an object. In a program we could have many objects. They might all be instances of the same Class, or there might be just a few instances each of many Classes.

Here is a link to an example I made on repl.it.

1 Like

Thank you, @midlindner

Can you please provide a real-world example for the this keyword just like you did with the previous example? Also, when someone uses the this keyword?

Not sure if it’s exactly what you’re looking for, but if you click on the link in my previous post, and in the left pane, click on sales.cs, you’ll see the code for the Sales Class which uses the this keyword. I just updated the link.

I can see that. However, I still do not understand it.

In sales.cs, can you please explain these lines of code?

class Sales
{

private string name {get; set;}
private string[] skills {get; set;}
private string phrase {get; set;}

  public Sales(string name, string[] skills, string phrase)
  {
    this.name = name;
    this.skills = skills;
    this.phrase = phrase;
  }
  
  public string NamePlease()
  {
    return this.name;
  }

I updated the program at the link once again to include some comments explaining a little about what is happening. The this keyword refers to the specific object being created in the constructor method. You could also say the specific instance of the class being instantiated. The specific instance of the class is an object.

Because the instance fields for Sales objects are private, they cannot be accessed directly. If we instantiate a Sales object (create an instance of the Sales Class) like so:

Sales stanley = new Sales("Stanley", new string[] {"complaining", "crossword puzzles", "napping"}, "I'm going home.");

We cannot access the name property directly like this:

Console.WriteLine(stanley.name);

We will throw an error because the name property is private. It can only be accessed by methods from within the Class.
To access the name property we do this:

Console.WriteLine(stanley.NamePlease()); //output: Stanley

As you progress through Basic Classes and Objects this will all make a lot more sense.

1 Like

Thank you, @midlindner

That helped me. However, I still do not understand the Constructor method.

//Constructor Method
  public Sales(string name, string[] skills, string phrase)
  {
    /*using this allows us to use the same parameter and property names:
    name, skills, phrase
    The instance of Sales instantiated when this method is invoked will 
    have the properties assigned here
    */
    this.name = name; 
    this.skills = skills;
    this.phrase = phrase;
  }

What does this.name = name; do and why it is necessary in the Constructor method?

I will try a guess though, correct me if and where I am wrong. I will try to analyze this.name = name;

The this keyword refers to the object being instantiated when we first created the instance of Sales class in the Main method. In our first case, it looks at jim object. So this equals to jim.name . Next, the = name; it is the parameter in our Construtor method. So, whatever it is in our second part of the statement, assign it to the first part. In other words, jim.name = ‘Jim’;

In the following source code,

public string NamePlease() {
return this.name;
}
, it looks at the Constructor method and prints Jim
1 Like

Your explanation pretty well sums it up. Since we can’t hardcode jim.name = name we use this to refer to the current instance. We could avoid the use of this altogether in the constructor by using different names for the properties and parameters, but this example illustrates its usefulness.

1 Like