0% found this document useful (0 votes)
15 views

Methods - Learn Object-Oriented Programming in C#

This document provides an introduction to methods in object-oriented programming in C#. It discusses the purpose of methods, how they are defined and implemented, how they can take parameters and return values, and method overloading. Method overloading allows a method to perform different operations based on the number and type of arguments passed to it. This provides advantages like cleaner code and not having to track different method names.

Uploaded by

Naj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Methods - Learn Object-Oriented Programming in C#

This document provides an introduction to methods in object-oriented programming in C#. It discusses the purpose of methods, how they are defined and implemented, how they can take parameters and return values, and method overloading. Method overloading allows a method to perform different operations based on the number and type of arguments passed to it. This provides advantages like cleaner code and not having to track different method names.

Uploaded by

Naj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Back To Module Home

Object-oriented Programming
in C#

10% 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


Methods
In this lesson, you will get to know the role of methods in classes.

We'll cover the following

• The Purpose of Methods


• Definition and Implementation
• Method Parameters, Return Type
• Return Statement
• Method Overloading
• Advantages of Method Overloading

The Purpose of Methods


In general, methods act as an interface between a program and the fields of a class
in that program. Got any feedback? Get in touch with us.

These methods can either alter the data stored in the fields or use their existing
values to perform a certain computation. All the useful methods should be public,
although, some methods which do not need to be accessed from the outside could
be kept private.

Definition and Implementation


A method is a group of statements that performs some operations and may
or may not return a result.

Here is an example of a Buy() method implemented inside the VendingMachine class.


The method is being invoked in the Main() method, inside the Demo class:

1 class VendingMachine {
2
3 // Public method implementation to print print the bought product
4 public void Buy(string product) {
5 Console.WriteLine("You bought: " + product);
6 }
7
8 }
9
10 class Demo {
11
12 public static void Main(string[] args) {
13 var vendingMachine = new VendingMachine();
14 vendingMachine.Buy("Chocolate"); // Calling public method
15 }
16
17 }
Got any feedback? Get in touch with us.

Run Save Reset

Method Parameters, Return Type


Method parameters make it possible to pass values to a method and return type
makes it possible to get a result returned by the called method. The parameters are
declared inside the parentheses after the method name while the return type is
declared before method name.

Return Statement
For methods that define a return type, the return statement must be immediately
followed by the return value.

1 // public method which returns the manufacturer


2 string manufacturer = "Vendy Inc.";
3
4 public string GetManufacturer() {
5
6 return manufacturer; // return statement
7 }

The above method returns the name of the manufacturer.

The return type, string, which comes before the method name GetManufacturer,
indicates that this method returns a string.
Got any feedback? Get in touch with us.

Method Overloading

Overloading refers to making a method perform different operations based


on the nature and type of its arguments.
Methods can be overloaded in C#.

We could redefine a method several times with the same name but with a different
number of arguments and/or types. When the method is called, the appropriate
definition will be selected by the compiler at the compile time.

Let’s see this in action by overloading the product method in the Calculator class:

1 class Calculator {
2
3 public double Product(double x, double y) {
4 return x * y;
5 }
6
7 // Overloading the function to handle three arguments
8 public double Product(double x, double y, double z) {
9 return x * y * z;
10 }
11
12 // Overloading the function to handle int
13 public int Product(int x, int y){
14 return x * y;
15 }
16
17 }
18
19 class Demo {
Got any feedback? Get in touch with us.
20
21 public static void Main(string[] args) {
22 Calculator calculator = new Calculator();
23
24 double x = 10;
25 double y = 20;
26 double z = 5;
27
28 int a = 12;
29 int b = 4;
30
31 Console WriteLine(calculator Product(x y));

Run Save Reset

In the code above, we see the same method behaving differently when encountering
different number and types of arguments.

Note: Methods that have no arguments and differ only in the return types
cannot be overloaded since the compiler won’t be able to differentiate
between their calls.

Advantages of Method Overloading


One might wonder why we wouldn’t simply create new methods to perform
different jobs rather than overloading the same method.

• An obvious benefit is that the code becomes simple and clean.

• We don’t have to keep track of different method names.

Polymorphism is a very important concept in object-oriented programming. It will


Got any feedback? Get in touch with us.
come up later on in the course, but method overloading plays a vital role in its
implementation.

In the next lesson, you will get to know about static and non-static methods.

Back Mark As Completed Next


Fields Static and Non-Static Methods

Got any feedback? Get in touch with us.

You might also like