Method is a code block that contains a series of statements. A program causes the statements to be executed by calling the method and specifying any required method arguments. In C#, every executed instruction is performed in the context of a method. The Main method is the entry point for every C# application and it is called by the common language runtime (CLR) when the program is started.
To use a method, you need to −
Define the method
Call the method
Example With- the method:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Method
{
class Program
{
public static void Main(string[] args)
{
add();
Console.ReadKey();
}
static void add()
{
int x;
int y;
int z;
Console.WriteLine("enter two numbers");
x = int.Parse(Console.ReadLine());
y = int.Parse(Console.ReadLine());
z = x + y;
Console.WriteLine(z);
}
}
}
Call the method:
Example With-Call the method:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace callingmethod
{
class Program
{
public static void Main(string[] args)
{
int z = add();
Console.WriteLine("the sum of {0}",z);
Console.ReadKey();
}
static int add()
{
int x;
int y;
int z;
Console.WriteLine("enter two numbers");
x = int.Parse(Console.ReadLine());
y = int.Parse(Console.ReadLine());
z = x + y;
return z;
}
}
}
Comments
Post a Comment