Declaration and Implementation - Learn Object-Oriented Programming in C#
Declaration and Implementation - Learn Object-Oriented Programming in C#
Object-oriented Programming
in C#
4% completed
Search Module
Introduction to Object-
Oriented Programming
in C#
Access Modifiers
Fields
Methods
• Declaration
• Creating a Class Object
• Implementation of the VendingMachine Class
• Naming Conventions
Declaration
In C#, we declare classes in the following way:
The keyword class tells the compiler that we are creating our custom class. All the
members of the class will be defined within the class scope, i.e., inside the curly
brackets, {...}.
Creating a Class Object
The name of the class, ClassName, will be used to create an instance/object of the class
in our Main() method. We can create an object of a class by using the new keyword:
In the above snippet, we can see that there are two ways we can create an object of
a class. We can use the name of the class on both sides of the assignment operator or
we can use the var keyword on the left side and the name Got
of any
thefeedback?
class onGet in touch with us.
the other.
When using var, the compiler automatically infers the type of the object being
created from the right side of the assignment operator.
�
� Whenever the compiler comes across the keyword new, it realizes that
an object is being created and allocates a separate memory location for the
object being created.
Class Diagram
1 class VendingMachine //Class name Got any feedback? Get in touch with us.
2 {
3 //Class fields
4 int count;
5 int capacity;
6 int moneyCollected;
7 string manufacturer;
8
9 //Class Methods
10 void Display()
11 {
12 //Method definition goes here
13 }
14 void DispenseProducts()
15 {
16 //Method definition goes here
17 }
18 void Refill()
19 {
20 //Method definition goes here
21 }
22
23 }
Now that we have learned how to declare and implement a class in C#, let’s briefly
discuss the naming conventions we should follow when working with classes.
Naming Conventions
• The name of a class should preferably be a noun so that it represents the actual
purpose of creating that class, e.g., VendingMachine, Calculator, etc.
We’ve seen the structure of a class and the basic skeleton of the VendingMachine class.
In the next lesson, we will build upon this by creating an object of the vending
machine class and accessing the class members through it.
Back Mark As Completed Next