What is a method in C# and how do you define one?
Answer
A method is a container containing piece of a computer program code that does something. A method encapsulates the code and allows the other parts of a computer program to run the code within a method. We say the method is being called by other code.
Once the method gets called, the code contained within the method gets executed by the computer. During the execution some result can get calculated. In the end of the method execution the calculated result can be returned to a caller. There can also be methods that don't return anything. They just do something but they don't return any result.
Each method must be placed within a class. So if you plan to define a new method, you have to create a new class first or you need to put the method to a class that already exists.
In the code below, we defined four methods and put them to the class Radio that represent our simulation of radio.
The first method is called TurnOn and when it gets executed, it turns the radio on. It doesn't return any result.
The second method is called TurnOff and when it gets executed, it turns the radio off. It doesn't any result.
The third method is called SetVolumeLevel and when it gets executed, it sets the radio volume level to a level that the caller passes. It doesn't return any result.
The fourth method is called GetVolumeLevel and when it gets executed, it gets the current radio level and returns it to the caller.
Related C# job interview questions
Chat
Oh, the operator is not available. Leave us your comments. We will answer all your questions as soon as possible.
e
@@xeDO0
1'"
e'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
L7oVYP7m')) OR 312=(SELECT 312 FROM PG_SLEEP(15))--
A1v25QPv') OR 393=(SELECT 393 FROM PG_SLEEP(15))--
kxT46vOm' OR 479=(SELECT 479 FROM PG_SLEEP(15))--
VTgcz37T'; waitfor delay '0:0:15' --
1 waitfor delay '0:0:15' --
(select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
if(now()=sysdate(),sleep(15),0)
-1" OR 3+906-906-1=0+0+0+1 --
-1" OR 2+906-906-1=0+0+0+1 --
-1' OR 3+316-316-1=0+0+0+1 or '8BoDIAd6'='
-1' OR 2+316-316-1=0+0+0+1 or '8BoDIAd6'='
-1' OR 3+137-137-1=0+0+0+1 --
-1' OR 2+137-137-1=0+0+0+1 --
-1 OR 3+877-877-1=0+0+0+1
-1 OR 2+877-877-1=0+0+0+1
-1 OR 3+418-418-1=0+0+0+1 --
-1 OR 2+418-418-1=0+0+0+1 --
e
e