How to make a function in 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# function
public void SayHello()
{
Console.WriteLine("Hello") ;
}
Example 4: c# funtion
public int AddNumbers(int number1, int number2){ int result = number1 + number2; if(result > 10) { return result; } return 0;}
Example 5: C# func
Func<string> YourFuncProperty
= new Func(()=>{ return "or return what you want";});
Func<string> YourFuncProperty
= ()=>{return "or return what you want";};
Func<int
,
string> YourParamitarizedFuncProperty =
(x)=>
{return $"you entered a {x} or return what you want";};
string YouReturn = YourFuncProperty.Invoke();
string YouReturn = YourParamitarizedFuncProperty.Invoke(5);
YourFuncProperty.BeginInvoke();
YourParamitarizedFuncProperty.BeginInvoke(5);
string YouReturn = YourFuncProperty.EndInvoke();
string YouReturn = YourParamitarizedFuncProperty.EndInvoke(5);
Func<string> YourActionDefinedProperty = YourDefinedMethod;
string YourDefinedMethod()
{
return "or return what you want";
}
public sealed class DataContainer
{
static func<string> SealedFuncStringOverride;
DataContainer(datetime Date)
{
this.Date = Date;
}
public datetime Date {get; private set;}
public int Amount {get; private set;}
public string Info {get; private set;}
public string FirstName {get; private set;}
public override string ToString()
{
if(SealedFuncStringOverride!=null)
return SealedFuncStringOverride.BeginInvoke();
return base.ToString;
}
}
Example 6: function c#
public int AddNumbers(int number1, int number2)
{
int result = number1 + number2;
return result;
}