create function c# code example
Example 1: How to make a function in C#
public void SayHello(string name)
{
Console.WriteLine("Hello")
}
Console.WriteLine("What is your name?")
string name = Console.ReadLine() ;
SayHello(Adam)
Example 2: c# functions
using System;
public class Program
{
public static void Main()
{
Console.WriteLine(square(4));
Console.WriteLine(cube(4));
}
public static int square(int n)
{
return n * n;
}
public static int cube(int n)
{
return n * n * n;
}
}
Example 3: c# funtion
public int AddNumbers(int number1, int number2){ int result = number1 + number2; if(result > 10) { return result; } return 0;}
Example 4: function c#
public int AddNumbers(int number1, int number2)
{
int result = number1 + number2;
return result;
}
Example 5: c# func
using System;
using System.Collections.Generic;
using static System.Console;
namespace CSFlow.Delegates.Others
{
public class ActionFunc
{
public static void Run ()
{
Action<string> display = new Action<string>(DisplayMessage);
display("Calculate Discount :");
Func<double, double> discount = new Func<double, double>(Discount);
display(discount(12.5).ToString());
List<Customer> custList = new List<Customer>();
custList.Add(new Customer { Id = 1, FirstName = "Joydip", LastName = "Kanjilal", State = "Telengana", City = "Hyderabad", Address = "Begumpet", Country = "India" });
custList.Add(new Customer { Id = 2, FirstName = "Steve", LastName = "Jones", State = "OA", City = "New York", Address = "Lake Avenue", Country = "US" });
custList.Add(new Customer { Id = 3, FirstName = "Sefat", LastName = "Anam", State = "OSA", City = "New York", Address = "Manhatten", Country = "US" });
Predicate<Customer> FindAddress = customer => customer.Address == "Manhatten";
Customer searchData = custList.Find(FindAddress);
display($"{searchData?.FirstName} {searchData?.LastName} From - {searchData?.City} ");
ReadKey();
}
static void DisplayMessage (string message)
{
WriteLine(message);
}
static double Discount (double money)
{
return money * .5;
}
class Customer
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
}
}
}