You are on page 1of 6

Back To Module Home

Object-oriented Programming
in C#

4% completed

Search Module

Introduction to Object-
Oriented Programming
in C#

Classes and Objects

Introduction to Objects and Classes


Got any feedback? Get in touch with us.
Declaration and Implementation

Accessing the Class Members

Access Modifiers

Fields

Methods

Static and Non-Static Methods

The 'this' Reference Variable


Declaration and Implementation
In this lesson, you will learn about the declaration and implementation details of a class.

We'll cover the following

• Declaration
• Creating a Class Object
• Implementation of the VendingMachine Class
• Naming Conventions

Declaration
In C#, we declare classes in the following way:

1 class ClassName // Class name


2 {
3 Got any feedback? Get in touch with us.
4 /* All member variables
5 and methods */
6
7 }

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:

1 class ClassName //Class name


2 {
3 ...
4 }
5
6 class Program //Class having the Main() method
7 {
8 public static void Main(string[] args) //Main method
9 {
10 ClassName objectOneName = new ClassName(); //ClassName first object
11 var objectTwoName = new ClassName(); //ClassName second object
12 }
13 }

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.

Implementation of the VendingMachine


Class
Let’s implement the VendingMachine class illustrated below:

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.

• While naming a class, we should use the PascalCase.

• While naming the methods in a class we should use the


Got any . in touch with us.
feedback? Get
PascalCase

The naming conventions of fields will be discussed in the upcoming lesson.

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

Introduction to Objects and Classes Accessing the Class Members

Got any feedback? Get in touch with us.

You might also like