Learn How To Program In C# Part 9 – Classes And Objects


Hey, everyone. Today we’ll be learning about classes and
objects. The reason why it’s exiting is that this
is our first step towards understanding object-oriented programming. Let’s define a class. A class is a construct which allows us to
create our own custom types. The way it does that is by allowing us to
group together variables of other types and our own custom type that we’re building. We can also include that there. We haven’t gone over methods and events,
but we will in our future video, so do not worry about that. One more thing, when you are thinking of a
class, I want you to think of a template, a blueprint, but it’s exactly what it is. A class is a template which defines the data
and the behavior of the type which we’re trying to create. Let’s create a project. Let’s call this ClassesAndObjects. namespace ClassesAndObjects
{ class Program
{ static void Main(string[] args)
{ }
} }
And right off the bat, I want you to notice this, class Program. Remember the infamous method that we’re
very familiar with, the Main method where the execution of our program begins? Exactly. The method is in the class called Program. And this is the custom type that it’s defining. Let’s go to Solution Explorer and right-click
on ClassesAndObjects, go to Add ->New Item. Click on Class, and type in any name you want. In this case, I will say Person. There you go. namespace ClassesAndObjects
{ class Person
{ }
} So you can see that it says class Person and
we have a block. namespace ClassesAndObjects
{ public class Person
{ }
} I have included the keyword public to the
class. This is called an access specifier. You might be asking, “Okay, well, there’s
no access specifier when we just created the class.” And that’s true. We can see it. But it doesn’t mean that it’s not there. This is short for saying internal class Person. namespace ClassesAndObjects
{ internal class Person
{ }
} It’s equivalent to that. So there’s always an access specifier. However, it is just a short way of writing
it. It’s the default. Whenever you are not specifying an access
specifier, the default is internal. What the public access specifier stands for
is it pretty much says anything outside of this class and inside of it can use the class
and its members. That’s pretty much it. There is a little bit more to it, but we will
go over that later. We have class reserved keyword which allows
us to define our custom type and create our class template, so to speak. And then we have the name. This name can be anything. And the more descriptive the better so that
you know what your class stands for. In this case, it’s a person. And then we have the code block. This is where you will put the fields and
the properties and your methods and events and such. Let’s go back to our program. namespace ClassesAndObjects
{ class Program
{ static void Main(string[] args)
{ Person person1=new Person();
} }
} We have added a new statement. You see new. This is a reserved keyword. new allows us to create the object based on
the class. And then what we do is we use the assignment
operator to assign it to a variable that has the custom type which we have defined, which
is Person. One thing to keep in mind is that each object
is its own instance. So you could say Person person2=new Person(). namespace ClassesAndObjects
{ class Program
{ static void Main(string[] args)
{ Person person1=new Person();
Person person2=new Person(); }
} }
And what you’re doing here is you’re creating another instance of an object, which is still
using the class to know how its data and behavior are defined. That’s it, just a quick intro on classes
and objects. We will dive into more details. But for now that’s it. Please don’t forget to like the video and
subscribe to the channel. And I will keep updating you with the latest
content.

8 Replies to “Learn How To Program In C# Part 9 – Classes And Objects”

  1. Hi, great video. Just a small question. How do we scale up? Creating objects manually like person 1, person 2 etc would be tedious to do for say 1000 people? How can we create sets of objects from a database using a loop?

  2. Never really understood the multiple use of class call-out per line. This seems redundant to me. In your example, Person person1 = new Person(). It would seem that it would be enough to simply declare person1 = new Person(), for person1 to adopt the variables and methods of that class, but I know that this is incorrect. What does the first class declaration Person person1 add? Is there ever an instance where the first declaration would not match the second after the new keyword?

Leave a Reply

Your email address will not be published. Required fields are marked *