C# Methods : In Depth With Example

Since so long we have seen overview of basic programming structure of C#. In that we need to cove most important aspect of any programming language, we call it as Methods. Methods hide working of code that need to be performed repeatedly. We can call methods need to perform specific peace of code again and again. So it performs abstraction and code re-usability reducing the work of programmers.

C#-Methods-In Depth-With-Example

Before we move on further one must understand the structure of methods. This will make you understand better how programing structure is.

Defining Methods In C#

(Parameter List)
//body/code/ logic of methode

  • Access Specifiers:
    Access specifies denote wheather your class will be accessible to which elements. Mostly 3 access specifiers are used viz. Public, Private and protected. You can read here in more detail about access specifiers here.
  • Return Type:
    Return type defines what method is going to return some value, if yes then you need to define the type of value. Blow Used define method type we have created maxoftwo method that is going to return integer value(int). So whenever any method return any value such as int, float, double, string. If method is not returning any value then it must be marked as void.
  • Method Name:
    Method name is the name we will use for accessing that method. If we have class program with object name p. Under program we have method name called as abc(); So while accessing that method abc() is the name we will use. We can see this in user-define method example after sometime. But as some implied rules generally method name must be something that define it's function. Some logical name must be provided.
  • Parameter List :
    Parameter list is the argument that sometime required by methods. Some system define as well as user define methods require an argument to pass on in order to get the desire result from that method. But this parameters are option for methods. If method not reuqire it just leave that round bracket blank.
Methods are most important part of class and it defines the behavior of the class. All action about what class is all about which function must be perform by class is determine by methods define within that class. There are 2 type if class :
  • System Define Methods:
    This methods are usually defined by programming system itself. They are part of programing package and their definitions is already define by system. 
    We generally use Console.WriteLine(); and Console.ReadLine(); are 2 system define methods. WriteLine(); is used to print the parameters as text that display while executing the program on runtime.
    On the other hand ReadLine(); is used to take input from user unless system encounter Enter command.
    Printing text on runtime and Accepting data from user it define by Console class methods namely WritLine() And ReadLine(); Programmer just need to user it properly. Like there are many methods are define by system to help programmer making work easier for them.
  • User Define Methods :
    This methods are also called as custom methods. This methods are not a System defined they are define for different costume purpose.
    Let's understand this with the help of an example. In the program below we have created method called maxoftwo accepting 2 parameters for comparing 2 numbers. This is user define method that we have access through class object.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
public class Program
public int maxoftwo(int a, int b)
int res;

return res;

public static void Main(string[] args)
int a=100,b=200;
int res;
Program p= new Program();
res= p.maxoftwo(a,b);

Console.WriteLine("Max Value is {0}",res);
Output :

Max Value is 200
In the example above code Program is the class where method maxoftwo resides. For accesing the same we user the period sign. But before that we need to create object p. We created object busing new keyword.  After doing this we can access method using
 in our code check this code for above expression:

Share this

Related Posts

Next Post »