using System;
namespace ConsoleApplication1
{
class Program
{
delegate int delDemo(int n);
static void Main(string[] args)
{
delDemo delObj = y => y * y;
Console.WriteLine("Enter a number");
int x = Convert.ToInt32(Console.ReadLine());
int j = delObj(x);
Console.WriteLine("The squre of {0} is {1}",x,j);
Console.ReadLine();
}
}
}
using System;
namespace ConsoleApplication1
{
class Program
{
delegate int factDelegate(int n);
static void Main(string[] args)
{
int fact = 1;
factDelegate delObj = y => {
for (int i = 1; i <= y; i++)
{
fact = fact * i;
}
return fact;
};
Console.WriteLine("Enter a number");
int x = Convert.ToInt32(Console.ReadLine());
int j = delObj(x);
Console.WriteLine("The factorial of {0} is {1}", x, j);
Console.ReadLine();
}
}
}
using System;
namespace ConsoleApplication1
{
class FuncDemo
{
//ShowWelcome() method does not take parameter and return string type.
public string ShowWelcome()
{
return string.Format("Welcome at TutorialRide");
}
//Add method takes two parameter and return one integer type.
public int Add(int a, int b)
{
return a + b;
}
//ShowInformation method takes two string parameter and return one string type.
public string ShowInformation(string firstName, string lastName)
{
return string.Format("Your Name is {0} {1}", firstName, lastName);
}
}
class Program
{
static void Main(string[] args)
{
FuncDemo obj = new FuncDemo();
Func<string> welcomeObj = obj.ShowWelcome;
Func<int, int, int> addObj = obj.Add;
Func<string, string, string> showObj = obj.ShowInformation;
Console.WriteLine("Enter the First Number");
int a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the Second Number");
int b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the First Name");
string firstName = Console.ReadLine();
Console.WriteLine("Enter the Last Name");
string lastName =Console.ReadLine();
Console.WriteLine("\n**********************\n");
Console.WriteLine(welcomeObj());
Console.WriteLine("The addition of {0} and {1} is = {2}",a,b,addObj(a,b));
Console.WriteLine(showObj(firstName,lastName));
Console.WriteLine("\n**********************\n");
Console.ReadLine();
}
}
}