functions csharp

Code Example - functions csharp

                
                        public void SayHello(string name) 
{
    Console.WriteLine("Hello");
}

public void SayName()
{
	Console.WriteLine("What is your name?");
	string name = Console.ReadLine(); 
	SayHello(name);
}
                    
                
 

methods csharp

                        
                                Search on YT: Brackeys C# Methods (for a good explaination)
                            
                        
 

csharp funtion

                        
                                public int AddNumbers(int number1, int number2){    int result = number1 + number2;    if(result > 10)    {    return result;    }    return 0;}
                            
                        
 

csharp func

                        
                                //Func is a invokeable container for Mehod or function method 
//that returns something. They can accept inputs based on the generals 
//that you template on and reserve the last general template for the return. 
//If you do not need a return or wish to return nothing check out the Action Class
//example:
Func<string/*Or some other type to return*/> YourFuncProperty
= new Func(()=>{/*do something*/ return "or return what you want";});
//or
Func<string/*Or some other type to return*/> YourFuncProperty
= ()=>{/*do something*/return "or return what you want";};
//for a paramiterized func
Func<int
/*Or some other types followed by others optional comma seperated*/,
string/*Or some other type to return last*/> YourParamitarizedFuncProperty =
  (x/*Each Param will be to the comma seperated types*/)=>
  {/*do some with the inputs*/return %%%~COMPRESS~PRE~3~%%%quot;you entered a {x} or return what you want";};

// you can invloke them by calling their invokes.
string YouReturn = YourFuncProperty.Invoke();
string YouReturn = YourParamitarizedFuncProperty.Invoke(5);
//The last is the basic sycronous way. For a aysnc call uses
YourFuncProperty.BeginInvoke();
YourParamitarizedFuncProperty.BeginInvoke(5);
//however, you will need to begin a await with EndInvoke to get your result after.
string YouReturn = YourFuncProperty.EndInvoke();
string YouReturn = YourParamitarizedFuncProperty.EndInvoke(5);

//You can also fill them with defined methods in a class if you wish, 
//but the signatures must match.
Func<string> YourActionDefinedProperty = YourDefinedMethod;
string YourDefinedMethod()
{
  //do something
  return "or return what you want";
}

//Example of use
public sealed class DataContainer
{
  	//A bit contrived but we will give the ablity to overide the printout 
  	//of the class while still keeping it sealed. See the invoke at ToString.
  	//could be useful in a library or something?
	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;}
  
  	//The invoke is used in here.
  	public override string ToString()
   	{
      if(SealedFuncStringOverride!=null)
        return SealedFuncStringOverride.BeginInvoke();
      return base.ToString;
   	}
}
                            
                        
 

function csharp

                        
                                public int AddNumbers(int number1, int number2)
{
    int result = number1 + number2;
    return result;
}
                            
                        
 

function in csharp

                        
                                //function example
using System;
					
public class Program
{
	static void function(){
		Console.WriteLine("I am a function!");
	}
	public static void Main()
	{
		function();
	}
}
                            
                        
 

how to define a function in csharp

                        
                                //The "void" in this instance refers to the return type of this function
//This function won't have a return value.
void functionName(int parameter) { 
  //Code you want to run
}
//Calls the code in the function
functionName(Aninteger)
                            
                        
 

csharp method

                        
                                /* --Syntax--
[] = Optional
<> = Required
[modifiers] <return type> <identifier>([<parameter type> <parameter identifier>])
{
  //Code block
  //Return is required if type is not 'void'
  return null;
} */
//Example
/*Modifiers   Return Type	Identifier	Parameters, optional, separate with comma*/
public static bool 			MyFunc		(int x, int y)
{
  	x = x * 2;
    return x > y; //Omittable if return type is void
}
//Shortened to return the given expression |Expression body definition|	
public static int answerToEverything() 		=> 42;