C# Classes Tutorial | Mosh

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay we're gonna start our journey by learning about classes in this lecture I'm gonna introduce you to the basics of classes I'm going to show you a real world example of classes and why we need them then I'm gonna talk about what is an object and how it's different from a class and finally we'll talk about static members so what is a class a class is simply a building block of a software application so in the real world an application consists of multiple classes each responsible for a particular behavior in the application and overall all these classes together we'll provide the behavior expected from the application let's take a look at a real world example we all are familiar with the concept of blogs and posts let's say we would like to implement a web application as a blog engine with a typical layered architecture or software application consists of at least three layers presentation business logic which was also referred to as domain layer and data access which is also referred to as persistence layer in each of these layers we can have classes each responsible for something particular for example in the presentation layer we can have a class like post view which is purely responsible for displaying a post to the user in the business logic or domain layer we can have the concept of post itself and finally in the data access or persistence layer we can have a class like post repository which knows how to save a post to the database or load it from a database a real-world application often consists of hundreds or even thousands of classes collaborating with each other so for you as a c-sharp programmer it's absolutely crucial to understand classes and how to use them effectively to build a maintainable clean robust reliable application this lecture is pretty basic but as we progress in this course in every lecture and in every section I teach you something new about classes by the end of this course you're going to learn a lot of materials about how to use classes and interfaces to build loosely coupled extensible and maintainable applications so let's continue our journey now we know a little bit about a class so it's a type that we developers define and give it a responsibility depending on the kind of application we're building and the layer that class belongs to but what is inside a class a class essentially has two parts data which is represented by fields and behavior which is represented by methods or functions here is how we illustrate a class in UML or unified modeling language UML is a graphical language for representing classes and their collaboration and we use that as a communication tool as part of building software so in UML a class has three parts the first part is the name of the class then here are the fields or attributes of that class which represent data about that class and finally we've got the methods or functions which provides some behavior so in this example our person class has Phil's like name age height and weight and it has methods like walk talk eat and sleep let's take a look at another example talked about blogs and posts so a blog post can have attributes or fields like title description and day time and it can have methods like publish like or comment which receives a message a term that is often used with classes and sometimes used interchangeably is an object but an object is fundamentally different from a class yet a lot of developers including myself sometimes use the word object to refer to a class but an object is essentially an instance of a class that resides in the memory so back to our person example if person is a class we can have objects like John Mary and Scott these are instances of that person the person class or the person type defines the blueprint from which we can create objects at runtime these objects in the memory talk to each other and as a result we get the behavior expected from the application now let's see how we can declare a class in c-sharp here is a basic syntax for declaring a class public and class our keywords are reserved keywords in c-sharp and that's why i've indicated them with blue is what we call an access modifier and that's something you're gonna learn later in this section so for now don't worry about it each class should have a name or an identifier like person in terms of naming convention and c-sharp we use paschal case to name our classes what is pascal case in pascal case the first letter of every word should be uppercase we also have camel case in which the first letter of the first word should be lowercase but the first letter of every word after that should be uppercase in still sharp we use camel case when naming parameters of methods as you will see later in this section so back to the declaration of our class the class can have fields and methods which we refer to as members of that class it put members inside these curly braces here we can declare a field for this class and call it name similar to the class we start with an access modifier in this case public then the datatype for that field and an identifier in the real world applications we shouldn't declare fields with public this is purely for taking you on a step-by-step journey without confusing you with a lot of details along the way as we progress in this section you will find the best way to declare fields okay now let's see how we can declare a method in this class very similar so we start with an access modifier and here is the signature of the method so a method can have a return type in this case void means it doesn't return any value then it has an identifier and here in brackets we can put any parameters that that method may need inside that method we have the code and here we can access any of the members of that class so in this example I'm simply using console table right line to display the name of that person on the console ok now that we have a class let's see how we can create an object or an instance from it to do that we use the new operator so we start with the type of the class in this case person we give it an identifier and then we use the new operator along with the name of the class to initialize an instance of that class note that here we are using camel notation for naming the person object a shorter way to write this code is to use var var is a c-sharp keyword and the reason we use that is to save us from typing the full name of a class because sometimes the name of the class might be quite a few characters plus as you see it's a little bit of duplication to repeat the name of the class in two places so we use the VAR keyword and the compiler automatically knows that this person object is of type person now that we have an object we can access members on that object like accessing the name field and giving a value or we can call one of its methods before we start coding let's talk about class members in c-sharp we have two types of class members we have instance members and study members an instance member is accessible from an object you have already seen that before so we create a person object and then call the introduced method on the object but we also have static members and a static member is accessible from the class not an object an example of that is console.writeline so console is a class and writeline is a static method so we don't have to create an instance of console using the new operator in order to call right line now you may wonder why do we use static members basically we use them to represent concepts that are singleton that means we should have only one instance of that concept in the memory think of the concept of the current date time we should have only one place in the memory where we know the current date time it doesn't make sense to have different objects it's telling us that current date time is a different value another example is console the right line when we run a console application basically we have only one console and that's why the console class and all his members are defined as static to declare a member as a study all you have to do is to use the static keyword after the access modifier you can use a static on any members of a class okay that's no fury let's open up visual studio and do some coding okay here I've got a console application in Visual Studio and that's where we're gonna do all the coding for this course you're not gonna get into the complexity of Windows applications or web applications or mobile apps we just want to learn about classes interfaces and object-oriented programming what you learn here you can always apply in any kind of application you're gonna build okay now let's start creating our first class so we create a class by public class let's give it a name like person let's define a field for it so a public string name again for now we're gonna use public until we get to the lecture about the access modifiers and then we're gonna learn a better way to define fields and let's create a method called introduce I want this method to take a parameter and that's the name of the other person that this person is going to introduce himself to so let's say introduce two and here we use the console class the right line method hi zero I am one basically what we have here is what we call a format string so it's like a template and here are parameters that we will fill by some values so I want the first parameter to be filled by the value of two argument and the second one is going to be the name field stored in the class now that we have this class let's create an object from it so we go to the main method we can either go person person equals new person or a shorter way is to use bar so with VAR well let the compiler figure out what is the type of this person object well compiler knows it's assigned to a new person object here so when you hover your mouse over VAR you see the tooltip says person we can assign this person named person that name equals a John and then we can call the introduced method and pass another name as an argument say introduce yourself to Marsh now if you run this application you see hi Marsh I'm John now let's take this to the next level let's say we would like to create a personal object from a string what we can do is to create a parse method so let's go and you find a parse method here the parse method should take a string and return a personal object so person parse string so STR basically what we need to do here is to create a person object set the name field based on the STR argument here and then return that so far personal equals new person we set the name to STR and finally returned that person now the interesting thing here is if you want to use this parse method we we have to use it on an object because it's an instance method so that means I can go here and say person dot parse and give it a name like John and that returns a person object so let's call it P and we can get rid of this name here for now see it's a little bit weird we have to create a person object first and then call the parse method to return a person object it's it just doesn't make sense that's where we use a static modifier so we can declare this parse method as a static and this way we don't have to create a person object first in order to parse a string so we can simply change this go to use the person class we parse that string and now we have a person object and finally we can call the introduced method on it let's run the application we get the same result so that's a use case for using a static member a static member is only accessible from the class and not an object that's pretty much it for this lecture in the next lecture we're going to learn about constructors I hope you enjoyed this lecture and thank you for watching hi guys ma chere I hope you're having a fantastic day or night wherever you are in the world this tutorial you have been watching is actually part of one of my C sharp courses where you will learn everything about classes interfaces and object-oriented programming in case you're interested to enroll in the full course I've put the link for you in the video description and if not that's perfectly fine have a great day
Info
Channel: Programming with Mosh
Views: 188,994
Rating: undefined out of 5
Keywords: c#, csharp, .net, programming with mosh, code with mosh, c# (programming language), c# programming, learn c#, c# tutorial, object oriented programming, c sharp
Id: ZqDtPFrUonQ
Channel Id: undefined
Length: 14min 25sec (865 seconds)
Published: Wed Nov 08 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.